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: Final dataclass field with type variable #22396
When using a dataclass with a field defined using Final and containing a type variable, mypy incorrectly errors because it infers the field as a class variable.
No errors, the check does not apply to dataclasses.
Actual Behavior
main.py:6: error: Final name declared in class body cannot depend on type variables [misc]
Found 1 error in 1 file (checked 1 source file)
Note: mypy does correctly infer that the field is a readonly generic instance attribute, but this is a separate, specific error that should exempt dataclasses.
Simply suppressing the specific misc error leads to desired behavior.
Your Environment
Mypy version used: 2.4.1
Mypy command-line flags: N/A
Mypy configuration options from mypy.ini (and other config files): N/A
I’d like to work on this. The error comes from the DEPENDENT_FINAL_IN_CLASS_BODY check in TypeChecker.visit_assignment_stmt (mypy/checker.py), which doesn’t account for dataclass fields. Those are instance attributes, and the dataclass plugin already allows bare Final[...] fields. I’m planning to skip the check for classes processed by the dataclass plugin and add a test case to check-dataclasses.test.
Activity
[-]False positive: Final dataclass field[/-][+]False positive: Final dataclass field with type variable[/+]nilayp-codes commentedon Nov 8, 2026
I’d like to work on this. The error comes from the DEPENDENT_FINAL_IN_CLASS_BODY check in TypeChecker.visit_assignment_stmt (mypy/checker.py), which doesn’t account for dataclass fields. Those are instance attributes, and the dataclass plugin already allows bare Final[...] fields. I’m planning to skip the check for classes processed by the dataclass plugin and add a test case to check-dataclasses.test.
ilevkivskyi commentedon Nov 8, 2026
This is essentially a duplicate of #21637
Finalattributes using typevars in dataclasses #21637