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

Infinite recursion with recursive TypedDict #3685

Closed
JukkaL opened this issue Jul 10, 2017 · 1 comment · Fixed by #3952
Closed

Infinite recursion with recursive TypedDict #3685

JukkaL opened this issue Jul 10, 2017 · 1 comment · Fixed by #3952

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 10, 2017

Mypy crashes with RecursionError when I type check this program:

from mypy_extensions import TypedDict

A = TypedDict('A', {'a': 'A'})

a: A

Output:

t2.py:3: error: Invalid type "t2.A"
Traceback (most recent call last):
  File "/Users/jukka/src/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/Users/jukka/src/mypy/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/jukka/src/mypy/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/Users/jukka/src/mypy/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 1769, in dispatch
    process_graph(graph, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 2012, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/jukka/src/mypy/mypy/build.py", line 2107, in process_stale_scc
    graph[id].semantic_analysis()
  File "/Users/jukka/src/mypy/mypy/build.py", line 1664, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
  File "/Users/jukka/src/mypy/mypy/semanal.py", line 295, in visit_file
    self.accept(d)
  File "/Users/jukka/src/mypy/mypy/semanal.py", line 3636, in accept
    node.accept(self)
  File "/Users/jukka/src/mypy/mypy/nodes.py", line 859, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/jukka/src/mypy/mypy/semanal.py", line 1567, in visit_assignment_stmt
    s.type = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
  File "/Users/jukka/src/mypy/mypy/semanal.py", line 1555, in anal_type
    return t.accept(a)
  File "/Users/jukka/src/mypy/mypy/types.py", line 189, in accept
    return visitor.visit_unbound_type(self)
  File "/Users/jukka/src/mypy/mypy/typeanal.py", line 292, in visit_unbound_type
    return td.copy_modified(item_types=self.anal_array(list(td.items.values())),
  File "/Users/jukka/src/mypy/mypy/typeanal.py", line 520, in anal_array
    res.append(self.anal_type(t, nested))
  File "/Users/jukka/src/mypy/mypy/typeanal.py", line 527, in anal_type
    return t.accept(self)
  File "/Users/jukka/src/mypy/mypy/types.py", line 189, in accept
    return visitor.visit_unbound_type(self)
...
  File "/Users/jukka/src/mypy/mypy/typeanal.py", line 152, in visit_unbound_type
    sym = self.lookup(t.name, t)
  File "/Users/jukka/src/mypy/mypy/semanal.py", line 3436, in lookup_qualified
    return self.lookup(name, ctx)
RecursionError: maximum recursion depth exceeded
@ilevkivskyi
Copy link
Member

I think this is exactly the same as the first crash in #3340 modulo NamedTuple -> TypedDict.

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