diff --git a/mypy/checker.py b/mypy/checker.py index ea558df8fae0..b9534e3c7ee2 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1608,7 +1608,7 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool partial_type = PartialType(None, name, [init_type]) elif isinstance(init_type, Instance): fullname = init_type.type.fullname() - if (isinstance(lvalue, NameExpr) and + if (isinstance(lvalue, (NameExpr, MemberExpr)) and (fullname == 'builtins.list' or fullname == 'builtins.set' or fullname == 'builtins.dict') and diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 6ba5a478596b..3cb0e0fce876 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1267,8 +1267,22 @@ class A: def f(self) -> None: # Attributes aren't supported right now. self.a = [] # E: Need type annotation for variable - self.a.append(1) # E: Cannot determine type of 'a' - self.a.append('') # E: Cannot determine type of 'a' + self.a.append(1) + self.a.append('') +[builtins fixtures/list.pyi] +[out] + +[case testInferListInitializedToEmptyInClassBodyAndOverriden] +from typing import List + +class A: + def __init__(self) -> None: + self.x = [] # E: Need type annotation for variable + +class B(A): + @property + def x(self) -> List[int]: + return [123] [builtins fixtures/list.pyi] [out] diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 9413cf760513..ce9978a38c4c 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -28,3 +28,5 @@ class function: pass class int: pass class str: pass class bool: pass + +property = object() # Dummy definition.