Skip to content

Add typing and collections.abc module prefix #5663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

gentlegiantJGC
Copy link
Contributor

@gentlegiantJGC gentlegiantJGC commented May 14, 2025

Description

Add typing. and collections.abc prefix to remove ambiguity.

Suggested changelog entry:

Fix typing and collections.abc type hint ambiguity.

gentlegiantJGC and others added 2 commits May 14, 2025 09:58
These type hints are invalid in Python 3.8.
Add `typing.` prefix to remove ambiguity.
@gentlegiantJGC
Copy link
Contributor Author

The tests need updating to reflect these changes.
Please give an indication if this is something you would consider merging and I will update the tests.

@henryiii
Copy link
Collaborator

@InvincibleRMC, thoughts?

@InvincibleRMC
Copy link
Contributor

InvincibleRMC commented May 14, 2025

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 Sequence class for example and not have the type hints conflict with typing.Sequence see issues like sizmailov/pybind11-stubgen#241.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but do we actually still want to support Python 3.8?

Python 3.8 EOL was 2024-10-07

@@ -239,6 +239,27 @@
# define PYBIND11_SUBINTERPRETER_SUPPORT
#endif

// 3.9 Compatibility
#if 0x03090000 <= PY_VERSION_HEX
# define PYBIND11_ITERABLE_TYPE_HINT "collections.abc.Iterable"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to make these

PYBIND11_TYPE_HINT_...

so they line up nicely and are easier to pin-point.

@@ -239,6 +239,27 @@
# define PYBIND11_SUBINTERPRETER_SUPPORT
#endif

// 3.9 Compatibility
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Python 3.9+ Compatibility

?
(The + is the most important aspect of my suggestion.)

@gentlegiantJGC
Copy link
Contributor Author

do we actually still want to support Python 3.8?

You still have test runners for 3.8 so I assumed you still supported it.
I couldn't find anything saying what your minimum supported version was.
If you want to drop support then it would simplify this.

@rwgk
Copy link
Collaborator

rwgk commented May 14, 2025

If you want to drop support then it would simplify this.

Me: yes

But I'll go with @henryiii's judgement.

@henryiii
Copy link
Collaborator

Let's keep it for 3.0. We can drop it for 3.1.

@henryiii
Copy link
Collaborator

I couldn't find anything saying what your minimum supported version was.

It's in setup.cfg (or pyproject.toml after #5598)

@gentlegiantJGC
Copy link
Contributor Author

I think I am going to scale back this pull request to just adding the module prefix.
Fixing the issue in 3.8 is going to require a lot of changes to the tests which seems pointless for a version that has reached end of life.
I will leave the 3.8 macros but commented out so someone can add them later if they want.

Fixing this issue in Python 3.8 will require updating lots of tests. This can be added in a further pull request.
@gentlegiantJGC gentlegiantJGC changed the title Fix Python 3.8 type hints and add module prefix Add typing and collections.abc module prefix May 15, 2025
@gentlegiantJGC
Copy link
Contributor Author

I have fixed the tests.
This only includes adding the module prefixes for typing and collections.abc.
I have no personal interest in supporting Python 3.8 so I will leave that to someone else.
I have left the type hint macros for 3.8 in common.h but commented out to make it easier if someone does want to fix the 3.8 issues. This should just be a case of uncommenting the macros and updating the tests with a switch based on python version.

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.

4 participants