From 5a11fee2aa9ac5c75b69046bba4768d2920249df Mon Sep 17 00:00:00 2001 From: achbj Date: Thu, 23 Apr 2026 11:12:34 +0200 Subject: [PATCH] Fix error message for type application on non-generic expressions Update the error message "Type application is only supported for generic classes" to also mention type aliases, since type application is also valid for generic type aliases. Fixes #20927. Co-Authored-By: Claude Opus 4.6 --- mypy/message_registry.py | 2 +- test-data/unit/check-generics.test | 10 +++++----- test-data/unit/check-parameter-specification.test | 2 +- test-data/unit/check-typevar-tuple.test | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mypy/message_registry.py b/mypy/message_registry.py index 8140e28e16d7a..056016a72bff1 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -121,7 +121,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage: "Function is missing a type annotation", codes.NO_UNTYPED_DEF ) ONLY_CLASS_APPLICATION: Final = ErrorMessage( - "Type application is only supported for generic classes" + "Type application is only supported for generic classes and type aliases" ) RETURN_TYPE_EXPECTED: Final = ErrorMessage( "Function is missing a return type annotation", codes.NO_UNTYPED_DEF diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index a3a5b02d54f89..8ff1143f4be42 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -426,7 +426,7 @@ from typing import TypeVar T = TypeVar('T') def f(x: T) -> T: pass -y = f[int] # E: Type application is only supported for generic classes +y = f[int] # E: Type application is only supported for generic classes and type aliases [out] @@ -1004,7 +1004,7 @@ reveal_type(y) # N: Revealed type is "builtins.int" U[int] # E: Bad number of arguments for type alias, expected 0, given 1 O[int] # E: Bad number of arguments for type alias, expected 0, given 1 \ - # E: Type application is only supported for generic classes + # E: Type application is only supported for generic classes and type aliases [case testAliasesInClassBodyNormalVsSubscripted] @@ -1072,9 +1072,9 @@ reveal_type(ts) # N: Revealed type is "Any" us = UA.x # E: "" has no attribute "x" reveal_type(us) # N: Revealed type is "Any" -xx = CA[str] + 1 # E: Type application is only supported for generic classes -yy = TA[str]() # E: Type application is only supported for generic classes -zz = UA[str].x # E: Type application is only supported for generic classes +xx = CA[str] + 1 # E: Type application is only supported for generic classes and type aliases +yy = TA[str]() # E: Type application is only supported for generic classes and type aliases +zz = UA[str].x # E: Type application is only supported for generic classes and type aliases [builtins fixtures/tuple.pyi] [typing fixtures/typing-medium.pyi] [out] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 970ba45d0e8e2..1d6f486b5b7b0 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -92,7 +92,7 @@ class B(Generic[P]): ... Other = B[P] T = TypeVar('T', bound=Alias[..., Any]) -Alias[..., Any] # E: Type application is only supported for generic classes +Alias[..., Any] # E: Type application is only supported for generic classes and type aliases B[...] Other[...] [builtins fixtures/paramspec.pyi] diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 703653227e200..0a3cc5aee3fdf 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -2763,6 +2763,6 @@ T = TypeVar('T') def func(d: Callable[[Unpack[Ts]], T]) -> T: ... -y = func[1, int] # E: Type application is only supported for generic classes \ +y = func[1, int] # E: Type application is only supported for generic classes and type aliases \ # E: Invalid type: try using Literal[1] instead? [builtins fixtures/tuple.pyi]