Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal error using Union and NamedTuple #3799

Closed
ned opened this issue Aug 4, 2017 · 1 comment · Fixed by #3952
Closed

Internal error using Union and NamedTuple #3799

ned opened this issue Aug 4, 2017 · 1 comment · Fixed by #3952

Comments

@ned
Copy link

ned commented Aug 4, 2017

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
@ilevkivskyi
Copy link
Member

Thank you for reporting! I think this is essentially a duplicate of #3419 but I would like to keep this issue open, since we have a nice small repro here.

JukkaL pushed a commit that referenced this issue Sep 27, 2017
Forward references didn't work with anything apart from classes, for example 
this didn't work:

```
x: A
A = NamedTuple('A', [('x', int)])
```

The same situation was with `TypedDict`, `NewType`, and type aliases. The 
root problem is that these synthetic types are neither detected in first pass, 
nor fixed in third pass. In certain cases this can lead to crashes (first six issues 
below are various crash scenarios). This fixes these crashes by applying some 
additional patches after third pass.

Here is the summary of the PR:

* New simple wrapper type `ForwardRef` with only one field `link` is introduced 
  (with updates to type visitors)
* When an unknown type is found in second pass, the corresponding 
  `UnboundType` is wrapped in `ForwardRef`, it is given a "second chance" in 
  third pass.
* After third pass I record the "suspicious" nodes, where forward references and 
  synthetic types have been encountered and append patches (callbacks) to fix 
  them after third pass. Patches use the new visitor `TypeReplacer` (which is the 
  core of this PR).

Fixes #3340
Fixes #3419
Fixes #3674
Fixes #3685
Fixes #3799
Fixes #3836
Fixes #3881
Fixes #867
Fixes #2241
Fixes #2399
Fixes #1701
Fixes #3016
Fixes #3054
Fixes #2762
Fixes #3575
Fixes #3990
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants