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

[PEP 695] Implement new scoping rules for type parameters #17258

Merged
merged 12 commits into from
May 17, 2024

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 17, 2024

Type parameters get a separate scope with some special features.

Work on #15238.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great!

mypy/semanal.py Outdated
SCOPE_CLASS: Final = 1 # Class body
SCOPE_FUNC: Final = 2 # Function or lambda
SCOPE_COMPREHENSION: Final = 3 # Comprehension or generator expression
SCOPE_TYPE_PARAM: Final = 4 # Python 3.12 new-style type parameter scope (PEP 695)
Copy link
Member

Choose a reason for hiding this comment

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

The same scoping is used for type alias values and in the future will be used for annotations (under PEP 649). In CPython we're calling this "annotation scopes", I'd suggest using the same here:

Suggested change
SCOPE_TYPE_PARAM: Final = 4 # Python 3.12 new-style type parameter scope (PEP 695)
SCOPE_ANNOTATION: Final = 4 # Annotation scopes for type parameters and aliases (PEP 695)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed as suggested.

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit 3b97e6e into master May 17, 2024
18 checks passed
@JukkaL JukkaL deleted the type-var-syntax-2-scoping branch May 17, 2024 14:02
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.

2 participants