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

   ### What changes were proposed in this pull request?
   
   Follow-ups of #777 (SPARK-61854), one commit each, so that the type stubs 
declare what the drop-in's modules have at run time:
   
   #### 1. Declare the private functions and constants of PySpark's modules in 
the type stubs when the drop-in's modules have them
   
   The type stubs (`scripts/gen_type_stubs.py`) kept a private module-level 
function, class or constant of PySpark's module only when the stub referred to 
it. So 54 private names that both the official `pyspark-client` and the drop-in 
have at run time were missing for type checkers, such as 
`pyspark.sql.types._parse_datatype_string`, `_parse_datatype_json_string`, 
`_infer_schema` and `_merge_type`, which applications and libraries use. A stub 
also referred to some of them in its own imports (`from pyspark.sql.types 
import _parse_datatype_string` in `udf.pyi`), which then resolved to nothing.
   
   A private declaration of PySpark's module is now kept when the source of the 
drop-in's module (its `.py`, not its stub) binds the name at its top level, so 
the stubs declare what the drop-in has at run time, with PySpark's annotations:
   
   - `DropIn.runtime_names` reads the names of the drop-in's module from its 
source;
   - `prune` keeps the private declarations of PySpark's module among them, 
with what they refer to.
   
   The stubs of `pyspark.sql.types`, `udf`, `udtf`, `connect.udf`, 
`connect.functions.builtin` and `pyspark.util` gain 55 private declarations. Of 
the private names that both clients have at run time, the stubs now miss only 
`pyspark.sql.types._typecode`, the variable that a loop of PySpark's module 
leaves behind.
   
   #### 2. Keep PySpark's type ignore comments and the import of the annotation 
of __all__ in the type stubs, and check that their imports resolve in the 
drop-in
   
   Stacked on `stubs-f2-private-names`: this PR is its last commit, to merge 
after it.
   
   Three robustness fixes of `scripts/gen_type_stubs.py`:
   
   - The reference pass returned on `__all__` before it read its annotation, so 
`pyspark/util.pyi` wrote `__all__: List[str] = []` without importing `List` 
(mypy: `Name "List" is not defined` in the stub). It reads the annotation now.
   - `ast.unparse` drops comments, so the stubs lost PySpark's `# type: 
