diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index 24077bb4a549..00c46e1417c5 100644 --- 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. diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 4cddc59b0153..ca6b53490106 100644 --- 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