Skip to content

Crash due to unresolved forward reference to decorated method #4383

Closed
@JukkaL

Description

@JukkaL

Mypy crashes when type checking this program:

from typing import NewType

N = NewType('N', XXX)

b = C.g()

class C:
    @staticmethod
    def g() -> N:
        pass

If I replace @staticmethod with another decorator the crash persists.

Here's the traceback:

$ mypy --show-traceback t/t2.py
t/t2.py:3: error: Argument 2 to NewType(...) must be subclassable (got "Any")
t/t2.py:3: error: Name 'XXX' is not defined
t/t2.py:9: error: Invalid type "t2.N"
t/t2.py:5: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.560-dev-ba615b74f07c92221f264f7a6a5375be0a41ebed
Traceback (most recent call last):
  File "/Users/jukka/src/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/Users/jukka/src/mypy/mypy/main.py", line 66, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/jukka/src/mypy/mypy/main.py", line 119, in type_check_only
    options=options)
  File "/Users/jukka/src/mypy/mypy/build.py", line 218, in build
    graph = dispatch(sources, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 2001, in dispatch
    process_graph(graph, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 2305, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 2481, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/jukka/src/mypy/mypy/build.py", line 1880, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/jukka/src/mypy/mypy/checker.py", line 200, in check_first_pass
    self.accept(d)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 295, in accept
    stmt.accept(self)
  File "/Users/jukka/src/mypy/mypy/nodes.py", line 805, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 1350, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 1445, in check_assignment
    rvalue)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 1909, in infer_variable_type
    elif not is_valid_inferred_type(init_type) and not self.no_partial_types:
  File "/Users/jukka/src/mypy/mypy/checker.py", line 3441, in is_valid_inferred_type
    return is_valid_inferred_type_component(typ)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 3450, in is_valid_inferred_type_component
    if is_same_type(typ, UninhabitedType()):
  File "/Users/jukka/src/mypy/mypy/sametypes.py", line 28, in is_same_type
    return left.accept(SameTypeVisitor(right))
  File "/Users/jukka/src/mypy/mypy/types.py", line 1415, in accept
    return visitor.visit_forwardref_type(self)
  File "/Users/jukka/src/mypy/mypy/types.py", line 1500, in visit_forwardref_type
    raise RuntimeError('Internal error: unresolved forward reference')
RuntimeError: Internal error: unresolved forward reference
t/t2.py:5: : note: use --pdb to drop into pdb

This seems related to #4200 but the same fix doesn't seem to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions