diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index 6e0e222723567..acb785aad70a8 100644 --- a/mypy/plugins/dataclasses.py +++ b/mypy/plugins/dataclasses.py @@ -768,6 +768,8 @@ def _freeze(self, attributes: list[DataclassAttribute]) -> None: if sym_node is not None: var = sym_node.node if isinstance(var, Var): + if var.is_final: + continue # do not turn `Final` attrs to `@property` var.is_property = True else: var = attr.to_var(info) diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 2e7259e4de0a0..26c81812ab628 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -2553,3 +2553,26 @@ class X(metaclass=DCMeta): class Y(X): a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "Optional[int]", expression has type "int") [builtins fixtures/tuple.pyi] + + +[case testFrozenWithFinal] +from dataclasses import dataclass +from typing import Final + +@dataclass(frozen=True) +class My: + a: Final = 1 + b: Final[int] = 2 + +reveal_type(My.a) # N: Revealed type is "Literal[1]?" +reveal_type(My.b) # N: Revealed type is "builtins.int" +My.a = 1 # E: Cannot assign to final attribute "a" +My.b = 2 # E: Cannot assign to final attribute "b" + +m = My() +reveal_type(m.a) # N: Revealed type is "Literal[1]?" +reveal_type(m.b) # N: Revealed type is "builtins.int" + +m.a = 1 # E: Cannot assign to final attribute "a" +m.b = 2 # E: Cannot assign to final attribute "b" +[builtins fixtures/tuple.pyi]