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

   ### What changes were proposed in this pull request?
   
   A follow-up of #777 (SPARK-61854) and #857 (SPARK-62011, merged as 
5efac904). The type stubs that `scripts/gen_type_stubs.py` generates dropped 
every docstring. So an editor showed the signature of `F.col`, 
`DataFrame.select` or `Column.cast` without PySpark's documentation, which 
pyright's hover shows with pyspark-client: 416, 1097 and 828 characters with 
pyspark-client, against the signatures only with the drop-in.
   
   **1. Each declaration of a stub has the docstring that PySpark's object has 
at run time.**
   - The generator works out, from PySpark's source, the docstring that 
`inspect.getdoc` gives of PySpark's object of each name (the new `Docs`):
     - the docstring of its source;
     - with the `.. versionadded::` that `pyspark.since` appends;
     - or the `__doc__` that its module assigns it, which it follows to the 
object of the right side. These are the copies of PySpark Connect's modules 
(`col.__doc__ = pysparkfuncs.col.__doc__`, `select.__doc__ = 
PySparkDataFrame.select.__doc__`) and a class body's `__doc__ = ...`;
     - or, for a class or a method of no docstring, that of the first class of 
its MRO that has one.
   - It follows `inspect.getdoc` where that departs from the plain rule:
     - the `__doc__` that a class body sets on a `classmethod` or 
`staticmethod` is the wrapper's, not that of the function the class gives;
     - `inspect.getdoc` looks no base up for a cache of `functools`;
     - a `classproperty`'s attribute is the object that its getter returns 
(`SparkSession.builder`).
   - A decorator that the generator does not know to keep a docstring is an 
error (`DOC_KEEPING_DECORATORS`).
   - Docstrings that are not PySpark's are left out:
     - those that Python makes, of a named tuple or a dataclass of none;
     - those that a member inherits from a class of Python (`object.__init__`, 
`threading.Thread.start`).
     Type checkers of pyspark-client do not show these either.
   - The classes of the stubs of `pyspark.ml.connect` get the docstrings of 
PySpark's classes of `pyspark.ml`, which their public classes have, and the 
modules get those of PySpark's modules of their import paths.
   - A docstring is written as a string literal of its text, which 
`inspect.cleandoc` and type checkers read back exactly:
     - a raw literal when it has a backslash, where it can be one;
     - escapes of quotes and of the control characters of PySpark's examples 
(`b'\x01'`);
     - a first line that begins with a space, as in `Normalizer`'s, kept on the 
line after the quotes.
   
   **2. The generation is 7 times faster.** `_verbatim_defaults` read each 
default value's text with `ast.get_source_segment`, which splits the whole of 
PySpark's source again at each call. It now reads it from a line index made 
once per source, of the same lines (`\r\n`, `\r` and `\n`, not a form feed). A 
generation of the stubs takes 2.9 s of CPU without the docstrings, where it 
took 40.9 s, and 5.6 s with them. The stubs that it writes are unchanged by 
this.
   
   The stubs grow from 369 KiB to 3810 KiB, and from 54 KiB to 495 KiB 
deflated. `pyspark/sql/functions/builtin.pyi` and its copy of PySpark Connect 
are 1088 and 1056 KiB of these. A cold mypy run over the user's application of 
the stub test takes 3.6 s either way. A pyright run takes 1.4-1.5 s either way, 
with 42 MB more memory. `--check` stays deterministic, and the stubs stay in 
ruff's excludes.
   
   `docs/compatibility.md` and `dev/parity/known-differences.md` describe the 
one difference that remains. pyright does not see a docstring that PySpark 
Connect's modules copy at run time in PySpark's source, so a hover over 
`pyspark.sql.connect.functions.col` or Spark Connect's `SparkSession.builder` 
shows PySpark's documentation with the drop-in, and none with pyspark-client. 
`docs/contributing.md` and `dev/design/ci.md` describe the golden.
   
   ### Why are the changes needed?
   
   So that editors show PySpark's documentation of the API of the drop-in, as 
they do with pyspark-client.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Type checkers and editors show PySpark's docstrings of the names of the 
stubbed modules, as at run time. Nothing changes at run time.
   
   ### How was this patch tested?
   
   - **New `tests/golden/stub_docstrings.json`**, which 
`scripts/capture_stub_docstrings_golden.py` takes from pyspark-client 4.3.0 
with `inspect.getdoc`. It holds a digest of the docstrings of the declarations 
of each stub, the declarations of no object of PySpark, and the full docstrings 
of 11 samples:
     - `F.col`, `DataFrame.select`, Spark Connect's `SparkSession.builder`, 
`Column.cast` and `pyspark.ml.connect.classification.LogisticRegression`;
     - a Connect copy, `pyspark.since`, a class body's `__doc__`, a 
classmethod's wrapper, a cache of `functools`, and a first line that begins 
with a space.
   - **`test_the_stubs_have_the_docstrings_of_pysparks_objects`** in 
`python/tests/test_type_stubs_offline.py` compares the stubs with the golden. 
It fails on master, where the stubs have no docstrings. The 16 tests of the 
file pass, with mypy 2.4.1 and pyright 1.1.414.
   - **Probes against pyspark-client 4.3.0:**
     - the docstrings of all 2,656 declarations of the stubs that have an 
object in pyspark-client equal its `inspect.getdoc`, under the rule above;
     - the full hover texts of pyright's language server over `F.col`, 
`DataFrame.select`, `Column.cast`, `F.like`, `F.when`, `F.udf`, 
`DataFrame.groupBy`, `DataFrameReader.json` and `StructType.add` are 
byte-identical with pyspark-client;
     - the segments of the line index equal `ast.get_source_segment`'s, and the 
stubs that the generator writes without docstrings are unchanged;
     - `gen_type_stubs.py --check` gives the same stubs with Python 3.10, 3.11, 
3.12 and 3.13.
   - **`scripts/tests/test_gen_type_stubs.py`**: 20 passed, with the docstrings 
of a source tree of a classic module and of a Connect module that copies it, an 
unknown decorator, and the literal of each kind of text.
   - **The gates:**
     - `gen_type_stubs.py --check` (5 s), `gen_fn_dispatch.py --check`, 
`scripts/tests` (109 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 (168 paths);
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and 
`audit_no_stubs.sh`;
     - the Rust gates, of no change of this patch: `cargo +1.100.0 fmt --all 
--check`, clippy 1.100.0 with `-D warnings`, rustdoc with `-D warnings`, and 
`cargo test -p pyspark-rs --lib` as `rust.yml` runs it (13 passed);
     - the offline suite of `coverage.yml`: 6379 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).
     - Nothing changes at run time, so `test_dropin_e2e.py` was not run again.
   
   ### 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