Skip to content

Commit f9c0614

Browse files
authored
Fix false positive when using properties (#5574)
1 parent eb41417 commit f9c0614

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3293,7 +3293,7 @@ def add_symbol(self, name: str, node: SymbolTableNode,
32933293
self.locals[-1][name] = node
32943294
elif self.type:
32953295
existing = self.type.names.get(name)
3296-
if existing and existing.node != node.node:
3296+
if existing and isinstance(existing.node, TypeInfo) and existing.node != node.node:
32973297
self.name_already_defined(name, context, existing)
32983298
return
32993299
self.type.names[name] = node

test-data/unit/check-classes.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5087,3 +5087,33 @@ c = D()
50875087
from a import Other
50885088
class B: ...
50895089
[out]
5090+
5091+
[case testAllowPropertyAndInit1]
5092+
class C:
5093+
def __init__(self, x: int) -> None:
5094+
self.x = x
5095+
@property
5096+
def x(self) -> int: pass
5097+
@x.setter
5098+
def x(self, x: int) -> None: pass
5099+
[builtins fixtures/property.pyi]
5100+
[out]
5101+
5102+
[case testAllowPropertyAndInit2]
5103+
class C:
5104+
@property
5105+
def x(self) -> int: pass
5106+
@x.setter
5107+
def x(self, x: int) -> None: pass
5108+
def __init__(self, x: int) -> None:
5109+
self.x = x
5110+
[builtins fixtures/property.pyi]
5111+
5112+
[case testAllowPropertyAndInit3]
5113+
class C:
5114+
def __init__(self, x: int) -> None:
5115+
self.x = x # type: ignore
5116+
@property # Should be no error here
5117+
def x(self) -> int: pass
5118+
[builtins fixtures/property.pyi]
5119+
[out]

0 commit comments

Comments
 (0)