Skip to content

Commit 2943f62

Browse files
author
Guido van Rossum
committed
Don't crash on undefined decorator involved in import cycle.
Fixes #1972.
1 parent 28e2652 commit 2943f62

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

mypy/checker.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,13 @@ def check_method_override_for_base_with_name(
808808
# Map the overridden method type to subtype context so that
809809
# it can be checked for compatibility.
810810
original_type = base_attr.type
811-
if original_type is None and isinstance(base_attr.node,
812-
FuncDef):
813-
original_type = self.function_type(base_attr.node)
811+
if original_type is None:
812+
if isinstance(base_attr.node, FuncDef):
813+
original_type = self.function_type(base_attr.node)
814+
elif isinstance(base_attr.node, Decorator):
815+
original_type = self.function_type(base_attr.node.func)
816+
else:
817+
assert False, str(base_attr.node)
814818
if isinstance(original_type, FunctionLike):
815819
original = map_type_from_supertype(
816820
method_type(original_type),
@@ -824,7 +828,6 @@ def check_method_override_for_base_with_name(
824828
base.name(),
825829
defn)
826830
else:
827-
assert original_type is not None
828831
self.msg.signature_incompatible_with_supertype(
829832
defn.name(), name, base.name(), defn)
830833

test-data/unit/check-functions.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,20 @@ main:1: note: In module imported here:
10021002
tmp/a.py:3: error: Argument 1 to "dec" has incompatible type "int"; expected "str"
10031003
tmp/a.py:5: error: "str" not callable
10041004

1005+
[case testUndefinedDecoratorInImportCycle]
1006+
# cmd: mypy -m foo.base
1007+
[file foo/__init__.py]
1008+
import foo.base
1009+
class Derived(foo.base.Base):
1010+
def method(self) -> None: pass
1011+
[file foo/base.py]
1012+
import foo
1013+
class Base:
1014+
@decorator
1015+
def method(self) -> None: pass
1016+
[out]
1017+
tmp/foo/base.py:3: error: Name 'decorator' is not defined
1018+
10051019

10061020
-- Conditional function definition
10071021
-- -------------------------------

0 commit comments

Comments
 (0)