You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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#3340Fixes#3419Fixes#3674Fixes#3685Fixes#3799Fixes#3836Fixes#3881Fixes#867Fixes#2241Fixes#2399Fixes#1701Fixes#3016Fixes#3054Fixes#2762Fixes#3575Fixes#3990
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.
I got the following error
And the traceback
The text was updated successfully, but these errors were encountered: