Skip to content
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

Fix crash on dataclass field / property collision #16147

Merged
merged 1 commit into from
Sep 19, 2023
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
5 changes: 5 additions & 0 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ClassDef,
Context,
DataclassTransformSpec,
Decorator,
Expression,
FuncDef,
FuncItem,
Expand Down Expand Up @@ -575,6 +576,10 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
# but the only alternative would be to modify the SymbolTable,
# and it's a little hairy to do that in a plugin.
continue
if isinstance(node, Decorator):
# This might be a property / field name clash.
# We will issue an error later.
continue

assert isinstance(node, Var)

Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2519,3 +2519,15 @@ a: MyDataclass
b = [a, a] # trigger joining the types

[builtins fixtures/dataclasses.pyi]

[case testPropertyAndFieldRedefinitionNoCrash]
from dataclasses import dataclass

@dataclass
class Foo:
@property
def c(self) -> int:
return 0

c: int # E: Name "c" already defined on line 5
[builtins fixtures/dataclasses.pyi]