ignore[...]`, such as `[override]` on `Column.__eq__`, and mypy reported those 
errors in the stubs. A stub writes the comment of each decorator, signature, 
class header and assignment on the line of its statement, the comments of the 
lines of a multi-line signature merged.
   - A stub imported names that the drop-in does not have, which made PySpark's 
annotations of them `Any` without a word. `check_imports` fails on a name that 
a stub imports at its top level from a module of pyspark and that the drop-in's 
module does not have, unless `UNRESOLVED_IMPORTS` lists it with its reason: 
`pyspark.core.*` (classic PySpark's, which `pyspark-client` does not ship 
either), the `StringConcat` of the private tree strings of the classic types, 
and nine internal classes of PySpark Connect's plans and expressions, which the 
drop-in builds in Rust. An entry that resolves is an error too.
   
   #### 3. Declare in the type stubs only the names that the drop-in has at run 
time, and check it
   
   Stacked on `stubs-f4-generator-checks`: this PR is its last commit, to merge 
after it.
   
   A stub replaces its module for a type checker, so a name that only the stub 
declares type-checks and then fails at run time: `from pyspark.sql.types import 
DateConverter`, `from pyspark.sql.catalog import DataFrame` or `from 
pyspark.sql.readwriter import PathOrPaths` passed mypy and raised 
`ImportError`. The stubs of master declared 244 public names that the drop-in 
does not have. A gate and a decision per name:
   
   - **The gate**, `test_every_name_of_a_stub_exists_at_run_time` of 
`python/tests/test_type_stubs_offline.py`: each public name of the module of a 
stub, and each member of its classes (not the declarations of the attributes of 
instances), exists at the drop-in's run time, as an attribute or as an 
attribute of the instances that the class's methods assign; but those of 
`TYPE_ONLY`, with their reasons, which must still be missing.
   - **Imports of PySpark's modules (219 names)**, such as `DataFrame` of 
`pyspark.sql.catalog` or `DataFrameWriter` of `pyspark.sql.dataframe`: a stub 
re-exported a name that PySpark's module imports whenever the module it comes 
from has it. It re-exports it now when the drop-in's module of the stub has it 
too, as its source binds it (`DropIn.runtime_complete`). The incidental imports 
of PySpark's modules are not their API, and the drop-in's modules do not import 
them.
   - **Given to the drop-in**: `PathOrPaths` and `TupleOrListOfString` of 
`pyspark.sql.readwriter` and `pyspark.sql.connect.readwriter`, PySpark's 
aliases of the types of the paths and the column names, which an application 
can annotate its code with.
   - **Left out of the stubs** (`DROPPED` of the generator), plumbing that no 
annotation needs: the seven py4j converters of `pyspark.sql.types`, the classic 
`StreamingCheckpointManager` (not in PySpark's `__all__`), `TableArg.to_plan` 
of PySpark Connect, and what the modules of functions that copy their functions 
do not import (`Column` of `functions.partitioning`, `Expression` of 
`connect.functions.builtin`, ...).
   - **Kept for PySpark's annotations** (`TYPE_ONLY`): `DataFrame.rdd`, which 
PySpark defines when not `is_remote_only()`, so type checkers of 
`pyspark-client` see it and neither client has it; the decorators `dfapi` and 
`df_varargs_api`, which type `GroupedData`'s methods as PySpark's; the bases 
`PandasGroupedOpsMixin` (of GroupedData's pandas and Arrow methods) and 
`OptionUtils` (of the readers and writers, of private members only), and the 
metaclass `DataTypeSingleton`, of PySpark's classes that the drop-in implements 
natively; the type variables `T` and `U` of `pyspark.sql.types`; and PySpark 
Connect's `WindowFrame` and `StreamingQueryListenerBus`, which annotate the 
constructor of its `WindowSpec` and the private `_sqlb` of its 
`StreamingQueryManager`.
   
   The generator also takes a module for dynamic only when it calls `globals()` 
at its top level, not in a function (`pyspark/sql/types.py`).
   
   #### 4. Add the type stubs of pyspark.sql.connect.datasource and 
pyspark.ml.stat, whose classes are native
   
   Stacked on `stubs-f1-runtime-names`: this PR is its last commit, to merge 
after it.
   
   Two modules of the drop-in re-export native classes and had no stub, so type 
checkers saw `Any` for them: `pyspark.sql.connect.datasource`, of 
`DataSourceRegistration` (`spark.dataSource` of a Connect session), and 
`pyspark.ml.stat`, of `ChiSquareTest`, `Correlation`, `KolmogorovSmirnovTest`, 
`Summarizer` and the `SummaryBuilder` of `Summarizer.metrics`.
   
   - Both are in `STUB_MODULES` of `scripts/gen_type_stubs.py`, which generates 
their stubs from PySpark 4.3.0's modules.
   - `pyspark.since`, the factory of decorators of the methods of `Summarizer`, 
is in `IDENTITY_DECORATORS`: `check_identity_decorators` takes a decorator 
annotated `(...) -> Callable[[T], T]` too, as `since` is.
   
   The other modules that import from `pyspark._pyspark` need no stub: the 
drop-in's own `pyspark.sql.connect.client` and private modules (its proto 
runtime, `_column_reduce`, `_pandas_input`, ...); PySpark's modules with native 
parts swapped in, which keep PySpark's annotations (`pyspark.resource.profile`, 
whose types mypy reveals as with `pyspark-client`, `pyspark.sql.connect.types`, 
`sql_formatter`, `tvf_argument`, the pipelines' graph registry); and 
`pyspark.ml.connect`, whose native classes follow `pyspark.ml`, not PySpark's 
deprecated `pyspark.ml.connect`, which a stub of PySpark's module would 
describe.
   
   ### Why are the changes needed?
   
   **1.** With `pyspark-client`, mypy and pyright type `from pyspark.sql.types 
