Skip to content

Internal error using Union and NamedTuple #3799

Closed
@ned

Description

@ned

mypy version 0.521, python 3.6.2

I tried to find the smallest example that would reproduce the bug I'm getting.
Removing references to NamedTuple or moving the type alias below the classes seems to stop the error.

$ cat bug.py 
from typing import Union, NamedTuple

# Moving this below the class declarations stops the error
NodeType = Union['Foo', 'Bar']

# Removing NamedTuple from both classes stops the error
class Foo(NamedTuple):
    pass

class Bar(NamedTuple):
    pass

def foo(node: NodeType):
    x = node

I got the following error

$ mypy bug.py 
bug.py:14: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521
bug.py:14: note: please use --show-traceback to print a traceback when reporting a bug

And the traceback

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions