HyukjinKwon opened a new pull request, #777:
URL: https://github.com/apache/spark-connect-rust/pull/777

   ### What changes were proposed in this pull request?
   
   Static type checkers now see PySpark's API on the drop-in, as on official 
`pyspark-client` 4.3.0. Two things hid it:
   - `python/pyspark/sql/functions.py` loads `functions_generated.py` through 
`importlib` and injects the functions with `globals()`, so type checkers could 
not see them;
   - the Rust-backed classes of `pyspark._pyspark` had no stubs.
   
   **`scripts/gen_type_stubs.py`** generates `.pyi` stubs from PySpark v4.3.0's 
annotated sources. It parses `--pyspark-src` and imports none of it, with an 
AST transform:
   - It keeps the signatures, annotations, overloads, properties, class 
members, type aliases, `__all__` and PySpark's default values.
   - It drops bodies, docstrings, doctests and the implementations of 
overloaded functions.
   - `TYPE_CHECKING` blocks and `sys.version_info` checks are kept as a type 
checker reads them.
   - Decorators:
     - the typing-relevant ones are kept;
     - PySpark's dispatch decorators are dropped only after checking, in 
PySpark's source, that they are typed `(f: FuncT) -> FuncT`;
     - an unknown decorator is an error.
   - A name that a module of PySpark imports from `pyspark` is re-exported only 
if the drop-in's module has it.
   - `EXTRAS` adds the names only the drop-in's modules have (`FrameBound`, 
`Trigger`, the listener events of `streaming.query`, ...).
   - `--check` reports drift and stale stubs. The output is the same on Python 
3.9 to 3.13.
   
   **The stubs**, 43 files:
   - `pyspark.sql.functions` is a stub-only package beside `functions.py`: 
`pyspark/sql/functions/{__init__,builtin,partitioning}.pyi`, with no 
`__init__.py`.
     - mypy and pyright both prefer it to the module, and resolve 
`pyspark.sql.functions.builtin` and `partitioning`. A `functions.pyi` would not 
resolve those submodules, checked on a toy package.
     - The runtime still imports `functions.py`, as a module takes precedence 
over a namespace directory (PEP 420), checked on Python 3.9 to 3.13.
   - `pyspark.sql.{catalog, column, conf, dataframe, group, merge, observation, 
pandas.group_ops, readwriter, table_arg, tvf, types, window, udf, udtf}`, 
`sql.streaming.{query, readwriter}`, `sql.avro.functions`, 
`sql.protobuf.functions`, `pyspark.storagelevel` and `pyspark.util`.
   - 19 modules of `pyspark.sql.connect`.
   
   **CI:**
   - the "Generated files drift" workflow runs `gen_type_stubs.py --check` 
beside `gen_fn_dispatch.py --check`;
   - `coverage.yml` installs `mypy==2.4.1`, and runs the new 
`python/tests/test_type_stubs_offline.py`:
     - mypy on a user's application (`python/tests/type_stubs_user_app.py`) 
reports exactly its one deliberate error, with PySpark's revealed types;
     - all 562 names of `pyspark.sql.functions.__all__` are typed, none `Any`;
     - every public runtime name of each stubbed module is in its stub;
     - the vendored `pyspark/sql/pandas/functions.pyi` has no errors;
     - Python still imports `functions.py`.
   - `scripts/tests/test_gen_type_stubs.py` tests the generator.
   
   `docs/compatibility.md`, `docs/contributing.md` and `dev/design/ci.md` 
describe the stubs, their regeneration and what remains different.
   
   ### Why are the changes needed?
   
   With the official client, mypy reports only real errors. With the drop-in, 
it reported `attr-defined` errors for valid code, and revealed `Any` for 
DataFrames and functions, so it did not catch type errors:
   
   | | official pyspark-client 4.3.0 | before | after |
   |---|---|---|---|
   | names of `pyspark.sql.functions.__all__` that mypy cannot see | 0 | 518 of 
552 | 0 |
   | the user application: errors | 1 (the deliberate one) | 5 false 
`attr-defined` (`desc`, `row_number`, `upper`, `sum`, `first`), and not the 
deliberate one | 1 (the deliberate one) |
   | `reveal_type(df)` | `pyspark.sql.dataframe.DataFrame` | `Any` | 
`pyspark.sql.dataframe.DataFrame` |
   | `overload-cannot-match` on the vendored `pyspark/sql/pandas/functions.pyi` 
| 0 | 3 to 4 | 0 |
   
   Over 5,042 public names and members that exist on both runtimes:
   
   | | reveals different from official | `Any` | attribute errors |
   |---|---|---|---|
   | before | 4,454 | 4,337 | 2,128 |
   | after | 12 | 9 | 18 |
   | official | | 8 | 17 |
   
   The revealed types of all 562 functions are the same as the official 
client's under mypy 2.4.1 and pyright 1.1.414.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. mypy, pyright and IDEs see PySpark's API of these modules, as with 
`pyspark-client`. The runtime is unchanged, and the wheel ships the stubs with 
`py.typed`.
   
   ### How was this patch tested?
   
   - `test_type_stubs_offline.py` (5 tests) and 
`scripts/tests/test_gen_type_stubs.py` (5 tests). All of the first fail on 
master.
   - `gen_type_stubs.py --check`.
   - The offline suite of `coverage.yml`.
   - mypy and pyright on the application against the official client, against 
this branch, and against an unzipped wheel built from it.
   - ruff 0.14.0, `test_ruff_excludes.py` and `scripts/tests`.
   
   The stubs re-export some top-level names of the drop-in's modules 
(`pyspark.sql.utils`, `connect.plan`, `connect.client`, ...). A change of those 
names needs a regeneration, which the drift check reports.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to