Skip to content

Commit 2c686e8

Browse files
kirbyfan64gvanrossum
authored andcommitted
Fix #2998: correctly handle MemberExpr in infer_partial_type (#3064)
Fix #2998
1 parent 76824c9 commit 2c686e8

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
16171617
partial_type = PartialType(None, name, [init_type])
16181618
elif isinstance(init_type, Instance):
16191619
fullname = init_type.type.fullname()
1620-
if (isinstance(lvalue, NameExpr) and
1620+
if (isinstance(lvalue, (NameExpr, MemberExpr)) and
16211621
(fullname == 'builtins.list' or
16221622
fullname == 'builtins.set' or
16231623
fullname == 'builtins.dict') and

test-data/unit/check-inference.test

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,22 @@ class A:
12671267
def f(self) -> None:
12681268
# Attributes aren't supported right now.
12691269
self.a = [] # E: Need type annotation for variable
1270-
self.a.append(1) # E: Cannot determine type of 'a'
1271-
self.a.append('') # E: Cannot determine type of 'a'
1270+
self.a.append(1)
1271+
self.a.append('')
1272+
[builtins fixtures/list.pyi]
1273+
[out]
1274+
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]
12721286
[builtins fixtures/list.pyi]
12731287
[out]
12741288

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)