Skip to content

_typeshed.structseq: Use Final instead of ClassVar #6825

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
Jan 5, 2022

Conversation

AlexWaygood
Copy link
Member

As discussed in #6816, for some structseq classes these attributes are writeable ClassVars, but for other classes, they're read-only ClassVars. Either way, however, it's probably a bad idea to be modifying these attributes, so it makes sense to simply annotate them with Final rather than ClassVar.

Note that PEP 591 specifically states:

Type checkers should infer a final attribute that is initialized in a class body as being a class variable. Variables should not be annotated with both ClassVar and Final.

As such, annotating these attributes as Final[ClassVar[int]] would be neither necessary nor possible.

As discussed in python#6816, for some `structseq` classes these attributes are writeable `ClassVar`s, but for other classes, they're read-only `ClassVar`s. Either way, however, it's probably a bad idea to be modifying these attributes, so it makes sense to simply annotate them with `Final` rather than `ClassVar`.

Note that [PEP 591](https://www.python.org/dev/peps/pep-0591/) specifically states:

> Type checkers should infer a final attribute that is initialized in a class body as being a class variable. Variables should not be annotated with both ClassVar and Final.

As such, annotating these attributes as `Final[ClassVar[int]]` would be neither necessary nor possible.
@AlexWaygood
Copy link
Member Author

Cc. @Akuli

@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

I'm a bit surprised that Final in a class makes the member implicitly ClassVar, but it seems to work.

@Akuli
Copy link
Collaborator

Akuli commented Jan 5, 2022

There's probably a lot of other things where we aren't using Final even though we should.

@Akuli Akuli merged commit 3112f0e into python:master Jan 5, 2022
@AlexWaygood AlexWaygood deleted the patch-1 branch January 5, 2022 12:17
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