Skip to content

Allow to use Final and ClassVar after python 3.13 #18358

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

Merged
merged 9 commits into from
Dec 30, 2024

Conversation

aliwo
Copy link
Contributor

@aliwo aliwo commented Dec 29, 2024

Hi, Thanks for making a great library! I've been using mypy for years and it saved me from several (possibly happened) accidents!

Description

this PR allows to use Final and ClassVar after python 3.13

I saw this PR

and I saw recent changes of python 3.13
https://docs.python.org/3/library/typing.html#typing.Final
image
Final now can be nested with ClassVar. so I added a version check!

This comment has been minimized.

1 similar comment

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thank you!

mypy/typeanal.py Outdated
@@ -605,7 +605,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
t,
code=codes.VALID_TYPE,
)
else:
elif self.options.python_version < (3, 13):
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should still error in several of the cases this previously triggered, e.g. def f(x: Final[int]) -> int: ... (grep the test cases for Final can be only used as an outermost qualifier for examples)

This comment has been minimized.

mypy/semanal.py Outdated
@@ -5080,7 +5084,9 @@ def check_classvar(self, s: AssignmentStmt) -> None:
node = lvalue.node
if isinstance(node, Var):
node.is_classvar = True
analyzed = self.anal_type(s.type)
analyzed = self.anal_type(
s.type, allow_final_in_classvar=self.options.python_version >= (3, 13)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this one might be unnecessary? Let me push to your PR

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thank you for this improvement!

@aliwo
Copy link
Contributor Author

aliwo commented Dec 30, 2024

@hauntsaninja Thank you very much!

Copy link
Contributor

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

@hauntsaninja hauntsaninja merged commit 80e5e8b into python:master Dec 30, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants