From d9ee30abcdfd6c44e5098b552d1598e7664efc65 Mon Sep 17 00:00:00 2001 From: echoes666 <311929028+echoes666@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:46:16 +0800 Subject: [PATCH] Fix missing 'Annotated' arity check for type aliases and functional forms exprtotype stripped Annotated[X, ...] down to X whenever the subscript had at least one argument, so a one-argument Annotated (which is invalid: it needs a type plus at least one metadata element) was turned into its inner type before typeanal could reject it. The error was only reported when the annotation went through a path that skipped exprtotype. Require at least two arguments before stripping, so the one-argument form reaches typeanal's arity check for type alias right-hand sides (implicit, explicit and PEP 695) and for the functional NamedTuple/TypedDict field types. --- mypy/exprtotype.py | 6 +++++- test-data/unit/check-annotated.test | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index 8c42d205d5687..c8bfee49e2ec0 100644 --- a/mypy/exprtotype.py +++ b/mypy/exprtotype.py @@ -122,7 +122,11 @@ def expr_to_unanalyzed_type( else: base_fullname = expr.base.fullname - if base_fullname is not None and base_fullname in ANNOTATED_TYPE_NAMES and args: + if ( + base_fullname is not None + and base_fullname in ANNOTATED_TYPE_NAMES + and len(args) >= 2 + ): # TODO: this is not the optimal solution as we are basically getting rid # of the Annotation definition and only returning the type information, # losing all the annotations. diff --git a/test-data/unit/check-annotated.test b/test-data/unit/check-annotated.test index 03ae8906f1d32..1c64557d25b1c 100644 --- a/test-data/unit/check-annotated.test +++ b/test-data/unit/check-annotated.test @@ -41,6 +41,26 @@ x: Annotated[int] # E: Annotated[...] must have exactly one type argument and a reveal_type(x) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] +[case testAnnotatedBadOneArgInTypeAlias] +from typing_extensions import Annotated, TypeAlias +A: TypeAlias = Annotated[int] # E: Annotated[...] must have exactly one type argument and at least one annotation +B = Annotated[int] # E: Annotated[...] must have exactly one type argument and at least one annotation +reveal_type(A) # N: Revealed type is "Any" +reveal_type(B) # N: Revealed type is "Any" +[builtins fixtures/tuple.pyi] + +[case testAnnotatedBadOneArgInPEP695TypeAlias] +# flags: --python-version 3.12 +from typing_extensions import Annotated +type C = Annotated[int] # E: Annotated[...] must have exactly one type argument and at least one annotation +[builtins fixtures/tuple.pyi] + +[case testAnnotatedBadOneArgInFunctionalForms] +from typing_extensions import Annotated, NamedTuple, TypedDict +N = NamedTuple("N", [("x", Annotated[int])]) # E: Annotated[...] must have exactly one type argument and at least one annotation +T = TypedDict("T", {"x": Annotated[int]}) # E: Annotated[...] must have exactly one type argument and at least one annotation +[builtins fixtures/tuple.pyi] + [case testAnnotatedBadEmptyTupleIndexInTypeApplication] from typing_extensions import Annotated list[Annotated[()]] # E: Annotated[...] must have exactly one type argument and at least one annotation