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

   ### What changes were proposed in this pull request?
   
   A follow-up of #777 (SPARK-61854), #817 (SPARK-61949) and #818 
(SPARK-61950), from a review of the type stubs against pyspark-client 4.3.0 
with mypy 2.4.1 and pyright 1.1.414. Each item was probed with both checkers on 
both clients.
   
   **1. `pyspark.sql.connect.proto` has a stub.**
   - PySpark's package star-imports the messages of its generated modules 
(`python/pyspark/sql/connect/proto/__init__.py`). The drop-in's installs its 
native messages in `globals()`, so type checkers saw none: mypy reported 
`Module has no attribute "Plan"`, pyright `"Plan" is not a known attribute of 
module "pyspark.sql.connect.proto"`, and every annotation of a message in 
PySpark's modules was `Any`, such as the `plan` of 
`SparkConnectClient.to_table` and `MergeIntoWriter._matched_actions`.
   - The generator now stubs the package as PySpark's module: its star imports 
of the modules whose stubs (`base_pb2.pyi`, ...) the drop-in ships as 
PySpark's. It leaves out the star import of `base_pb2_grpc`, the gRPC service, 
which the drop-in does not have, with a new `DROPPED_STAR_IMPORTS`. A star 
import of a module that the drop-in does not have is now an error of the 
generator.
   - The 211 names that the stub gives the package are those that it has at run 
time.
   
   **2. The Avro and Protobuf functions of `pyspark.sql.connect` have stubs.** 
`pyspark.sql.connect.avro.functions` and 
`pyspark.sql.connect.protobuf.functions` copy their functions into `globals()`, 
so `from_avro`, `to_avro`, `from_protobuf` and `to_protobuf` were unknown to 
type checkers. They are in `STUB_MODULES` now. `DROPPED` leaves out the names 
that PySpark's modules import and the drop-in's do not have: `lit` of the Avro 
module, and `Column` and `lit` of the Protobuf module.
   
   **3. The modules of `pyspark.ml.connect` have stubs.**
   - The drop-in's modules of its native ML classes (classification, 
clustering, evaluation, fpm, pipeline, recommendation, regression and tuning) 
replace each native class with its public class 
(`pyspark.ml._native_classes.publish`). The public class is a subclass of the 
public classes of its bases, and of PySpark's class of its name in `pyspark.ml`.
   - None of these modules had a stub, nor did their package. So:
     - mypy typed their classes `Any`;
     - pyright reported `"Pipeline" is not exported from module 
"pyspark.ml.connect.pipeline"`;
     - `from pyspark.ml.connect import VectorAssembler` was an error.
   - Their stubs are made from the drop-in's own modules 
(`ML_CONNECT_MODULES`), not from PySpark's modules of these paths, which are 
its ML API on PyTorch:
     - each native class is `class X(<public bases>, pyspark.ml.<module>.X): 
...`;
     - each module re-exports what it imports of `pyspark.ml`.
   - The package's stub re-exports its classes and the native stages of 
`pyspark.ml.feature` that it sets.
   - The stub of the vendored `pyspark.ml.connect.feature` declares those 
stages in place of PySpark's classes of the same names, with the module's 
run-time `__all__`.
   - The bases of the 140 classes are those that they have at run time.
   
   **4. `pyspark.sql.context` is PySpark's, vendored as it is.**
   - pyspark-client ships PySpark's classic `pyspark/sql/context.py`. Its 
`SQLContext` and `HiveContext` have all their methods, and its `getOrCreate()` 
returns the `SQLContext` of `pyspark.sql.connect.context`.
   - The drop-in's own module had `getOrCreate()` only:
     - `hasattr(SQLContext, "sql")` was `False`;
     - mypy typed `SQLContext.getOrCreate()` as `Any`, and the attributes of a 
`SQLContext` were errors.
   - The module is now PySpark's file, kept byte-identical to the tag by 
`scripts/check_vendored_upstream.sh`. It imports `StreamingCheckpointManager` 
of `pyspark.sql.streaming.query`, which the drop-in's module now has as 
PySpark's class, outside its `__all__` as in PySpark.
   
   **5. The classic names of `pyspark`.**
   - PySpark's `pyspark/__init__.py` imports these names of `pyspark.core` when 
not `is_remote_only()`, which type checkers do not evaluate:
     - `SparkContext`, `RDD`, `RDDBarrier`, `SparkFiles` and `Broadcast`;
     - `StatusTracker`, `SparkJobInfo`, `SparkStageInfo` and 
`SparkExecutorInfo`;
     - the modules of these, and `SQLContext` and `HiveContext`.
   - pyspark-client does not ship `pyspark.core`. So with it, mypy takes `from 
pyspark import SparkContext` as `Any`, and pyright reports it as an unknown 
import symbol.
   - The drop-in's module had none of these names, so mypy reported `Module 
