Repository navigation
Internal error on dataclasses with properties #6493
Copy link
Copy link
Closed
Labels
crashpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins
Description
On CPython 3.7.2 [GCC 7.3.0] on linux, with mypy 0.670
When attempting to follow the guidelines described in this article
As illustrated here:
from dataclasses import dataclass
from typing import Optional
@dataclass(init=False)
class C:
a: Optional[int]
def __init__(self, a: Optional[int] = None) -> None:
self._a = None
self.a = a
@property
def a(self) -> Optional[int]:
return self._a
@a.setter
def a(self, value: Optional[int]) -> Optional[int]:
self._a = value
return self._a
Mypy (without options) raises an INTERNAL ERROR with the following:
Traceback (most recent call last):
File ".../bin/mypy", line 10, in <module>
sys.exit(console_entry())
File ".../lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File ".../lib/python3.7/site-packages/mypy/main.py", line 91, in main
res = build.build(sources, options, None, flush_errors, fscache)
File ".../lib/python3.7/site-packages/mypy/build.py", line 162, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache)
File ".../lib/python3.7/site-packages/mypy/build.py", line 217, in _build
graph = dispatch(sources, manager)
File ".../lib/python3.7/site-packages/mypy/build.py", line 2360, in dispatch
process_graph(graph, manager)
File ".../lib/python3.7/site-packages/mypy/build.py", line 2660, in process_graph
process_stale_scc(graph, scc, manager)
File ".../lib/python3.7/site-packages/mypy/build.py", line 2761, in process_stale_scc
graph[id].semantic_analysis()
File ".../lib/python3.7/site-packages/mypy/build.py", line 1896, in semantic_analysis
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 306, in visit_file
self.accept(d)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 3787, in accept
node.accept(self)
File ".../lib/python3.7/site-packages/mypy/nodes.py", line 846, in accept
return visitor.visit_class_def(self)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 804, in visit_class_def
self.analyze_class(defn)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 822, in analyze_class
self.analyze_class_body_common(defn)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 830, in analyze_class_body_common
self.apply_class_plugin_hooks(defn)
File ".../lib/python3.7/site-packages/mypy/semanal.py", line 892, in apply_class_plugin_hooks
hook(ClassDefContext(defn, decorator, self))
File ".../lib/python3.7/site-packages/mypy/plugins/dataclasses.py", line 312, in dataclass_class_maker_callback
transformer.transform()
File ".../lib/python3.7/site-packages/mypy/plugins/dataclasses.py", line 79, in transform
attributes = self.collect_attributes()
File ".../lib/python3.7/site-packages/mypy/plugins/dataclasses.py", line 189, in collect_attributes
assert isinstance(node, Var)
AssertionError:
Any suggestions about workarounds or better implementations of the initial premise would be much appreciated!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
crashpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new plugins
Activity
[-]Bug on dataclasses with properties[/-][+]Internal error on dataclasses with properties[/+]ilevkivskyi commentedon Feb 28, 2019
Thanks for reporting! It looks like the our dataclass plugin collects a property as a dataclass field (unlike the runtime implementation), but then got confused. Should be probably not hard to fix.
ckanesan commentedon Feb 28, 2019
That's good news! Please let me know I can help, especially if you think that this might be a good candidate for a first contribution.
ilevkivskyi commentedon Mar 1, 2019
This is not super trivial and will still require some understanding of how mypy works, but I think you can try submitting a PR. If you will feel stuck, you can ask questions here or on our Gitter channel.
davidban77 commentedon Mar 13, 2019
Hello there! experiencing this myself as well!
JukkaL commentedon Feb 14, 2020
This no longer crashes, so I'm closing this issue. Mypy still doesn't support dataclasses with properties, however -- feel free to create a separate issue about this feature.