Skip to content

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

Merged
JelleZijlstra merged 2 commits into
python:masterfrom
uriyyo:bugfix/final-classvar
May 19, 2021
Merged

JelleZijlstra merged 2 commits into
python:masterfrom
uriyyo:bugfix/final-classvar

Conversation

@uriyyo

@uriyyo uriyyo commented May 15, 2021

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@uriyyo uriyyo May 16, 2021 •

Copy link
Copy Markdown
Member Author

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.

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

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