-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed as duplicate of#17623
Closed as duplicate of#17623
Copy link
Labels
bugmypy got something wrongmypy got something wrongtopic-dataclassestopic-pep-695Issues related to PEP 695 syntaxIssues related to PEP 695 syntax
Description
Bug Report
Frozen dataclasses should be implicitly Final on its members.
Currently mypy detects correctly that a frozen dataclass member cannot be assigned to; this is similar (if not altogether equivalent) to having a Final annotation on that (and every) member. This is important because it affects type covariance:
To Reproduce
from typing import cast
from dataclasses import dataclass
class C: pass
class D(C): pass
@dataclass(frozen=True)
class MyData[T: C]:
m: T
def f(x: MyData[C]) -> None: pass
f(cast(MyData[D], 0)) # error, T is invariant here because `m` is public and deemed assignableHowever, since MyData above is frozen, m cannot be assigned to and can be deemed final for purposes of covariance.
Expected Behavior
The above should typecheck:
f(cast(MyData[D], 0)) # this should typecheckActual Behavior
The above doesn't typecheck.
Your Environment
- Mypy version used: 1.18.2
- Mypy command-line flags: nothing special
- Mypy configuration options from
mypy.ini(and other config files): strict mode = True - Python version used: 3.13.7
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-dataclassestopic-pep-695Issues related to PEP 695 syntaxIssues related to PEP 695 syntax