From d9e8607d045a615afebb806491d34a5ebf6b12ee Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 5 Sep 2018 16:42:15 +0100 Subject: [PATCH] Fix false positive when using properties --- mypy/semanal.py | 2 +- test-data/unit/check-classes.test | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 3b9b96ca0ba1..7f346ac036ea 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -3293,7 +3293,7 @@ def add_symbol(self, name: str, node: SymbolTableNode, self.locals[-1][name] = node elif self.type: existing = self.type.names.get(name) - if existing and existing.node != node.node: + if existing and isinstance(existing.node, TypeInfo) and existing.node != node.node: self.name_already_defined(name, context, existing) return self.type.names[name] = node diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index f53a5d22f8fd..14ed21207a00 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -5087,3 +5087,33 @@ c = D() from a import Other class B: ... [out] + +[case testAllowPropertyAndInit1] +class C: + def __init__(self, x: int) -> None: + self.x = x + @property + def x(self) -> int: pass + @x.setter + def x(self, x: int) -> None: pass +[builtins fixtures/property.pyi] +[out] + +[case testAllowPropertyAndInit2] +class C: + @property + def x(self) -> int: pass + @x.setter + def x(self, x: int) -> None: pass + def __init__(self, x: int) -> None: + self.x = x +[builtins fixtures/property.pyi] + +[case testAllowPropertyAndInit3] +class C: + def __init__(self, x: int) -> None: + self.x = x # type: ignore + @property # Should be no error here + def x(self) -> int: pass +[builtins fixtures/property.pyi] +[out]