Uh oh!
There was an error while loading. Please reload this page.
fix(types): add typing and collections.abc module prefix - #5663
Conversation
These type hints are invalid in Python 3.8. Add `typing.` prefix to remove ambiguity.
gentlegiantJGC
commented
May 14, 2025
The tests need updating to reflect these changes. |
henryiii
commented
May 14, 2025
@InvincibleRMC, thoughts? |
I think this is a good idea. This was discussed a little in and around #5566 (comment). Adding the module prefix will allow users to create their own |
rwgk
left a comment
There was a problem hiding this comment.
Looks good to me, but do we actually still want to support Python 3.8?
Python 3.8 EOL was 2024-10-07
| // 3.9 Compatibility | ||
| #if 0x03090000 <= PY_VERSION_HEX | ||
| # define PYBIND11_ITERABLE_TYPE_HINT "collections.abc.Iterable" |
There was a problem hiding this comment.
Suggestion to make these
PYBIND11_TYPE_HINT_...
so they line up nicely and are easier to pin-point.
| # define PYBIND11_SUBINTERPRETER_SUPPORT | ||
| #endif | ||
| // 3.9 Compatibility |
There was a problem hiding this comment.
// Python 3.9+ Compatibility
?
(The + is the most important aspect of my suggestion.)
gentlegiantJGC
commented
May 14, 2025
You still have test runners for 3.8 so I assumed you still supported it. |
rwgk
commented
May 14, 2025
Me: yes But I'll go with @henryiii's judgement. |
henryiii
commented
May 14, 2025
Let's keep it for 3.0. We can drop it for 3.1. |
henryiii
commented
May 15, 2025
It's in setup.cfg (or pyproject.toml after #5598) |
gentlegiantJGC
commented
May 15, 2025
I think I am going to scale back this pull request to just adding the module prefix. |
Fixing this issue in Python 3.8 will require updating lots of tests. This can be added in a further pull request.
gentlegiantJGC
commented
May 15, 2025
I have fixed the tests. |
henryiii
commented
May 15, 2025
If you want it to go into 3.0, 3.8 needs to work (where "work" is very loosely defined as "not be worse that it is now"). If you are fine to wait till after 3.0 (like a week), then no need to worry about 3.8. I'd personally go for the latter. |
gentlegiantJGC
commented
May 16, 2025
This can wait for 3.1. I have removed the 3.8 compatibility macros and moved the strings inline. |
gentlegiantJGC
commented
May 16, 2025
I don't know why that test is failing |
rwgk
commented
May 17, 2025
All tests pass. @gentlegiantJGC do you want to merge this now (looks good to me)? Or wait until after the v3.0.0 release? |
gentlegiantJGC
commented
May 17, 2025
This can't be merged yet because it changes the following which are incompatible with Python 3.8 I decided not to support 3.8 because it would make the codebase more complex for something which is getting removed which felt pointless. |
rwgk
commented
May 17, 2025
Sounds good. I'm just curious: What exactly is incompatible? Certain type checkers when running under 3.8? We're not testing that here? Could/should we? |
henryiii
commented
May 18, 2025
Aren't these strings? Those are valid at type-check time, just not turning them into non-string annotations at runtime. I think that's not terrible, though? We might even have examples elsewhere? Our annotations are very much not runnable at runtime already. |
gentlegiantJGC
commented
May 18, 2025
You are correct that this changes nothing about the runtime behaviour. The |
gentlegiantJGC
commented
May 18, 2025
It probably wouldn't hurt. You can use pybind11-stubgen to generate stubs for your test suite and then run mypy on the result. It would catch issues like this in the future. |
henryiii
commented
May 19, 2025
Type checkers shouldn't care if a Generally for type hints, if new syntax ( |
gentlegiantJGC
commented
May 19, 2025
Upon further research it seems that mypy has backported class subcription and collection.abc classes to 3.7 so I see no issue merging this now. I assumed they would use the functionality of that version. I still think it is worth validating the generated type hints. It would have caught issues like #5662 which did raise errors in mypy in older versions. # type_check.pyifrom __future__ importannotationsfromcollections.abcimportBufferdefreturn_buffer() ->Buffer: ... |
timohl
commented
May 19, 2025
Good change (also thanks for the
If using the Looking over the current state of type hints:
In my opinion, pybind v3 should support correct type hints for 3.9+. For 3.9+ type hints, we could also think about replacing |
rwgk
commented
May 19, 2025
@gentlegiantJGC@InvincibleRMC@timohl FYI mostly:
A long while ago I started some discussion with mypy developers, related to #4888. This went stale. I believe it'd be awesome to integrate some level of testing with mypy in the pybind11 CI, maybe using some of the work I did under #4888. I won't be able to drive this myself, but I'd be happy to chime in and review. |
@rwgk Would something like what mypy has be fine, where they check generated files against a test file? Or were you thinking about some other approach? https://github.com/python/mypy/tree/master/test-data/pybind11_fixtures |
rwgk
commented
May 20, 2025
I'm totally not sure. Some thoughts: What I did under #4888 was to add I'm not so convinced about the value of tests/test_cases_for_stubgen.py, because it only makes assertions on automatically generated artifacts. It's only good for keeping track in an obvious way (git history) what changed when. I believe ideally we'd run some type checker (e.g. mypy) in the pybind11 CI, to ensure pybind11 type hints are compatible. Maybe we need CI jobs with multiple mypy versions? |
timohl
commented
May 20, 2025
I have created a small demo how pybind11-stubgen and mypy could be called from pytest: #5678 |
Description
Add
typing.andcollections.abcprefix to remove ambiguity.Suggested changelog entry:
Fix
typingandcollections.abctype hint ambiguity.