Skip to content

Fix #2998: correctly handle MemberExpr in infer_partial_type #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/fixtures/list.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ class function: pass
class int: pass
class str: pass
class bool: pass

property = object() # Dummy definition.