Skip to content

Do not allow to use Final and ClassVar at same time #10478

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 2 commits into from
May 19, 2021

Conversation

uriyyo
Copy link
Member

@uriyyo uriyyo commented May 15, 2021

Description

Fixes #10477

Do not allow to use Final and ClassVar at same time.

Test Plan

Code bellow:

from typing import Final, ClassVar

class A:
    a: Final[ClassVar[int]]

After this PR will produce an error:

error: Variable should not be annotated with both ClassVar and Final

But currently, it will not raise any error.

from typing import Final, ClassVar

class A:
a: Final[ClassVar[int]] # E: Variable should not be annotated with both ClassVar and Final
Copy link
Member

Choose a reason for hiding this comment

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

Can you test some other variants, such as a: ClassVar[Final] = 1, a: ClassVar[Final[int]]?

Copy link
Member Author

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

@uriyyo uriyyo May 16, 2021

Choose a reason for hiding this comment

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

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.

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.

Looks good, but I'm going to hold off on merging because GitHub Actions are broken at the moment.

@JelleZijlstra JelleZijlstra merged commit c605579 into python:master May 19, 2021
hauntsaninja added a commit that referenced this pull request Dec 30, 2024
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 <seungwon.jeong@wesang.com>
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
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.

Variable can be annotated with Final and ClassVar at the same time
2 participants