Skip to content

Internal Error with forward references #3881

Description

@garrettheel

Reporting a bug I found.

$ mypy mypy_bug.py --show-traceback
mypy_bug.py:15: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521
Traceback (most recent call last):
  File "/Users/gheel/.virtualenvs/mypy/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/build.py", line 1801, in dispatch
    process_graph(graph, manager)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/build.py", line 2044, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/build.py", line 2147, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/build.py", line 1716, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 185, in check_first_pass
    self.accept(d)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 754, in accept
    return visitor.visit_class_def(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1109, in visit_class_def
    self.accept(defn.defs)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_block(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
    self.accept(s)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 565, in accept
    return visitor.visit_func_def(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 519, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 578, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 738, in check_func_def
    self.accept(item.body)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_block(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
    self.accept(s)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 859, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1209, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1301, in check_assignment
    rvalue)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1692, in infer_variable_type
    elif not is_valid_inferred_type(init_type):
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/checker.py", line 3113, in is_valid_inferred_type
    if is_same_type(typ, NoneTyp()):
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/sametypes.py", line 25, in is_same_type
    left = simplify_union(left)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/sametypes.py", line 33, in simplify_union
    return UnionType.make_simplified_union(t.items)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/types.py", line 1096, in make_simplified_union
    if (i != j and is_proper_subtype(tj, ti)):
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
    return left.accept(ProperSubtypeVisitor(right))
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/types.py", line 435, in accept
    return visitor.visit_instance(self)
  File "/Users/gheel/.virtualenvs/mypy/lib/python3.6/site-packages/mypy/subtypes.py", line 605, in visit_instance
    for base in left.type.mro:
TypeError: 'NoneType' object is not iterable
mypy_bug.py:15: note: use --pdb to drop into pdb

Reproducible case:

from typing import NamedTuple, Union

AOrB = Union['A', 'B']

class A(NamedTuple):
    lat: float
    lng: float

class B(object):

    def __init__(self, a: AOrB) -> None:
        self.a = a

    @property
    def lat(self) -> float:
        return self.a.lat

    @property
    def lng(self) -> float:
        return self.a.lng

Note that moving the definition of AOrB after the definition of A works fine. This seems only to happen when all of the types are forward references. Making the constructor of B use a forward reference to AOrB does not fix the issue either.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions