Skip to content

Add stub file for static type checkers - #389

Open
dlugoschvincent wants to merge 3 commits into
capnproto:masterfrom
dlugoschvincent:master
Open

Add stub file for static type checkers#389
dlugoschvincent wants to merge 3 commits into
capnproto:masterfrom
dlugoschvincent:master

Conversation

@dlugoschvincent

@dlugoschvincent dlugoschvincent commented Nov 8, 2025

Copy link
Copy Markdown

Summary

This PR adds first-party PEP 561 type stubs for pycapnp.

It includes stubs for the root capnp package, the Cython extension module, capnp.types, and version information. The py.typed marker and packaging configuration ensure the stubs are included in wheels and source distributions.

Approach

The declarations were audited against the compiled runtime and Cython implementation. The stubs describe existing objects without introducing typing-only classes, generic APIs, inheritance relationships, or modules.

Notable details include:

  • capnp.AsyncIoStream correctly represents the public alias of the internal runtime class.
  • capnp.types exposes the real built-in schema descriptors.
  • Runtime enum and module-like objects retain their actual structure.
  • The base which() return type remains schema-agnostic; generated schema stubs can refine it using Literal[...].
  • Cython docstrings are included for editor hover documentation.

Validation

The exported names, inheritance, aliases, signatures, and docstrings were checked against runtime objects. The stubs also pass Ruff, Basedpyright, and the pycapnp test suite.

@dlugoschvincent dlugoschvincent changed the title feat: add stub file for static type checkers Add stub file for static type checkers Nov 8, 2025
@dlugoschvincent
dlugoschvincent marked this pull request as draft November 8, 2025 06:15
@dlugoschvincent

Copy link
Copy Markdown
Author

Changing to draft as its being finalized.

@dlugoschvincent
dlugoschvincent marked this pull request as ready for review November 8, 2025 06:28
@dlugoschvincent
dlugoschvincent marked this pull request as draft November 8, 2025 06:34
@dlugoschvincent
dlugoschvincent force-pushed the master branch 2 times, most recently from d213f07 to 662ddeb Compare November 8, 2025 18:42
@haata
haata marked this pull request as ready for review July 3, 2026 07:10
@haata

haata commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

@dlugoschvincent sorry this took me so long to look at. Do you think this is ready now?

@dlugoschvincent

Copy link
Copy Markdown
Author

I think there is still a few issues. I will try to work them out in the coming days and align with my current local version!

build: ship PEP 561 typing metadata
@dlugoschvincent

Copy link
Copy Markdown
Author

Alright @haata I think its ready to merge I removed some complexity. Matched the docstrings to the cython functions and improved accuracy across the board. I also added types for the latest additions to the project.

@haata

haata commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

One question, is there a good way to identify (or have some sort of unit test) to make sure when new APIs are added or changed that these hints are also updated? Even better if it was some sort of compilation error.

@dlugoschvincent

Copy link
Copy Markdown
Author

Hmm I could add a Test case for that if you want. What do you think?

@dlugoschvincent

Copy link
Copy Markdown
Author

Okay I added a separate test using tox -e stubtest that test if stubs exist and if they are plausible for not explicitly ignored symbols or files.

@dlugoschvincent

Copy link
Copy Markdown
Author

I hope I fixed the issues with the build failures can you rerun the workflow?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants