Skip to content

Static checking

TypeSpark moves as many mistakes as it can out of Spark jobs and into your editor. This page is where the guarantees start and stop: what your type checker catches, what TypeSpark checks when it builds a DataFrame, what only Spark notices, and what you vouch for yourself.

Caught By When
Wrong column names, keywords, types, arguments your type checker while you write the code
Missing columns, invalid defaults, explode misuse TypeSpark when a typed DataFrame is built
Mistakes in plain PySpark strings, bad data Spark when the query runs
Data that doesn't match the model you when data enters a model

By your type checker

Models, columns and typed functions are ordinary typed Python, so pyright and mypy check them like any other code:

Models
import typespark as ts
import typespark.functions as tsf


class Person(ts.DataFrame):
    name: ts.String
    age: ts.Int


class Adult(ts.DataFrame):
    name: ts.String
    can_vote: ts.Bool


class Order(ts.DataFrame):
    order_id: ts.String
    amount: ts.Double
people.aeg  # type error: no such column
Adult(people, name=people.name)  # type error: can_vote missing
Adult(people, name=people.name, can_vote=people.age)  # type error: Int, not Bool
vote = people.age >= 18
Adult(people, name=people.name, can_vote=vote, x=vote)  # type error: no column x
tsf.upper(people.age)  # type error: upper takes a String
tsf.date_trunc("DAYS", people.age)  # type error: not a unit, and not a Timestamp
adults(orders)  # type error: an Order is not a Person

The same goes for enum values (StringOf, IntOf) and join types.

When a typed DataFrame is built

Some things only show up once there is data: TypeSpark checks them when it builds a typed DataFrame, before Spark runs a query.

  • A column the model needs is missing from the data: MissingColumnError.
  • A default that can't apply to its field: InvalidDefaultColumnError, InvalidLiteralDefaultError.
  • An exploded element used outside explode(access): ExplodeAccessError.

See Errors for every error, its message and how to fix it.

Only by Spark

TypeSpark can't see inside strings or data:

  • Plain PySpark. Code that uses column names as strings (F.col("aeg"), withColumn("x", ...), raw SQL) is checked by Spark when the query runs, as in plain PySpark.
  • The data itself. Values that don't fit an operation, such as a string that can't be cast to a number, fail or turn null in Spark.

Your responsibility: the data matches the model

Wrapping a DataFrame in a model with from_df() is a declaration: you vouch that the data has the model's columns, with the model's types. TypeSpark takes the model at its word and types everything from there.

TypeSpark does check that the columns exist (MissingColumnError). It doesn't check their types, nullability or values:

  • Types. If age holds strings while the model says ts.Int, the typed code still type checks; Spark fails or returns null when it computes with age.
  • Nullability. nullable=False is schema metadata for the generated schema. Nulls in the data aren't checked.
  • Enum values. StringOf[Literal["open", "closed"]] checks your code, not the rows.

Make sure the data matches where it enters your code: read tables whose schema you control, for example tables created from Model.generate_schema(), and check incoming data at that edge where it matters.

Escape hatches

Checking stops where you leave the typed API, by design:

  • to_spark() gives you the plain PySpark DataFrame or column.
  • ts.String.wrap(col) (and the other types) tells TypeSpark what an untyped Spark column holds, without checking.
  • # type: ignore silences your type checker for one line.

Set up your type checker

TypeSpark is checked with pyright (and Pylance, its VS Code version) and mypy. Neither needs a plugin or configuration: models are declared through dataclass_transform (PEP 681), which both understand natively.

Pyright and VS Code

Pyright works as is. In VS Code, Pylance's type checking is off by default, which hides the errors TypeSpark relies on, such as a missing column or a wrong column type. Turn it on in your settings:

{
  "python.analysis.typeCheckingMode": "standard"
}

mypy

mypy works as is.

Untyped lambda converters

With an untyped lambda converter and a literal default (ts.field(converter=lambda x: ..., default=1)), mypy infers the field from the literal. Use a typed converter function instead; it works in both checkers.