Add RC_2_0 Python type stubs
Fixes #7996
This PR cherry-picks the Python typing commits from master and fixes them up. There intent here is to NOT make any API changes, only to introduce typing. The first four commits are cherry-picked and only the second one (
965d9f2) is rewritten.
- One GitHub Actions job is added:
check_wheel_type_exposureenforces both exposed type presence and type consistency. - One entry is added to
.pre-commit-config.yaml:flake8-pyi-runenforces modern Python syntax in the stubs. - I updated some Python files in
bindings/python/to verify the typing (it is impossible to properly test types using a file that does not use types itself). - One Python TestCase is added to somewhat match function stub signatures to their actual bindings.
@qstokkink Why not use https://github.com/PyCQA/flake8-pyi (25.5.0) in pre-commit?
@xavier2k6 Because I presumed that only works if the repo has a .pre-commit-hooks.yaml file. I'm not a pre-commit poweruser by any means, though.
Can you share a .pre-commit-config.yaml excerpt/configuration (that works)?
I marked this as "ready". Not because there is nothing left to change, but more because I'm already roughly 5k lines above what I think the maximum PR size should be (though most of this is in the 5,252 lines of the Python type stubs).
you're making a lot of changes to the actual bindings themselves. I don't think that's reasonable, it really risks breaking compatibility. Is there a good reason to not just add type stubs for the existing bindings?
@arvidn Changed. I ripped all of the non-essential stuff out now.
[EDIT] If you care about the reason: I figured that pulling in as much of the non-impactful changes as possible would make it easier to merge back from RC_2_0 to master. That would make the typing-related things my problem in this PR, instead of the problem of whoever does that backmerge.
[EDIT 2] I can do that backmerge into master, if you want me to.
hi, any update on this? i'd like to see libtorrent with stubs :D
@stabldev You can download and play with the wheels of this PR (here: https://github.com/arvidn/libtorrent/actions/runs/17020524696?pr=8007). Feedback is welcome. It would be nice to have some "testimonials" that the typing is correct and useful.