PySpark Stubs (pyspark-stubs) provides automatically generated type stubs for the Apache PySpark library. These stubs enable IDEs and static type checkers like MyPy to provide intelligent code completion, detect common programming errors, and improve code quality by enforcing type safety in PySpark applications. The current version is 3.0.0.post3, typically updated to align with major PySpark releases, often with `post` versions for stub refinements.
pip install pyspark-stubsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use PySpark with type hints, leveraging `pyspark-stubs`. You install `pyspark-stubs` alongside your `pyspark` installation. When a type checker like MyPy processes this code, it uses the installed stubs to validate types for PySpark objects like `SparkSession` and DataFrame methods. The stubs themselves have no runtime effect; they only assist static analysis.
Ensure `pip install pyspark-stubs==X.Y.*` where `X.Y` matches your `pyspark` major.minor version. E.g., for PySpark 3.0.x, use `pyspark-stubs==3.0.*`.
Install `pyspark` (e.g., `pip install pyspark`) in addition to `pyspark-stubs` if you intend to run PySpark code.
Understand that `pyspark-stubs` is a development-time dependency for type checking, not a runtime dependency. If you encounter runtime errors, they are related to your PySpark setup, not the stubs.