import _parse_datatype_string`; with the drop-in, they reported `Module 
"pyspark.sql.types" has no attribute "_parse_datatype_string"` for code that 
runs on both.
   
   **2.** So that the stubs are what PySpark's modules declare, with PySpark's 
checks of them, and so that an import that resolves to nothing is a decision 
rather than an accident. mypy's errors in the generated stubs, with site 
packages unsilenced, go from 91 to 82: the 7 `override` and the `assignment` 
that PySpark's own comments cover, and the `name-defined` of `List`. The rest 
are of PySpark's typing or of the internal classes above.
   
   **3.** So that code that type-checks with the drop-in does not fail with 
`ImportError` or `AttributeError` at run time because a stub declares a name 
that the drop-in does not have.
   
   **4.** With `pyspark-client`, mypy and pyright type 
`spark.dataSource.register(...)`, `Correlation.corr(df, "features")` and 
`Summarizer.metrics("mean").summary(col)`. With the drop-in, they were `Any`, 
so `Correlation.corr(df, 1)` and `spark.dataSource.register(1)` were not 
reported.
   
   ### Does this PR introduce _any_ user-facing change?
   
   **1.** Yes, for type checkers: the private names above are typed as with 
`pyspark-client`. The run time is unchanged.
   
   **2.** No: type checkers do not report the errors of an installed package, 
and the types are unchanged.
   
   **3.** Yes. For type checkers, the 219 imported names and the dropped 
plumbing above are errors, as they fail at run time; mypy took the imported 
names by default with `pyspark-client`, where they are attributes of PySpark's 
modules (pyright reports them as private imports there too). `PathOrPaths` and 
`TupleOrListOfString` exist at run time now. The types of the API are 
unchanged: seven applications type exactly as before under mypy 2.4.1 and 
pyright 1.1.414, and the 562 functions as with `pyspark-client`.
   
   **4.** Yes, for type checkers: these APIs are typed as with 
`pyspark-client`. The run time is unchanged.
   
   ### How was this patch tested?
   
   **1.** - `scripts/tests/test_gen_type_stubs.py`: a private declaration is 
kept when the drop-in's module binds it at run time, and dropped otherwise.
   - `python/tests/test_type_stubs_offline.py`: mypy resolves ten private names 
that libraries use.
   - mypy 2.4.1 and pyright 1.1.414 on seven applications against the official 
client: the only differences left are the intended `Trigger` and the class of 
`SparkSession.client`.
   - `gen_type_stubs.py --check` on Python 3.9 to 3.13.
   - The gates of the branch: `gen_type_stubs.py --check` and 
`gen_fn_dispatch.py --check`, `scripts/tests`, `test_type_stubs_offline.py`, 
ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py`, 
`audit_no_stubs.sh` and the three guard steps of SPARK-61851 pass; the offline 
suite of `coverage.yml` passes 6164 tests, and its 2 failures 
(`test_session_hooks_see_deep_plans_as_pyspark[300]`, 
`test_every_native_class_has_its_bare_name_as_its_c_name`) fail the same way on 
master with the same local build of the extension.
   
   **2.** - `scripts/tests/test_gen_type_stubs.py`: the import of the 
annotation of `__all__`, the comments of a class header, a decorator, a 
multi-line signature and an assignment, and an unresolved, an allowed and a 
stale import.
   - `gen_type_stubs.py --check` on Python 3.9 to 3.13, and 
`test_type_stubs_offline.py`.
   - The gates of the branch: `gen_type_stubs.py --check` and 
`gen_fn_dispatch.py --check`, `scripts/tests`, `test_type_stubs_offline.py`, 
ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py`, 
`audit_no_stubs.sh` and the three guard steps of SPARK-61851 pass; the offline 
suite of `coverage.yml` passes 6164 tests, and its 2 failures 
(`test_session_hooks_see_deep_plans_as_pyspark[300]`, 
`test_every_native_class_has_its_bare_name_as_its_c_name`) fail the same way on 
master with the same local build of the extension.
   
   **3.** - The new gate, which fails on master with 232 names, besides the 12 
of `TYPE_ONLY`.
   - `scripts/tests/test_gen_type_stubs.py`: a re-export of a name that the 
drop-in's module binds, not of one it does not, a module that calls `globals()` 
in a function, and `DROPPED` (a class, a member, a name that PySpark's module 
lacks).
   - `gen_type_stubs.py --check` on Python 3.9 to 3.13, and the offline suite 
of `coverage.yml`.
   - The gates of the branch: `gen_type_stubs.py --check` and 
`gen_fn_dispatch.py --check`, `scripts/tests`, `test_type_stubs_offline.py`, 
ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py`, 
`audit_no_stubs.sh` and the three guard steps of SPARK-61851 pass; the offline 
suite of `coverage.yml` passes 6165 tests, and its 2 failures 
(`test_session_hooks_see_deep_plans_as_pyspark[300]`, 
`test_every_native_class_has_its_bare_name_as_its_c_name`) fail the same way on 
master with the same local build of the extension.
   
   **4.** - mypy 2.4.1 and pyright 1.1.414 on an application of these APIs: the 
same revealed types and errors as with `pyspark-client` 4.3.0, where master 
revealed `Any` and missed two errors.
   - `test_type_stubs_offline.py`: the new stubs declare every public name of 
their modules, every name that they declare exists at run time, and mypy types 
their instance attributes.
   - `scripts/tests/test_gen_type_stubs.py`: a factory of identity decorators, 
and one that is not.
   - `gen_type_stubs.py --check` on Python 3.9 to 3.13.
   - The gates of the branch: `gen_type_stubs.py --check` and 
`gen_fn_dispatch.py --check`, `scripts/tests`, `test_type_stubs_offline.py`, 
ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py`, 
`audit_no_stubs.sh` and the three guard steps of SPARK-61851 pass; the offline 
suite of `coverage.yml` passes 6165 tests, and its 2 failures 
(`test_session_hooks_see_deep_plans_as_pyspark[300]`, 
`test_every_native_class_has_its_bare_name_as_its_c_name`) fail the same way on 
master with the same local build of the extension.
   
   ### 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