Skip to content

Internal error on dataclasses with properties #6493

Closed

Description

@ckanesan

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!

Activity

  1. changed the title [-]Bug on dataclasses with properties[/-] [+]Internal error on dataclasses with properties[/+] on Feb 28, 2019
  2. ilevkivskyi commented on Feb 28, 2019

    @ilevkivskyi
    Member

    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.

  3. ckanesan commented on Feb 28, 2019

    @ckanesan
    Author

    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.

  4. ilevkivskyi commented on Mar 1, 2019

    @ilevkivskyi
    Member

    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.

  5. davidban77 commented on Mar 13, 2019

    @davidban77

    Hello there! experiencing this myself as well!

  6. JukkaL commented on Feb 14, 2020

    @JukkaL
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions