Skip to content

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