Skip to content

Crash when using decorated function as type annotation in import cycle #5652

@msullivan

Description

@msullivan

Discovered from a user report at Dropbox, the following test case crashes:

[case testCheckDecoratedFuncAsAnnotWithImportCycle]
import a
[file a.py]
from typing import TypeVar
import b

T = TypeVar('T')
def idf(x: T) -> T: return x

@idf
def Session() -> None: pass

[file b.py]
MYPY = False
if MYPY:
    from a import Session

def f(self, session: Session) -> None:  # E: Invalid type "a.Session"
    pass
[builtins fixtures/bool.pyi]

with a traceback ending with:

  File "/home/msullivan/src/mypy/mypy/semanal.py", line 442, in _visit_func_def
    defn.type = analyzer.visit_callable_type(defn.type, nested=False)
  File "/home/msullivan/src/mypy/mypy/typeanal.py", line 421, in visit_callable_type
    ret = t.copy_modified(arg_types=self.anal_array(t.arg_types, nested=nested),
  File "/home/msullivan/src/mypy/mypy/typeanal.py", line 639, in anal_array
    res.append(self.anal_type(t, nested))
  File "/home/msullivan/src/mypy/mypy/typeanal.py", line 646, in anal_type
    return t.accept(self)
  File "/home/msullivan/src/mypy/mypy/types.py", line 242, in accept
    return visitor.visit_unbound_type(self)
  File "/home/msullivan/src/mypy/mypy/typeanal.py", line 182, in visit_unbound_type
    typ = self.visit_unbound_type_nonoptional(t)
  File "/home/msullivan/src/mypy/mypy/typeanal.py", line 218, in visit_unbound_type_nonoptional
    tvar_def = self.tvar_scope.get_binding(sym)
  File "/home/msullivan/src/mypy/mypy/tvar_scope.py", line 78, in get_binding

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions