Skip to content

Commit c9929e2

Browse files
authored
Fix crash on dataclass field / property collision (#16147)
I think the current error message is enough: #16141 CC @ikonst and @hauntsaninja
1 parent 249f3f8 commit c9929e2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mypy/plugins/dataclasses.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ClassDef,
2424
Context,
2525
DataclassTransformSpec,
26+
Decorator,
2627
Expression,
2728
FuncDef,
2829
FuncItem,
@@ -575,6 +576,10 @@ def collect_attributes(self) -> list[DataclassAttribute] | None:
575576
# but the only alternative would be to modify the SymbolTable,
576577
# and it's a little hairy to do that in a plugin.
577578
continue
579+
if isinstance(node, Decorator):
580+
# This might be a property / field name clash.
581+
# We will issue an error later.
582+
continue
578583

579584
assert isinstance(node, Var)
580585

test-data/unit/check-dataclasses.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,3 +2519,15 @@ a: MyDataclass
25192519
b = [a, a] # trigger joining the types
25202520

25212521
[builtins fixtures/dataclasses.pyi]
2522+
2523+
[case testPropertyAndFieldRedefinitionNoCrash]
2524+
from dataclasses import dataclass
2525+
2526+
@dataclass
2527+
class Foo:
2528+
@property
2529+
def c(self) -> int:
2530+
return 0
2531+
2532+
c: int # E: Name "c" already defined on line 5
2533+
[builtins fixtures/dataclasses.pyi]

0 commit comments

Comments
 (0)