diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index 8c42d205d568..c8bfee49e2ec 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 03ae8906f1d3..1c64557d25b1 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