Skip to content
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

Allow using typing_extensions.TypeVar in stubs #486

Merged
merged 1 commit into from
May 9, 2024
Merged

Conversation

AlexWaygood
Copy link
Collaborator

No description provided.

@AlexWaygood
Copy link
Collaborator Author

noqa'd in python/typeshed#11880

Copy link

github-actions bot commented May 9, 2024

This change has no effect on typeshed. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 9c4a5d8 into main May 9, 2024
21 checks passed
@JelleZijlstra JelleZijlstra deleted the y023-default branch May 9, 2024 13:18
@bzoracler
Copy link

While this would have been great, the motivation for using typing_extensions.TypeVar in the PR was actually because of the existing TypeVar node class. I tried to search for how typeshed uses default= and noticed that TypeVars which use it were still being imported from typing (e.g. contextlib.pyi).

I'm guessing typing.TypeVar(default=) only worked because symbols from typing_extensions are now treated as exactly equivalent as those from typing. Not sure what typeshed's policy on this actually is; type-checkers certainly don't behave the same:

@AlexWaygood
Copy link
Collaborator Author

Right, I'm not sure why neither mypy nor pyright is emitting an error in contextlib.pyi on Python <3.13, even though we're passing the default= parameter to typing.TypeVar when it doesn't have that parameter. That seems like a bug in mypy and pyright, and is probably due to the fact that both type checkers extensively special-case TypeVar. I think we shouldn't rely on that bug; we should probably use typing_extensions.TypeVar when we want to provide a default value for the TypeVar.

@JelleZijlstra
Copy link
Collaborator

I was also curious why we hadn't run into this before. It might actually be reasonable for type checkers to allow default= on typing.TypeVar in stubs before Python 3.13 (I assume that's the choice pyright made), but in flake8-pyi we should allow typing_extensions.TypeVar so users can use it to avoid confusion with the runtime behavior.

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.

None yet

3 participants