"pyspark" has no attribute "SparkContext"` for each.
   - It now has the same block, which does not run in either client. The names 
that PySpark's `__all__` lists are re-exported, so mypy and pyright give the 
diagnostics of pyspark-client, `--no-implicit-reexport` included.
   
   **6. `TableArg.to_plan`.** `DROPPED` left the method out of the stub of 
`pyspark.sql.connect.table_arg` as PySpark Connect's plumbing. The drop-in has 
had the method since #799 (SPARK-61897), so `ta.to_plan(client)` was an error. 
The stub declares it now.
   
   **7. The gates of the stub test.**
   - Before, `test_type_stubs_offline.py` checked only the module-level names 
of stubbed modules, so 6 passed it.
   - It now probes the 7,079 public members of the classes of the stubs, with 
mypy and pyright. Two members are allowed, in `UNDECLARED_MEMBERS` with their 
reason: `StreamingQueryManager.close` of `pyspark.sql.streaming.query` and 
`TableArg.to_plan` of `pyspark.sql.table_arg`. Their classes are PySpark 
Connect's at those classic import paths, and type checkers of pyspark-client do 
not see these members either.
   - It checks that no name of `DROPPED`, and no module of 
`DROPPED_STAR_IMPORTS`, exists at the drop-in's run time.
   - It checks the names of the proto stub and the bases of the 
`pyspark.ml.connect` classes against the run time.
   - It runs mypy and pyright on a probe of the classic names.
   - The user's application now reveals a `Plan`.
   
   **8. Docs.**
   - `docs/compatibility.md`, the type-checking section:
     - it no longer claims more than the stubs give, and says that they keep no 
docstrings;
     - the re-export example is corrected: pyright reports `F.partitioning` 
with pyspark-client, and mypy `--no-implicit-reexport` reports 
`F.PandasUDFType`;
     - the `Any` of protobuf parameters and of `SQLContext` is gone;
     - it describes the difference of `pyspark.ml.connect`;
     - it gives the reason why `pyspark.sql.functions` stays a module;
     - it notes the `reportMissingModuleSource` warning of the modules of 
messages.
   - `dev/parity/known-differences.md` matches the section. 
`docs/contributing.md` and `dev/design/ci.md` describe what the generator and 
the test check. `dev/parity/4.3.0-module-audit.md` drops 
`StreamingCheckpointManager`.
   
   `pyspark.sql.functions` stays a module. As a package, what Python reports of 
it at run time (`__file__`, `__path__`, `pkgutil`) would change, and so would 
the way that it binds its functions, for no difference to type checkers but 
pyright's `reportMissingModuleSource` warning. `docs/compatibility.md` says so.
   
   ### Why are the changes needed?
   
   So that mypy and pyright type an application with the drop-in as with 
pyspark-client: its messages of Spark Connect, its Avro and Protobuf functions, 
its ML classes, its SQLContext and its classic names. Before, these were errors 
where pyspark-client reports none, or `Any` where it types them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - Type checkers see the names above as with pyspark-client, apart from the 
documented differences.
   - At run time, `SQLContext` and `HiveContext` are PySpark's classic classes, 
with their methods, and `pyspark.sql.streaming.query` has 
`StreamingCheckpointManager`, as in pyspark-client.
   - `SQLContext.getOrCreate()` with no active session now makes one with 
`SparkSession.builder`, as PySpark's does. So without a URL it raises the 
builder's documented `CONNECT_URL_NOT_SET`, where the drop-in's own code 
connected to `sc://localhost`.
   
   ### How was this patch tested?
   
   - **`python/tests/test_type_stubs_offline.py`**, with mypy 2.4.1 and pyright 
1.1.414: 15 passed, of which 6 are new and 3 extended. On master, all 9 of 
these fail.
     - the members of the classes of the stubs, with mypy and with pyright;
     - the classic names, with mypy and with pyright;
     - the names of `DROPPED`;
     - the proto stub;
     - the bases of the `pyspark.ml.connect` classes;
     - the user's application with its `Plan`, with mypy and with pyright.
   - **`python/tests/test_dropin_offline.py`**: 
`test_classic_sql_context_is_pysparks_class_of_its_methods`, which fails on 
master. The SQLContext tests of the file pass on the vendored module.
   - **`scripts/tests/test_gen_type_stubs.py`**: the star imports that the 
generator drops, and the stubs of `pyspark.ml.connect` from a source tree. 17 
passed.
   - **Probes of both clients:**
     - the diagnostics of mypy (default and `--no-implicit-reexport`) and of 
pyright are the same for the user's application and the classic names;
     - the names of the proto package, the members of `SQLContext` and 
`HiveContext`, and the bases of the 140 `pyspark.ml.connect` classes match the 
run time.
   - **The gates:**
     - `gen_type_stubs.py --check` (54 stubs), `gen_fn_dispatch.py --check`, 
`scripts/tests` (102 run, 2 skipped) and the guards of SPARK-61851;
     - `scripts/check_vendored_upstream.sh` against the `v4.3.0` tag from a 
local clone of apache/spark: `sql/context.py` and the other 167 vendored paths 
match;
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` (165 
vendored paths) and `audit_no_stubs.sh`;
     - the Rust gates: `cargo +1.100.0 fmt --all --check`, `cargo +1.100.0 
clippy -p apache-spark-connect -p apache-spark-connect-core -p pyspark-rs 
--all-targets -- -D warnings`, `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps 
-p apache-spark-connect -p apache-spark-connect-core`, and `cargo test -p 
pyspark-rs --lib` as `rust.yml` runs it (13 passed);
     - the offline suite of `coverage.yml`: 6336 passed and 77 skipped (68 for 
"not a public constructor", 8 that need `SPARK_REMOTE`, and 
`test_type_stubs_offline.py`, which needs mypy and ran above);
     - `test_dropin_e2e.py` against a Spark 4.3.0 server: 319 passed.
   
   ### 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