Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,18 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
elif not isinstance(stmt.rvalue, TempNode):
has_default = True

if (
node.is_final
and has_field_call
and not is_in_init
and not has_default
and not is_init_var
and self._spec is _TRANSFORM_SPEC_FOR_DATACLASSES
):
# A final attribute can only be assigned in __init__(), so if it is
# excluded from __init__() and has no default value, it can never be set.
self._api.fail("Final field must have a default value if init=False", stmt.rvalue)

if not has_default and self._spec is _TRANSFORM_SPEC_FOR_DATACLASSES:
# Make all non-default dataclass attributes implicit because they are de-facto
# set on self in the generated __init__(), not in the class body. On the other
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,27 @@ class Bad2:
x: InitVar[Final[int]] # E: Final can be only used as an outermost qualifier in a variable annotation
[builtins fixtures/dict.pyi]

[case testDataclassFinalFieldNotInInit]
from dataclasses import dataclass, field
from typing import Final

@dataclass
class Bad:
x: Final[int] = field(init=False) # E: Final field must have a default value if init=False

@dataclass
class GoodDefault:
x: Final[int] = field(init=False, default=0)

@dataclass
class GoodFactory:
x: Final[int] = field(init=False, default_factory=int)

@dataclass
class GoodPlain:
x: Final[int] = 0
[builtins fixtures/tuple.pyi]

[case testNoCrashOnNestedSpecialFormsWithAnnotations]
import dataclasses
from typing import ClassVar, NamedTuple
Expand Down
Loading