Skip to content

Several dataclass crashes involving Final attributes #10090

Closed
@matthewhughes934

Description

@matthewhughes934

Crash Report

I was just trying things out and found a crash with:

from dataclasses import dataclass
from typing import Final

@dataclass
class FirstClass:
    FIRST_CONST: Final = 3

@dataclass  # mypy runs fine if this isn't a dataclass
class SecondClass:
    SECOND_CONST: Final = FirstClass.FIRST_CONST

Note: I don't think this code is very usable. I think I would want ClassVar here (or just a module-scoped variable), but thought the report might be interesting all the same.

Traceback

Traceback on current master:

Please report a bug at https://github.com/python/mypy/issues
version: 0.820+dev.88dde657fedecf17e15222ad4b685b0aeddcc423
Traceback (most recent call last):
  File "/home/mjh/.pyenv/versions/3.8.6/bin/mypy", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "/home/mjh/src/mypy/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/mjh/src/mypy/mypy/main.py", line 98, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/mjh/src/mypy/mypy/build.py", line 179, in build
    result = _build(
  File "/home/mjh/src/mypy/mypy/build.py", line 253, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/mjh/src/mypy/mypy/build.py", line 2658, in dispatch
    process_graph(graph, manager)
  File "/home/mjh/src/mypy/mypy/build.py", line 2982, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/mjh/src/mypy/mypy/build.py", line 3074, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 78, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 199, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(next_id, state,
  File "/home/mjh/src/mypy/mypy/semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "/home/mjh/src/mypy/mypy/semanal.py", line 394, in refresh_partial
    self.refresh_top_level(node)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 405, in refresh_top_level
    self.accept(d)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 4835, in accept
    node.accept(self)
  File "/home/mjh/src/mypy/mypy/nodes.py", line 950, in accept
    return visitor.visit_class_def(self)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1048, in visit_class_def
    self.analyze_class(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1125, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1134, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/home/mjh/src/mypy/mypy/semanal.py", line 1180, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/home/mjh/src/mypy/mypy/plugins/dataclasses.py", line 361, in dataclass_class_maker_callback
    transformer.transform()
  File "/home/mjh/src/mypy/mypy/plugins/dataclasses.py", line 106, in transform
    ctx.api.defer()
  File "/home/mjh/src/mypy/mypy/semanal.py", line 4554, in defer
    assert not self.final_iteration, 'Must not defer during final iteration'
AssertionError: Must not defer during final iteration
/home/mjh/tmp/testing_py/mypy_test.py:9: : note: use --pdb to drop into pdb

Traceback on pip package:

mypy_test.py:9: 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.800
Traceback (most recent call last):
  File "mypy/semanal.py", line 4835, in accept
  File "mypy/nodes.py", line 950, in accept
  File "mypy/semanal.py", line 1048, in visit_class_def
  File "mypy/semanal.py", line 1125, in analyze_class
  File "mypy/semanal.py", line 1134, in analyze_class_body_common
  File "mypy/semanal.py", line 1180, in apply_class_plugin_hooks
  File "mypy/plugins/dataclasses.py", line 361, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 106, in transform
  File "mypy/semanal.py", line 4554, in defer
AssertionError: Must not defer during final iteration
mypy_test.py:9: : note: use --pdb to drop into pdb

To Reproduce

Run mypy over a file containing the content above.

Your Environment

  • Mypy version used: 0.820+dev.88dde657fedecf17e15222ad4b685b0aeddcc423 and 0.800
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.6
  • Operating system and version: Debian GNU/Linux 10 (buster)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions