Skip to content

GH-55596: [Python][Annotations] Add runtime docstrings to the type stubs with docstring-adder - #55597

Draft
rok wants to merge 2 commits into
apache:mainfrom
rok:pyarrow-stubs-lib-docstrings
Draft

rok wants to merge 2 commits into
apache:mainfrom
rok:pyarrow-stubs-lib-docstrings

Conversation

@rok

@rok rok commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

Closes #55596.

The runtime docstrings are inserted into the type stubs by a custom libcst codemod (python/scripts/update_stub_docstrings.py), which needs a hand-written mapping from stub files to runtime modules and makes libcst a build requirement of every source build. docstring-adder does the same job generically.

Stacked on #55264; only the last two commits belong to this PR.

What changes are included in this PR?

  • Squash the stub-only pyarrow.__lib package into lib.pyi, one section per .pxi file, matching the single pyarrow.lib extension module. This drops the per-file __all__ lists and the module aliasing in run_stubtest.py, and lets docstring-adder map lib.pyi to pyarrow.lib by name.
  • update_stub_docstrings.py now runs docstring-adder on the installed stubs (it still assembles an importable pyarrow from the source and install trees) and fails if lib.pyi got no docstrings.
  • Build Cython with embedsignature.format=clinic, so the embedded signature goes to __text_signature__ instead of the first lines of __doc__ and out of the stub docstrings.
  • Install the stubs into wheels again and add the docstrings in the wheel builds (PYARROW_REQUIRE_STUB_DOCSTRINGS=ON). py.typed stays excluded from wheels until the stubs are complete ([Python] type checkers broken for pyarrow.compute and other sub-modules since PyArrow 24.0+ #49831).
  • The wheel build scripts check the stubs in each built wheel against the modules in the same wheel with stubtest (run_stubtest.py --wheel, no install needed). The wheel builds don't have the pandas and scipy stubs, so mypy treats those modules as Any when their stubs are missing.
  • libcst is no longer a build requirement. docstring-adder isn't on PyPI, so requirements-wheel-build.txt pins it to a commit. The macOS Python 3.15 workaround for pyyaml (pulled in by libcst) is removed.

Are these changes tested?

Locally with a minimal Arrow C++ build and python -m build --wheel --no-isolation with PYARROW_REQUIRE_STUB_DOCSTRINGS=ON:

  • The wheel contains the stubs; 631 of the 709 classes and functions in lib.pyi have docstrings (the rest have none at runtime, e.g. exception classes and Cython properties), and none starts with a signature line.
  • stubtest, the stub tests, mypy, pyright and ty pass.
  • run_stubtest.py --wheel passes on the wheel in an environment with only the wheel build requirements, and fails on a wheel with an extra stub function and a renamed parameter.
  • inspect.signature of the pyarrow.lib functions and methods is unchanged by the clinic format.

Are there any user-facing changes?

Wheels ship the type stubs with docstrings again (without py.typed, so mypy keeps ignoring them). The __doc__ of Cython functions no longer starts with the signature; help() and inspect.signature() still show it.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…al types

Squash of apache#55264 (97a1f74) on top
of main, to stack the stub docstring changes on it. Not for merging.
@rok
rok force-pushed the pyarrow-stubs-lib-docstrings branch 2 times, most recently from 8c663f0 to 59936e6 Compare November 13, 2026 16:37
@rok

rok commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g wheel

@github-actions

Copy link
Copy Markdown

Revision: 59936e6

Submitted crossbow builds: ursacomputing/crossbow @ actions-087799c9ee

Task Status
python-sdist GitHub Actions
wheel-macos-monterey-cp311-cp311-amd64 GitHub Actions
wheel-macos-monterey-cp311-cp311-arm64 GitHub Actions
wheel-macos-monterey-cp312-cp312-amd64 GitHub Actions
wheel-macos-monterey-cp312-cp312-arm64 GitHub Actions
wheel-macos-monterey-cp313-cp313-amd64 GitHub Actions
wheel-macos-monterey-cp313-cp313-arm64 GitHub Actions
wheel-macos-monterey-cp314-cp314-amd64 GitHub Actions
wheel-macos-monterey-cp314-cp314-arm64 GitHub Actions
wheel-macos-monterey-cp314-cp314t-amd64 GitHub Actions
wheel-macos-monterey-cp314-cp314t-arm64 GitHub Actions
wheel-macos-monterey-cp315-cp315-amd64 GitHub Actions
wheel-macos-monterey-cp315-cp315-arm64 GitHub Actions
wheel-macos-monterey-cp315-cp315t-amd64 GitHub Actions
wheel-macos-monterey-cp315-cp315t-arm64 GitHub Actions
wheel-manylinux-2-28-cp311-cp311-amd64 GitHub Actions
wheel-manylinux-2-28-cp311-cp311-arm64 GitHub Actions
wheel-manylinux-2-28-cp312-cp312-amd64 GitHub Actions
wheel-manylinux-2-28-cp312-cp312-arm64 GitHub Actions
wheel-manylinux-2-28-cp313-cp313-amd64 GitHub Actions
wheel-manylinux-2-28-cp313-cp313-arm64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314-amd64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314-arm64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314t-amd64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314t-arm64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315-amd64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315-arm64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315t-amd64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315t-arm64 GitHub Actions
wheel-musllinux-1-2-cp311-cp311-amd64 GitHub Actions
wheel-musllinux-1-2-cp311-cp311-arm64 GitHub Actions
wheel-musllinux-1-2-cp312-cp312-amd64 GitHub Actions
wheel-musllinux-1-2-cp312-cp312-arm64 GitHub Actions
wheel-musllinux-1-2-cp313-cp313-amd64 GitHub Actions
wheel-musllinux-1-2-cp313-cp313-arm64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314-amd64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314-arm64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314t-amd64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314t-arm64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315-amd64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315-arm64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315t-amd64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315t-arm64 GitHub Actions
wheel-windows-cp311-cp311-amd64 GitHub Actions
wheel-windows-cp312-cp312-amd64 GitHub Actions
wheel-windows-cp313-cp313-amd64 GitHub Actions
wheel-windows-cp314-cp314-amd64 GitHub Actions
wheel-windows-cp314-cp314t-amd64 GitHub Actions
wheel-windows-cp315-cp315-amd64 GitHub Actions
wheel-windows-cp315-cp315t-amd64 GitHub Actions

@rok
rok force-pushed the pyarrow-stubs-lib-docstrings branch from 59936e6 to b44e1d7 Compare November 13, 2026 16:58
@rok

rok commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g wheel

@github-actions

Copy link
Copy Markdown

Revision: b44e1d7

Submitted crossbow builds: ursacomputing/crossbow @ actions-24014aafd7

Task Status
python-sdist GitHub Actions
wheel-macos-monterey-cp311-cp311-amd64 GitHub Actions
wheel-macos-monterey-cp311-cp311-arm64 GitHub Actions
wheel-macos-monterey-cp312-cp312-amd64 GitHub Actions
wheel-macos-monterey-cp312-cp312-arm64 GitHub Actions
wheel-macos-monterey-cp313-cp313-amd64 GitHub Actions
wheel-macos-monterey-cp313-cp313-arm64 GitHub Actions
wheel-macos-monterey-cp314-cp314-amd64 GitHub Actions
wheel-macos-monterey-cp314-cp314-arm64 GitHub Actions
wheel-macos-monterey-cp314-cp314t-amd64 GitHub Actions
wheel-macos-monterey-cp314-cp314t-arm64 GitHub Actions
wheel-macos-monterey-cp315-cp315-amd64 GitHub Actions
wheel-macos-monterey-cp315-cp315-arm64 GitHub Actions
wheel-macos-monterey-cp315-cp315t-amd64 GitHub Actions
wheel-macos-monterey-cp315-cp315t-arm64 GitHub Actions
wheel-manylinux-2-28-cp311-cp311-amd64 GitHub Actions
wheel-manylinux-2-28-cp311-cp311-arm64 GitHub Actions
wheel-manylinux-2-28-cp312-cp312-amd64 GitHub Actions
wheel-manylinux-2-28-cp312-cp312-arm64 GitHub Actions
wheel-manylinux-2-28-cp313-cp313-amd64 GitHub Actions
wheel-manylinux-2-28-cp313-cp313-arm64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314-amd64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314-arm64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314t-amd64 GitHub Actions
wheel-manylinux-2-28-cp314-cp314t-arm64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315-amd64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315-arm64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315t-amd64 GitHub Actions
wheel-manylinux-2-28-cp315-cp315t-arm64 GitHub Actions
wheel-musllinux-1-2-cp311-cp311-amd64 GitHub Actions
wheel-musllinux-1-2-cp311-cp311-arm64 GitHub Actions
wheel-musllinux-1-2-cp312-cp312-amd64 GitHub Actions
wheel-musllinux-1-2-cp312-cp312-arm64 GitHub Actions
wheel-musllinux-1-2-cp313-cp313-amd64 GitHub Actions
wheel-musllinux-1-2-cp313-cp313-arm64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314-amd64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314-arm64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314t-amd64 GitHub Actions
wheel-musllinux-1-2-cp314-cp314t-arm64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315-amd64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315-arm64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315t-amd64 GitHub Actions
wheel-musllinux-1-2-cp315-cp315t-arm64 GitHub Actions
wheel-windows-cp311-cp311-amd64 GitHub Actions
wheel-windows-cp312-cp312-amd64 GitHub Actions
wheel-windows-cp313-cp313-amd64 GitHub Actions
wheel-windows-cp314-cp314-amd64 GitHub Actions
wheel-windows-cp314-cp314t-amd64 GitHub Actions
wheel-windows-cp315-cp315-amd64 GitHub Actions
wheel-windows-cp315-cp315t-amd64 GitHub Actions

…ype stubs with docstring-adder

Squash the stub-only pyarrow.__lib package into lib.pyi, with a section
per .pxi file, since pyarrow.lib is a single extension module built from
the .pxi files it includes. The per-file __all__ lists go away, and so
does the aliasing of pyarrow.__lib modules to pyarrow.lib in
run_stubtest.py.

Replace the libcst codemod in update_stub_docstrings.py with
docstring-adder (https://github.com/astral-sh/docstring-adder). The script
still assembles an importable pyarrow from the source and install trees,
then runs docstring-adder on the installed stubs and checks that lib.pyi
got docstrings.

Build the Cython modules with embedsignature.format=clinic, so that the
embedded signatures go to __text_signature__ instead of the first lines of
__doc__ and don't end up in the docstrings of the stubs.

Install the stubs into wheels again and add the docstrings in the wheel
builds (PYARROW_REQUIRE_STUB_DOCSTRINGS=ON). py.typed stays excluded from
wheels until the stubs are complete (apacheGH-49831).

Check the stubs in each built wheel against the modules in the same wheel
with stubtest (run_stubtest.py --wheel) in the wheel build scripts. The
wheel builds don't install the pandas and scipy stubs, so mypy treats
those modules as Any when their stubs are missing.

The wheel content validation checks again that the wheels contain the
stubs of pyarrow-stubs/ with docstrings, and still that they don't
contain py.typed.

libcst is no longer a build requirement. docstring-adder isn't on PyPI, so
requirements-wheel-build.txt pins it to a commit. Bump the Windows
wheel image revision, so that the image installs the new requirements.
@rok
rok force-pushed the pyarrow-stubs-lib-docstrings branch from b44e1d7 to f5af72b Compare November 13, 2026 19:24
@rok

rok commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit wheel-windows-* wheel-macos-monterey-cp315-cp315-amd64

@github-actions

Copy link
Copy Markdown

Revision: f5af72b

Submitted crossbow builds: ursacomputing/crossbow @ actions-48add857b9

Task Status
wheel-macos-monterey-cp315-cp315-amd64 GitHub Actions
wheel-windows-cp311-cp311-amd64 GitHub Actions
wheel-windows-cp312-cp312-amd64 GitHub Actions
wheel-windows-cp313-cp313-amd64 GitHub Actions
wheel-windows-cp314-cp314-amd64 GitHub Actions
wheel-windows-cp314-cp314t-amd64 GitHub Actions
wheel-windows-cp315-cp315-amd64 GitHub Actions
wheel-windows-cp315-cp315t-amd64 GitHub Actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Use docstring-adder to add runtime docstrings to type stubs

1 participant