diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py index 48462b270d9f6..ac675047e34e4 100644 --- a/mypy/newsemanal/semanal.py +++ b/mypy/newsemanal/semanal.py @@ -2858,11 +2858,9 @@ def visit_decorator(self, dec: Decorator) -> None: self.fail("@final cannot be used with non-method functions", d) for i in reversed(removed): del dec.decorators[i] - if not dec.is_overload or dec.var.is_property: - if self.type: - dec.var.info = self.type - dec.var.is_initialized_in_class = True - self.add_symbol(dec.var.name(), dec, dec) + if (not dec.is_overload or dec.var.is_property) and self.type: + dec.var.info = self.type + dec.var.is_initialized_in_class = True if not no_type_check and self.recurse_into_functions: dec.func.accept(self) if dec.decorators and dec.var.is_property: diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index d8a148dd5be6f..e39988aeee5fa 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -1054,3 +1054,24 @@ class Test: import a def __init__(self) -> None: some_module = self.a + +[case testNewAnalyzerProperty] +class A: + @property + def x(self) -> B: + return 0 # E: Incompatible return value type (got "int", expected "B") + + @property + def y(self) -> B: + pass + + @y.setter + def y(self, x: B) -> None: + pass + +class B: pass + +a = A() +reveal_type(a.x) # E: Revealed type is '__main__.B' +a.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "B") +[builtins fixtures/property.pyi] diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 987c17a7fd3da..983ef56384e1e 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1368,5 +1368,4 @@ reveal_type(x['test'][0]) typing.pyi:251: error: Class typing.Sequence has abstract attributes "__len__" typing.pyi:251: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass ast.pyi:31: error: Name 'PyCF_ONLY_AST' already defined (possibly by an import) -builtins.pyi:39: error: Name '__class__' already defined on line 39 _testNewAnalyzerBasicTypeshed_newsemanal.py:4: error: Revealed type is 'builtins.int*'