Skip to content

TypedDict RecursionError #5357

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

Closed
dmfigol opened this issue Jul 14, 2018 · 4 comments · Fixed by #5434
Closed

TypedDict RecursionError #5357

dmfigol opened this issue Jul 14, 2018 · 4 comments · Fixed by #5434

Comments

@dmfigol
Copy link

dmfigol commented Jul 14, 2018

  • Are you reporting a bug, or opening a feature request?
    Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Dict, Any, Optional
from mypy_extensions import TypedDict


VarsDict = Dict[str, Any]
HostsDict = Dict[str, Optional[VarsDict]]

GroupDataDict = TypedDict(
    "GroupDataDict", {"children": "GroupsDict",
                      "vars": VarsDict,
                      "hosts": HostsDict}, total=False
)

GroupsDict = Dict[str, GroupDataDict]

trying to describe the following data (YAML):

all:
    children:
        servers:
            children:
                webservers:
                    hosts:
                        foo.example.com:
                        bar.example.com:
                dbservers:
                    hosts:
                        one.example.com:
                        two.example.com:
                            my_var: from_hostfile
                        three.example.com:
                            ansible_port: 5555
                            ansible_host: 192.0.2.50
            vars:
                some_server: foo.southeast.example.com
                halon_system_timeout: 30
                self_destruct_countdown: 60
                escape_pods: 2
        frontend:
            hosts:
                foo.example.com:
  • What is the actual behavior/output?
    Crash
  • What is the behavior/output you expect?
    No crash
  • What are the versions of mypy and Python you are using?
    0.610 + Python 3.7 / 3.6.6
  • Do you see the same issue after installing mypy from Git master?
    Haven't checked
  • What are the mypy flags you are using? (For example --strict-optional)
[mypy]
python_version = 3.6

check_untyped_defs = True
disallow_any_generics = True
disallow_untyped_calls = True
ignore_errors = False
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
  • If mypy crashed with a traceback, please paste
    the full traceback below.
...
    for l in it:
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1933, in <genexpr>
    return self.strategy(t.accept(self) for t in types)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1475, in accept
    return visitor.visit_forwardref_type(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1921, in visit_forwardref_type
    return t.resolved.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 512, in accept
    return visitor.visit_instance(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1895, in visit_instance
    return self.query_types(t.args)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1933, in query_types
    return self.strategy(t.accept(self) for t in types)
  File "/usr/local/lib/python3.7/site-packages/mypy/typeanal.py", line 990, in combine_lists_strategy
    for l in it:
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1933, in <genexpr>
    return self.strategy(t.accept(self) for t in types)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1103, in accept
    return visitor.visit_typeddict_type(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1905, in visit_typeddict_type
    return self.query_types(t.items.values())
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1933, in query_types
    return self.strategy(t.accept(self) for t in types)
  File "/usr/local/lib/python3.7/site-packages/mypy/typeanal.py", line 990, in combine_lists_strategy
    for l in it:
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1933, in <genexpr>
    return self.strategy(t.accept(self) for t in types)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1475, in accept
    return visitor.visit_forwardref_type(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1921, in visit_forwardref_type
    return t.resolved.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 512, in accept
    return visitor.visit_instance(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1895, in visit_instance
    return self.query_types(t.args)
RecursionError: maximum recursion depth exceeded
@ilevkivskyi
Copy link
Member

My guess this may be a duplicate of #4780 (modulo NamedTuple -> TypedDict). Does the error disappear when you set disallow_any_generics = False?

@dmfigol
Copy link
Author

dmfigol commented Jul 15, 2018

@ilevkivskyi
When i do disallow_any_generics = False, it no longer crashes, but still produces an error:

-> % mypy typeddict-recursion.py
typeddict-recursion.py:8: error: Recursive types not fully supported yet, nested types replaced with "Any"
typeddict-recursion.py:14: error: Recursive types not fully supported yet, nested types replaced with "Any"

@ilevkivskyi
Copy link
Member

it no longer crashes, but still produces an error

This is expected (the error explains why). You can just silence it with # type: ignore for now if you accept the risk explained.

@dmfigol
Copy link
Author

dmfigol commented Jul 15, 2018

I understand. I would prefer not to allow any generics.

ilevkivskyi added a commit that referenced this issue Aug 8, 2018
Fixes #5357
Fixes #4780

This might potentially fix similar crashes on recursive types.
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