Skip to content

Commit

Permalink
Don't crash on undefined decorator involved in import cycle.
Browse files Browse the repository at this point in the history
Fixes #1972.
  • Loading branch information
Guido van Rossum committed Aug 9, 2016
1 parent 28e2652 commit 2943f62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,13 @@ def check_method_override_for_base_with_name(
# Map the overridden method type to subtype context so that
# it can be checked for compatibility.
original_type = base_attr.type
if original_type is None and isinstance(base_attr.node,
FuncDef):
original_type = self.function_type(base_attr.node)
if original_type is None:
if isinstance(base_attr.node, FuncDef):
original_type = self.function_type(base_attr.node)
elif isinstance(base_attr.node, Decorator):
original_type = self.function_type(base_attr.node.func)
else:
assert False, str(base_attr.node)
if isinstance(original_type, FunctionLike):
original = map_type_from_supertype(
method_type(original_type),
Expand All @@ -824,7 +828,6 @@ def check_method_override_for_base_with_name(
base.name(),
defn)
else:
assert original_type is not None
self.msg.signature_incompatible_with_supertype(
defn.name(), name, base.name(), defn)

Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,20 @@ main:1: note: In module imported here:
tmp/a.py:3: error: Argument 1 to "dec" has incompatible type "int"; expected "str"
tmp/a.py:5: error: "str" not callable

[case testUndefinedDecoratorInImportCycle]
# cmd: mypy -m foo.base
[file foo/__init__.py]
import foo.base
class Derived(foo.base.Base):
def method(self) -> None: pass
[file foo/base.py]
import foo
class Base:
@decorator
def method(self) -> None: pass
[out]
tmp/foo/base.py:3: error: Name 'decorator' is not defined


-- Conditional function definition
-- -------------------------------
Expand Down

0 comments on commit 2943f62

Please sign in to comment.