Skip to content

Commit 8b456b2

Browse files
committed
Fix #2998: correctly handle MemberExpr in infer_partial_type
1 parent e674e25 commit 8b456b2

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
16081608
partial_type = PartialType(None, name, [init_type])
16091609
elif isinstance(init_type, Instance):
16101610
fullname = init_type.type.fullname()
1611-
if (isinstance(lvalue, NameExpr) and
1611+
if (isinstance(lvalue, (NameExpr, MemberExpr)) and
16121612
(fullname == 'builtins.list' or
16131613
fullname == 'builtins.set' or
16141614
fullname == 'builtins.dict') and

test-data/unit/check-inference.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,20 @@ class A:
12721272
[builtins fixtures/list.pyi]
12731273
[out]
12741274

1275+
[case testInferListInitializedToEmptyInClassBodyAndOverriden]
1276+
from typing import List
1277+
1278+
class A:
1279+
def __init__(self) -> None:
1280+
self.x = [] # E: Need type annotation for variable
1281+
1282+
class B(A):
1283+
@property
1284+
def x(self) -> List[int]:
1285+
return [123]
1286+
[builtins fixtures/list.pyi]
1287+
[out]
1288+
12751289
[case testInferSetInitializedToEmpty]
12761290
a = set()
12771291
a.add(1)

test-data/unit/fixtures/list.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ class function: pass
2828
class int: pass
2929
class str: pass
3030
class bool: pass
31+
32+
property = object() # Dummy definition.

0 commit comments

Comments
 (0)