Skip to content
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

Fix crash for deferred methods introduced by #2193 (self-type). #2375

Merged
merged 1 commit into from
Oct 29, 2016
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
16 changes: 14 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
[
('node', FuncItem),
('context_type_name', Optional[str]), # Name of the surrounding class (for error messages)
('class_type', Optional[Type]), # And its type (from class_context)
])


Expand Down Expand Up @@ -202,13 +203,20 @@ def check_second_pass(self) -> bool:
todo = self.deferred_nodes
self.deferred_nodes = []
done = set() # type: Set[FuncItem]
for node, type_name in todo:
for node, type_name, class_type in todo:
if node in done:
continue
# This is useful for debugging:
# print("XXX in pass %d, class %s, function %s" %
# (self.pass_num, type_name, node.fullname() or node.name()))
done.add(node)
if type_name:
self.errors.push_type(type_name)
if class_type:
self.class_context.append(class_type)
self.accept(node)
if class_type:
self.class_context.pop()
if type_name:
self.errors.pop_type()
return True
Expand All @@ -221,7 +229,11 @@ def handle_cannot_determine_type(self, name: str, context: Context) -> None:
type_name = self.errors.type_name[-1]
else:
type_name = None
self.deferred_nodes.append(DeferredNode(node, type_name))
if self.class_context:
class_context_top = self.class_context[-1]
else:
class_context_top = None
self.deferred_nodes.append(DeferredNode(node, type_name, class_context_top))
# 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
8 changes: 8 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,14 @@ def deco(f: Callable[[T], int]) -> Callable[[T], int]:
main:1: note: In module imported here:
tmp/a.py:6: error: Revealed type is 'def (builtins.str*) -> builtins.int'

[case testDeferredClassContext]
class A:
def f(self) -> str: return 'foo'
class B(A):
def f(self) -> str: return self.x
def initialize(self): self.x = 'bar'
[out]


-- Scripts and __main__

Expand Down