Skip to content

False positive for Final attributes using typevars in dataclasses #21637

Open

Description

@gschaffner

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.

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

Activity

  1. added
    bug
    on Jun 23, 2026
  2. A5rocks commented on Jun 26, 2026

    @A5rocks
    Collaborator

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

  3. gschaffner commented on Jul 1, 2026

    @gschaffner
    ContributorAuthor

    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]

  4. A5rocks commented on Jul 1, 2026

    @A5rocks
    Collaborator

    But it's not initialized in the class body!

  5. gschaffner commented on Jul 1, 2026

    @gschaffner
    ContributorAuthor

    Oh, right! Oops.

  6. changed the title [-]False positive for `Final` attributes using typevars in dataclasses[/-] [+]False positive for `Final` instance attributes using typevars[/+] on Jul 1, 2026
  7. gschaffner commented on Jul 1, 2026

    @gschaffner
    ContributorAuthor

    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?

  8. A5rocks commented on Jul 1, 2026

    @A5rocks
    Collaborator

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

  9. changed the title [-]False positive for `Final` instance attributes using typevars[/-] [+]False positive for `Final` attributes using typevars in dataclasses[/+] on Jul 1, 2026
  10. adhavan18 commented on Jul 16, 2026

    @adhavan18

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-dataclasses

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions