Skip to content

Fix crash on bare Final in dataclass #13528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2022
Merged
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
4 changes: 4 additions & 0 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def final_iteration(self) -> bool:
def is_stub_file(self) -> bool:
raise NotImplementedError

@abstractmethod
def analyze_simple_literal_type(self, rvalue: Expression, is_final: bool) -> Type | None:
raise NotImplementedError


# A context for querying for configuration data about a module for
# cache invalidation purposes.
Expand Down
19 changes: 18 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:

if isinstance(node, TypeAlias):
ctx.api.fail(
("Type aliases inside dataclass definitions " "are not supported at runtime"),
("Type aliases inside dataclass definitions are not supported at runtime"),
node,
)
# Skip processing this node. This doesn't match the runtime behaviour,
Expand Down Expand Up @@ -426,6 +426,23 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
is_kw_only = bool(ctx.api.parse_bool(field_kw_only_param))

known_attrs.add(lhs.name)

if sym.type is None and node.is_final and node.is_inferred:
# This is a special case, assignment like x: Final = 42 is classified
# annotated above, but mypy strips the `Final` turning it into x = 42.
# We do not support inferred types in dataclasses, so we can try inferring
# type for simple literals, and otherwise require an explicit type
# argument for Final[...].
typ = ctx.api.analyze_simple_literal_type(stmt.rvalue, is_final=True)
if typ:
node.type = typ
else:
ctx.api.fail(
"Need type argument for Final[...] with non-literal default in dataclass",
stmt,
)
node.type = AnyType(TypeOfAny.from_error)

attrs.append(
DataclassAttribute(
name=lhs.name,
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1796,3 +1796,21 @@ t: Two
reveal_type(t.__match_args__) # E: "Two" has no attribute "__match_args__" \
# N: Revealed type is "Any"
[builtins fixtures/dataclasses.pyi]

[case testFinalInDataclass]
from dataclasses import dataclass
from typing import Final

@dataclass
class FirstClass:
FIRST_CONST: Final = 3 # OK

@dataclass
class SecondClass:
SECOND_CONST: Final = FirstClass.FIRST_CONST # E: Need type argument for Final[...] with non-literal default in dataclass

reveal_type(FirstClass().FIRST_CONST) # N: Revealed type is "Literal[3]?"
FirstClass().FIRST_CONST = 42 # E: Cannot assign to final attribute "FIRST_CONST"
reveal_type(SecondClass().SECOND_CONST) # N: Revealed type is "Literal[3]?"
SecondClass().SECOND_CONST = 42 # E: Cannot assign to final attribute "SECOND_CONST"
[builtins fixtures/dataclasses.pyi]