Do not allow to use Final and ClassVar at same time#10478
Do not allow to use Final and ClassVar at same time#10478JelleZijlstra merged 2 commits intopython:masterfrom
Conversation
| from typing import Final, ClassVar | ||
|
|
||
| class A: | ||
| a: Final[ClassVar[int]] # E: Variable should not be annotated with both ClassVar and Final |
There was a problem hiding this comment.
Can you test some other variants, such as a: ClassVar[Final] = 1, a: ClassVar[Final[int]]?
There was a problem hiding this comment.
Such cases will produce an error with message:
Final can be only used as an outermost qualifier in a variable annotation.
Should it be changed to Variable should not be annotated with both ClassVar and Final?
There was a problem hiding this comment.
If it's easy to do, then yes. The existing error message would make users first swap the qualifiers, then get an error anyway from your new check, which is not very user-friendly. But don't worry about changing the error message if it's complicated.
Either way I think it would be useful to test all these variations here. They can all go in the same test case.
There was a problem hiding this comment.
I have added tests that you mention, thanks for pointing this out.
Looks like it is not easy to change message for a ClassVar[Final[int]] case.
I will skip this for this moment.
JelleZijlstra
left a comment
There was a problem hiding this comment.
Looks good, but I'm going to hold off on merging because GitHub Actions are broken at the moment.
This PR allows to use Final and ClassVar after python 3.13 I saw this [PR](#10478) and I saw recent changes of python 3.13 https://docs.python.org/3/library/typing.html#typing.Final Final now can be nested with ClassVar. so I added a version check! --------- Co-authored-by: triumph1 <[email protected]> Co-authored-by: hauntsaninja <[email protected]>
Description
Fixes #10477
Do not allow to use Final and ClassVar at same time.
Test Plan
Code bellow:
After this PR will produce an error:
But currently, it will not raise any error.