Skip to content

Crash when using TypeAlias inside a dataclass #12544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AlexWaygood opened this issue Apr 7, 2022 · 0 comments · Fixed by #12792
Closed

Crash when using TypeAlias inside a dataclass #12544

AlexWaygood opened this issue Apr 7, 2022 · 0 comments · Fixed by #12792
Labels
crash topic-dataclasses topic-type-alias TypeAlias and other type alias issues

Comments

@AlexWaygood
Copy link
Member

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.

@AlexWaygood AlexWaygood added crash topic-type-alias TypeAlias and other type alias issues topic-dataclasses labels Apr 7, 2022
JukkaL pushed a commit that referenced this issue May 20, 2022
Skip processing a type alias node and generate an error.

Fixes #12544.
JukkaL pushed a commit that referenced this issue May 20, 2022
Skip processing a type alias node and generate an error.

Fixes #12544.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-dataclasses topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant