Skip to content

Internal error with dataclasses.InitVar and init=False #7320

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
keysmashes opened this issue Aug 11, 2019 · 1 comment · Fixed by #7390
Closed

Internal error with dataclasses.InitVar and init=False #7320

keysmashes opened this issue Aug 11, 2019 · 1 comment · Fixed by #7390
Labels
crash priority-0-high topic-plugins The plugin API and ideas for new plugins

Comments

@keysmashes
Copy link

I was trying to work around #4997 by adding a dummy field to a dataclass which would not appear in __init__ (so init=False) or in fields() (so InitVar), but ran into a mypy crash when subclassing it.

Whilst the workaround is a nasty hack, it works at runtime, so IMO mypy should cope with it.

test.py:

import dataclasses


@dataclasses.dataclass
class Struct:
    _dummy: dataclasses.InitVar = dataclasses.field(init=False)


@dataclasses.dataclass
class Header(Struct):
    foo: int

mypy output:

$ ~/src/mypy/venv/bin/mypy test.py --show-traceback
test.py:10: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.730+dev.c610a312853921d09083f7525c183810e4232056
Traceback (most recent call last):
  File "/home/josh/src/mypy/venv/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/main.py", line 83, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/build.py", line 162, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/build.py", line 224, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/build.py", line 2559, in dispatch
    process_graph(graph, manager)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/build.py", line 2868, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/build.py", line 2961, in process_stale_scc
    semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal_main.py", line 77, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal_main.py", line 202, in process_top_levels
    patches)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal_main.py", line 330, in semantic_analyze_target
    active_type=active_type)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 376, in refresh_partial
    self.refresh_top_level(node)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 387, in refresh_top_level
    self.accept(d)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 4591, in accept
    node.accept(self)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/nodes.py", line 925, in accept
    return visitor.visit_class_def(self)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 1011, in visit_class_def
    self.analyze_class(defn)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 1082, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 1091, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/semanal.py", line 1136, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/plugins/dataclasses.py", line 352, in dataclass_class_maker_callback
    transformer.transform()
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/plugins/dataclasses.py", line 84, in transform
    if info[attr.name].type is None:
  File "/home/josh/src/mypy/venv/lib/python3.7/site-packages/mypy/nodes.py", line 2428, in __getitem__
    raise KeyError(name)
KeyError: '_dummy'
test.py:10: : note: use --pdb to drop into pdb
@ilevkivskyi
Copy link
Member

Thanks for reporting! I confirm this crash on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash priority-0-high topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants