False positive for `Final` attributes using typevars in dataclasses
Update: The false positive happens for non-dataclasses as well; see https://github.com/python/mypy/issues/21637#issuecomment-4806794052.
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
Finaldataclass field initialized in the class body is not a class attribute unless explicitly annotated withClassVar.
Related to #5608 and #21334.
To Reproduce
from dataclasses import dataclass
from typing import Final
from typing import Generic
from typing import TypeVar
T = TypeVar("T")
@dataclass(frozen=True)
class A(Generic[T]):
value: Final[T] # error: Final name declared in class body cannot depend on type variables [misc]
@dataclass(frozen=True)
class B(Generic[T]):
value: T
(https://mypy-play.net/?mypy=2.1.0&python=3.14&gist=996c4b8d68e8b57e292b49a834f4ac24)
Expected Behavior
No error.
Actual Behavior
main.py:11: error: Final name declared in class body cannot depend on type variables [misc]
Your Environment
- Mypy version used: 2.1.0
- Mypy command-line flags: none for reproducer
- Mypy configuration options from
mypy.ini(and other config files): none for reproducer - Python version used: 3.14
It seems to me this has nothing to do with dataclasses, the same error happens for the innocuous analog:
from typing import Final
from typing import Generic
from typing import TypeVar
T = TypeVar("T")
class A(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:
from typing import Final from typing import Generic from typing import TypeVar T = TypeVar("T") class A(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 = xI don't see why
Final[int]should be treated as an instance variable butFinal[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] = 3creates a dataclass field and instance-level final attributexwith default value3;x: ClassVar[Final[int]] = 3is necessary to create a final class variable with value3. In non-dataclasses, combiningClassVarandFinalis redundant, and type checkers may choose to warn or error on the redundancy. [src]
But it's not initialized in the class body!
Oh, right! Oops.
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?
Hm might as well keep this open -- dataclass Final things probably needs extra code to support. Let's see if I can mark it as a child issue :^)
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!