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: 'NoneType' object is not iterable #3836

Closed
martincmartin opened this issue Aug 16, 2017 · 5 comments · Fixed by #3952
Closed

INTERNAL ERROR: 'NoneType' object is not iterable #3836

martincmartin opened this issue Aug 16, 2017 · 5 comments · Fixed by #3952

Comments

@martincmartin
Copy link

mypy --show-traceback Deduction.py
Deduction.py:102: error: The return type of "__init__" must be None
Deduction.py:103: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty
Traceback (most recent call last):
  File "/usr/local/Cellar/mypy/0.521/libexec/bin/mypy", line 11, in <module>
    load_entry_point('mypy===0.521-858f7512cf3b4e39c0f4e8de5a13eee0e1e138fb-dirty', 'console_scripts', 'mypy')()
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1801, in dispatch
    process_graph(graph, manager)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2044, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 2147, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/build.py", line 1716, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 185, in check_first_pass
    self.accept(d)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 754, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1109, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_block(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
    self.accept(s)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 565, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 519, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 578, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 738, in check_func_def
    self.accept(item.body)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_block(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
    self.accept(s)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/nodes.py", line 974, in accept
    return visitor.visit_if_stmt(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 1977, in visit_if_stmt
    if_map, else_map = self.find_isinstance_check(e)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2559, in find_isinstance_check
    return find_isinstance_check(n, self.type_map)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2810, in find_isinstance_check
    return conditional_type_map(expr, vartype, type)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/checker.py", line 2613, in conditional_type_map
    and is_proper_subtype(current_type, proposed_type)):
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
    return left.accept(ProperSubtypeVisitor(right))
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 1114, in accept
    return visitor.visit_union_type(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 693, in visit_union_type
    return all([is_proper_subtype(item, self.right) for item in left.items])
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 693, in <listcomp>
    return all([is_proper_subtype(item, self.right) for item in left.items])
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
    return left.accept(ProperSubtypeVisitor(right))
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 900, in accept
    return visitor.visit_tuple_type(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 671, in visit_tuple_type
    if not is_proper_subtype(l, r):
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/subtypes.py", line 569, in is_proper_subtype
    return left.accept(ProperSubtypeVisitor(right))
  File "/usr/local/Cellar/mypy/0.521/libexec/lib/python3.6/site-packages/mypy/types.py", line 435, in accept
    return visitor.visit_instance(self)
  File "/usr/local/Cellar/mypy/0.521/libexec/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
Deduction.py:103: note: use --pdb to drop into pdb

@ilevkivskyi
Copy link
Member

Could you please provide the code that causes this? But from the traceback it looks related to #3799 (maybe a duplicate).

@martincmartin
Copy link
Author

martincmartin commented Aug 17, 2017

I found a small repro:

from typing import NamedTuple


# MyNamedTuple = namedtuple('MyNamedTuple', 'parent')
class MyNamedTuple(NamedTuple):
    parent: 'MyNamedTuple'


def foo(mymap) -> MyNamedTuple:
    return next((mymap[key] for key in mymap), None)

@martincmartin
Copy link
Author

martincmartin commented Aug 17, 2017

Perhaps related, it looks like the resolution of the string type hint 'MyNamedTuple' is happening too late, or is incorrect? This code:

class MyNamedTuple(NamedTuple):
    parent: 'MyNamedTuple'

def bar(nt: MyNamedTuple) -> MyNamedTuple:
    return nt

produces

Deduction.py:9: error: Incompatible return value type (got "MyNamedTuple", expected "MyNamedTuple")

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Aug 17, 2017

Then it looks very similar to the crashes reported in #3419 (not sure if it is a duplicate).

In general, forward references (string type hints) to "synthetic" types is a known problem. I hope it will be fixed very soon.

@martincmartin
Copy link
Author

Confirmed that a version with forward references, but not based on namedtuple, but rather "private" fields (leading underscore) and @Property getters (but not setters) works fine. Thanks! More verbose, but a fine work around until recursive NamedTuples are supported better.

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