Skip to content

Crash when using TypeAlias inside a dataclass #12544

Closed
@AlexWaygood

Description

@AlexWaygood

Crash Report

The following snippet of code causes a crash:

from dataclasses import dataclass
from typing import TypeAlias

class Foo:
    x = 1

@dataclass
class A:
    ShortName: TypeAlias = Foo
    var: int = ShortName.x

It doesn't work as expected at runtime, but it also probably shouldn't cause mypy to crash.

Traceback

test.py:8: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.950+dev.222029b07e0fdcb3c174f15b61915b3b2e1665ca
Traceback (most recent call last):
  File "C:\Users\alexw\coding\mypy\venv\Scripts\mypy-script.py", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "c:\users\alexw\coding\mypy\mypy\__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "c:\users\alexw\coding\mypy\mypy\main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "c:\users\alexw\coding\mypy\mypy\main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 180, in build
    result = _build(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 256, in _build
    graph = dispatch(sources, manager, stdout)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 2733, in dispatch
    process_graph(graph, manager)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 3081, in process_graph
    process_stale_scc(graph, scc, manager)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 3173, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "c:\users\alexw\coding\mypy\mypy\semanal_main.py", line 78, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "c:\users\alexw\coding\mypy\mypy\semanal_main.py", line 199, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(next_id, state,
  File "c:\users\alexw\coding\mypy\mypy\semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 414, in refresh_partial
    self.refresh_top_level(node)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 425, in refresh_top_level
    self.accept(d)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 5281, in accept
    node.accept(self)
  File "c:\users\alexw\coding\mypy\mypy\nodes.py", line 1021, in accept
    return visitor.visit_class_def(self)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 1120, in visit_class_def
    self.analyze_class(defn)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 1200, in analyze_class
    self.analyze_class_body_common(defn)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 1209, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "c:\users\alexw\coding\mypy\mypy\semanal.py", line 1255, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "c:\users\alexw\coding\mypy\mypy\plugins\dataclasses.py", line 521, in dataclass_class_maker_callback
    transformer.transform()
  File "c:\users\alexw\coding\mypy\mypy\plugins\dataclasses.py", line 121, in transform
    attributes = self.collect_attributes()
  File "c:\users\alexw\coding\mypy\mypy\plugins\dataclasses.py", line 327, in collect_attributes
    assert isinstance(node, Var)
AssertionError:

To Reproduce

Paste the code above into a .py file, run mypy on it. Also reproduced on mypy playground.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions