Skip to content

issue with dataclass default properties #8025

@sstollenwerk

Description

@sstollenwerk

Bug with mypy 0.740:

Example of code that causes the program to crash:


from dataclasses import dataclass, field

@dataclass(frozen=True)
class Demo:
    @property
    def c(self) -> int:
        return self.a + self.b

    a:int
    b:int
    
    c:int = field(init=False, default = c )
    

Expected behaviour:
run without crashing. Not sure whether or not it should complain about c being redefined (is a hack due to post_init issues with frozen dataclasses), but I don't think the code should crash.

actual behaviour (with --show-traceback flag, no other flags):

dataclass_mypy.py:13: error: Name 'c' already defined on line 6
dataclass_mypy.py:4: 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.740
Traceback (most recent call last):
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\HL167RR\AppData\Local\Programs\Python\Python38-32\Scripts\mypy.exe\__main__.py", line 9, in <module>
    sys.exit(console_entry())
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\main.py", line 88, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\build.py", line 164, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\build.py", line 229, in _build
    graph = dispatch(sources, manager, stdout)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\build.py", line 2566, in dispatch
    process_graph(graph, manager)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\build.py", line 2875, in process_graph
    process_stale_scc(graph, scc, manager)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\build.py", line 2968, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal_main.py", line 77, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal_main.py", line 198, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(next_id, state,
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal_main.py", line 325, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 378, in refresh_partial
    self.refresh_top_level(node)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 389, in refresh_top_level
    self.accept(d)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 4626, in accept
    node.accept(self)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\nodes.py", line 927, in accept
    return visitor.visit_class_def(self)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 1022, in visit_class_def
    self.analyze_class(defn)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 1093, in analyze_class
    self.analyze_class_body_common(defn)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 1102, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\semanal.py", line 1148, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\plugins\dataclasses.py", line 367, in dataclass_class_maker_callback
    transformer.transform()
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\plugins\dataclasses.py", line 79, in transform
    attributes = self.collect_attributes()
  File "c:\users\hl167rr\appdata\local\programs\python\python38-32\lib\site-packages\mypy\plugins\dataclasses.py", line 240, in collect_attributes
    assert isinstance(node, Var)
AssertionError:
dataclass_mypy.py:4: : note: use --pdb to drop into pdb

Tested with mypy 0.740 and 0.750+dev.771cf2b23f9201cb988676f5fdea00fd0d7c60c3
Crashes in both

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions