Repository: python/mypy (Python)

Issue:
Crash when using `TypeAlias` inside a dataclass
**Crash Report**

The following snippet of code causes a crash:

```python
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](https://github.com/python/typing/discussions/1124), 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](https://mypy-play.net/?mypy=latest&python=3.10&flags=show-traceback&gist=8d5e69cc29a4823717a6f377244c121e).



Hints:

Patch:
diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py
--- a/mypy/plugins/dataclasses.py
+++ b/mypy/plugins/dataclasses.py
@@ -5,7 +5,7 @@
 
 from mypy.nodes import (
     ARG_OPT, ARG_NAMED, ARG_NAMED_OPT, ARG_POS, ARG_STAR, ARG_STAR2, MDEF,
-    Argument, AssignmentStmt, CallExpr,    Context, Expression, JsonDict,
+    Argument, AssignmentStmt, CallExpr,  TypeAlias,  Context, Expression, JsonDict,
     NameExpr, RefExpr, SymbolTableNode, TempNode, TypeInfo, Var, TypeVarExpr,
     PlaceholderNode
 )
@@ -333,6 +333,20 @@ def collect_attributes(self) -> Optional[List[DataclassAttribute]]:
 
             node = sym.node
             assert not isinstance(node, PlaceholderNode)
+
+            if isinstance(node, TypeAlias):
+                ctx.api.fail(
+                    (
+                        'Type aliases inside dataclass definitions '
+                        'are not supported at runtime'
+                    ),
+                    node
+                )
+                # Skip processing this node. This doesn't match the runtime behaviour,
+                # but the only alternative would be to modify the SymbolTable,
+                # and it's a little hairy to do that in a plugin.
+                continue
+
             assert isinstance(node, Var)
 
             # x: ClassVar[int] is ignored by dataclasses.


Test patch:
diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test
--- a/test-data/unit/check-dataclasses.test
+++ b/test-data/unit/check-dataclasses.test
@@ -513,6 +513,34 @@ Application.COUNTER = 1
 
 [builtins fixtures/dataclasses.pyi]
 
+[case testTypeAliasInDataclassDoesNotCrash]
+# flags: --python-version 3.7
+from dataclasses import dataclass
+from typing import Callable
+from typing_extensions import TypeAlias
+
+@dataclass
+class Foo:
+    x: int
+
+@dataclass
+class One:
+    S: TypeAlias = Foo  # E: Type aliases inside dataclass definitions are not supported at runtime
+
+a = One()
+reveal_type(a.S)  # N: Revealed type is "def (x: builtins.int) -> __main__.Foo"
+a.S()  # E: Missing positional argument "x" in call to "Foo"
+reveal_type(a.S(5))  # N: Revealed type is "__main__.Foo"
+
+@dataclass
+class Two:
+    S: TypeAlias = Callable[[int], str]  # E: Type aliases inside dataclass definitions are not supported at runtime
+
+c = Two()
+x = c.S  # E: Member "S" is not assignable
+reveal_type(x)  # N: Revealed type is "Any"
+[builtins fixtures/dataclasses.pyi]
+
 [case testDataclassOrdering]
 # flags: --python-version 3.7
 from dataclasses import dataclass