Skip to content

Push correct enclosing class while deferring a method #4073

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 5 commits into from
Oct 12, 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
17 changes: 13 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,15 @@ def check_top_level(self, node: MypyFile) -> None:

def handle_cannot_determine_type(self, name: str, context: Context) -> None:
node = self.scope.top_function()
if (self.pass_num < LAST_PASS and node is not None
and isinstance(node, (FuncDef, LambdaExpr))):
if self.pass_num < LAST_PASS and isinstance(node, (FuncDef, LambdaExpr)):
# Don't report an error yet. Just defer.
if self.errors.type_name:
type_name = self.errors.type_name[-1]
else:
type_name = None
# Shouldn't we freeze the entire scope?
active_class = self.scope.active_class()
self.deferred_nodes.append(DeferredNode(node, type_name, active_class))
enclosing_class = self.scope.enclosing_class()
self.deferred_nodes.append(DeferredNode(node, type_name, enclosing_class))
# Set a marker so that we won't infer additional types in this
# function. Any inferred types could be bogus, because there's at
# least one type that we don't know.
Expand Down Expand Up @@ -3323,6 +3322,16 @@ def active_class(self) -> Optional[TypeInfo]:
return self.stack[-1]
return None

def enclosing_class(self) -> Optional[TypeInfo]:
top = self.top_function()
assert top, "This method must be called from inside a function"
index = self.stack.index(top)
assert index, "Scope stack must always start with a module"
enclosing = self.stack[index - 1]
if isinstance(enclosing, TypeInfo):
return enclosing
return None

def active_self_type(self) -> Optional[Union[Instance, TupleType]]:
info = self.active_class()
if info:
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3996,6 +3996,41 @@ class F(six.with_metaclass(t.M)): pass
@six.add_metaclass(t.M)
class G: pass

[case testCorrectEnclosingClassPushedInDeferred]
class C:
def __getattr__(self, attr: str) -> int:
x: F
return x.f

class F:
def __init__(self, f: int) -> None:
self.f = f
[out]

[case testCorrectEnclosingClassPushedInDeferred2]
from typing import TypeVar
T = TypeVar('T', bound=C)
class C:
def m(self: T) -> T:
class Inner:
x: F
f = x.f
return self

class F:
def __init__(self, f: int) -> None:
self.f = f
[out]

[case testCorrectEnclosingClassPushedInDeferred3]
class A:
def f(self) -> None:
def g(x: int) -> int:
return y

y = int()
[out]

[case testMetaclassMemberAccessViaType]
from typing import Type
class M(type):
Expand Down