libtorrent icon indicating copy to clipboard operation
libtorrent copied to clipboard

Add RC_2_0 Python type stubs

Open qstokkink opened this issue 4 months ago • 7 comments

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_exposure enforces both exposed type presence and type consistency.
  • One entry is added to .pre-commit-config.yaml: flake8-pyi-run enforces 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 avatar Aug 06 '25 12:08 qstokkink

@qstokkink Why not use https://github.com/PyCQA/flake8-pyi (25.5.0) in pre-commit?

xavier2k6 avatar Aug 11 '25 11:08 xavier2k6

@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)?

qstokkink avatar Aug 11 '25 13:08 qstokkink

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).

qstokkink avatar Aug 12 '25 08:08 qstokkink

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 avatar Aug 16 '25 08:08 arvidn

@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.

qstokkink avatar Aug 17 '25 14:08 qstokkink

hi, any update on this? i'd like to see libtorrent with stubs :D

stabldev avatar Nov 08 '25 02:11 stabldev

@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.

qstokkink avatar Nov 11 '25 08:11 qstokkink