You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive for Final attributes using typevars in dataclasses #21637
Update: The false positive happens for non-dataclasses as well; see #21637 (comment).
Bug Report
In a dataclass, annotating an instance attribute with a Final that depends on a type variable gives a false positive Final name declared in class body cannot depend on type variables [misc] error.
I think that this should be allowed for instance attributes (but not class attributes). Per the spec:
A Final dataclass field initialized in the class body is not a class attribute unless explicitly annotated with ClassVar.
fromdataclassesimportdataclassfromtypingimportFinalfromtypingimportGenericfromtypingimportTypeVarT=TypeVar("T")
@dataclass(frozen=True)classA(Generic[T]):
value: Final[T] # error: Final name declared in class body cannot depend on type variables [misc]@dataclass(frozen=True)classB(Generic[T]):
value: T
It seems to me this has nothing to do with dataclasses, the same error happens for the innocuous analog:
fromtypingimportFinalfromtypingimportGenericfromtypingimportTypeVarT=TypeVar("T")
classA(Generic[T]):
value: Final[T] # error: Final name declared in class body cannot depend on type variables [misc]def__init__(self, x: T):
self.value=x
I don't see why Final[int] should be treated as an instance variable but Final[T] should not...
It seems to me this has nothing to do with dataclasses, the same error happens for the innocuous analog:
fromtypingimportFinalfromtypingimportGenericfromtypingimportTypeVarT=TypeVar("T")
classA(Generic[T]):
value: Final[T] # error: Final name declared in class body cannot depend on type variables [misc]def__init__(self, x: T):
self.value=x
I don't see why Final[int] should be treated as an instance variable but Final[T] should not...
Final in a class body (except in a dataclass class body) means final class variable, so AFAIK, in that non-dataclass example, the error is a true positive because a class variable cannot depend on the class's type variable.
Relevant snippet of the spec:
Type checkers should infer a final attribute that is initialized in a class body as being a class variable, except in the case of Dataclasses, where x: Final[int] = 3 creates a dataclass field and instance-level final attribute x with default value 3; x: ClassVar[Final[int]] = 3 is necessary to create a final class variable with value 3. In non-dataclasses, combining ClassVar and Final is redundant, and type checkers may choose to warn or error on the redundancy. [src]
changed the title [-]False positive for `Final` attributes using typevars in dataclasses[/-][+]False positive for `Final` instance attributes using typevars[/+]on Jul 1, 2026
I'm not sure what maintainers prefer for tracking here: should we close this as a duplicate of #8982 and add a note in #8982 that it's broken for dataclasses as well?
changed the title [-]False positive for `Final` instance attributes using typevars[/-][+]False positive for `Final` attributes using typevars in dataclasses[/+]on Jul 1, 2026
A fix for the dataclass case is up in #21643 (scoped per the discussion above — plain generic class bodies still error, tracked by #8982). CI is green, mypy_primer reports no corpus changes, and it includes tests for both the dataclass-allowed and non-dataclass-still-errors cases. Review welcome!
Activity
A5rocks commentedon Jun 26, 2026
It seems to me this has nothing to do with dataclasses, the same error happens for the innocuous analog:
I don't see why
Final[int]should be treated as an instance variable butFinal[T]should not...gschaffner commentedon Jul 1, 2026
Finalin a class body (except in a dataclass class body) means final class variable, so AFAIK, in that non-dataclass example, the error is a true positive because a class variable cannot depend on the class's type variable.Relevant snippet of the spec:
A5rocks commentedon Jul 1, 2026
But it's not initialized in the class body!
gschaffner commentedon Jul 1, 2026
Oh, right! Oops.
[-]False positive for `Final` attributes using typevars in dataclasses[/-][+]False positive for `Final` instance attributes using typevars[/+]gschaffner commentedon Jul 1, 2026
I'm not sure what maintainers prefer for tracking here: should we close this as a duplicate of #8982 and add a note in #8982 that it's broken for dataclasses as well?
A5rocks commentedon Jul 1, 2026
Hm might as well keep this open -- dataclass
Finalthings probably needs extra code to support. Let's see if I can mark it as a child issue :^)[-]False positive for `Final` instance attributes using typevars[/-][+]False positive for `Final` attributes using typevars in dataclasses[/+]adhavan18 commentedon Jul 16, 2026
A fix for the dataclass case is up in #21643 (scoped per the discussion above — plain generic class bodies still error, tracked by #8982). CI is green, mypy_primer reports no corpus changes, and it includes tests for both the dataclass-allowed and non-dataclass-still-errors cases. Review welcome!