Skip to content

Fix two crashes in duplicate decorated functions #5427

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 2 commits into from
Aug 8, 2018
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
9 changes: 8 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,24 @@ class OverloadedFuncDef(FuncBase, SymbolNode, Statement):
"""

items = None # type: List[OverloadPart]
unanalyzed_items = None # type: List[OverloadPart]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't love all these unanalyzed things that aststrip forces upon us. Oh well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried few other solutions, but after few attempts to undo the logic of deleting overloads in second pass, I just went with this, although I don't like this either.

impl = None # type: Optional[OverloadPart]

def __init__(self, items: List['OverloadPart']) -> None:
super().__init__()
assert len(items) > 0
self.items = items
self.unanalyzed_items = items.copy()
self.impl = None
self.set_line(items[0].line)

def name(self) -> str:
return self.items[0].name()
if self.items:
return self.items[0].name()
else:
# This may happen for malformed overload
assert self.impl is not None
return self.impl.name()

def accept(self, visitor: StatementVisitor[T]) -> T:
return visitor.visit_overloaded_func_def(self)
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
# The first item was already visited
item.is_overload = True
item.accept(self)
# TODO support decorated overloaded functions properly
# TODO: support decorated overloaded functions properly
if isinstance(item, Decorator):
callable = function_type(item.func, self.builtin_type('builtins.function'))
assert isinstance(callable, CallableType)
Expand Down
6 changes: 2 additions & 4 deletions mypy/server/aststrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ def visit_decorator(self, node: Decorator) -> None:
def visit_overloaded_func_def(self, node: OverloadedFuncDef) -> None:
if not self.recurse_into_functions:
return
if node.impl:
# Revert change made during semantic analysis pass 2.
assert node.items[-1] is not node.impl
node.items.append(node.impl)
# Revert change made during semantic analysis pass 2.
node.items = node.unanalyzed_items.copy()
super().visit_overloaded_func_def(node)

@contextlib.contextmanager
Expand Down
62 changes: 62 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -7300,6 +7300,68 @@ def A(x: str) -> str: pass
==
a.py:4: error: Incompatible import of "A" (imported name has type "Callable[[str], str]", local name has type "Type[List[Any]]")

[case testFakeOverloadCrash]
import b
[file a.py]
def dec(fun):
pass
a = 1
[file a.py.2]
def dec(fun):
pass
a = 2
[file b.py]
from a import dec
@dec
def a():
pass
@dec
def a():
pass
[out]
b.py:5: error: Name 'a' already defined on line 2
==
b.py:5: error: Name 'a' already defined on line 2

[case testFakeOverloadCrash2]
# this test just should not crash
import a
[file a.py]
T = TypeVar("T")

def foo(func):
return func

@foo
def bar(x: T) -> T:
pass

@foo
def bar(x: T) -> T:
pass
[file a.py.2]
T = TypeVar("T")

def foo(func):
return func

@foo
def bar(x: T) -> T:
pass

@foo
def bar(x: T) -> T:
pass
x = 1
[out]
a.py:1: error: Name 'TypeVar' is not defined
a.py:10: error: Name 'bar' already defined on line 6
a.py:11: error: Invalid type "a.T"
==
a.py:1: error: Name 'TypeVar' is not defined
a.py:10: error: Name 'bar' already defined on line 6
a.py:11: error: Invalid type "a.T"

[case testRefreshForWithTypeComment1]
[file a.py]
from typing import List
Expand Down