Skip to content

Commit 5e64bd4

Browse files
authored
New semantic analyzer: fix properties (#6396)
Fixes #6295.
1 parent 32d1ef9 commit 5e64bd4

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

mypy/newsemanal/semanal.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,11 +2832,9 @@ def visit_decorator(self, dec: Decorator) -> None:
28322832
self.fail("@final cannot be used with non-method functions", d)
28332833
for i in reversed(removed):
28342834
del dec.decorators[i]
2835-
if not dec.is_overload or dec.var.is_property:
2836-
if self.type:
2837-
dec.var.info = self.type
2838-
dec.var.is_initialized_in_class = True
2839-
self.add_symbol(dec.var.name(), dec, dec)
2835+
if (not dec.is_overload or dec.var.is_property) and self.type:
2836+
dec.var.info = self.type
2837+
dec.var.is_initialized_in_class = True
28402838
if not no_type_check and self.recurse_into_functions:
28412839
dec.func.accept(self)
28422840
if dec.decorators and dec.var.is_property:

test-data/unit/check-newsemanal.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,3 +1054,24 @@ class Test:
10541054
import a
10551055
def __init__(self) -> None:
10561056
some_module = self.a
1057+
1058+
[case testNewAnalyzerProperty]
1059+
class A:
1060+
@property
1061+
def x(self) -> B:
1062+
return 0 # E: Incompatible return value type (got "int", expected "B")
1063+
1064+
@property
1065+
def y(self) -> B:
1066+
pass
1067+
1068+
@y.setter
1069+
def y(self, x: B) -> None:
1070+
pass
1071+
1072+
class B: pass
1073+
1074+
a = A()
1075+
reveal_type(a.x) # E: Revealed type is '__main__.B'
1076+
a.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "B")
1077+
[builtins fixtures/property.pyi]

test-data/unit/pythoneval.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,5 +1368,4 @@ reveal_type(x['test'][0])
13681368
typing.pyi:251: error: Class typing.Sequence has abstract attributes "__len__"
13691369
typing.pyi:251: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
13701370
ast.pyi:31: error: Name 'PyCF_ONLY_AST' already defined (possibly by an import)
1371-
builtins.pyi:39: error: Name '__class__' already defined on line 39
13721371
_testNewAnalyzerBasicTypeshed_newsemanal.py:4: error: Revealed type is 'builtins.int*'

0 commit comments

Comments
 (0)