Closed as not planned
Description
Bug Report
PEP 591 indicates:
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
andFinal
.
But in practice, Mypy displays a type error when you use Final
on a subclass where the parent class used a ClassVar
.
To Reproduce
from typing import ClassVar, Final, ClassVar, Literal
class Animal:
description: ClassVar[str] = "hard to generalize"
class Dog(Animal):
description: Final[str] = "very good"
https://mypy-play.net/?mypy=latest&python=3.11&gist=69298f23e25b94da7dadc694769db16d
Expected Behavior
No error.
Actual Behavior
Cannot override class variable (previously declared on base class "Animal") with instance variable [misc]
You may be tempted to work around this with:
class Dog(Animal):
description: Final[ClassVar[str]] = "very good"
But then Mypy outputs:
Variable should not be annotated with both ClassVar and Final [misc]
Your Environment
- Mypy version used: 1.4.1
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: 3.11