mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False positive for `Final` attributes using typevars in dataclasses

Open gschaffner opened this issue 5 months ago • 7 comments

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 Final dataclass field initialized in the class body is not a class attribute unless explicitly annotated with ClassVar.

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

gschaffner avatar Jun 23 '26 01:06 gschaffner

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...

A5rocks avatar Jun 26 '26 06:06 A5rocks

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...

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]

gschaffner avatar Jul 01 '26 01:07 gschaffner

But it's not initialized in the class body!

A5rocks avatar Jul 01 '26 01:07 A5rocks

Oh, right! Oops.

gschaffner avatar Jul 01 '26 01:07 gschaffner

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?

gschaffner avatar Jul 01 '26 01:07 gschaffner

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 :^)

A5rocks avatar Jul 01 '26 01:07 A5rocks

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!

adhavan18 avatar Jul 16 '26 06:07 adhavan18