You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Annotated[int] (no metadata) is not reported in type aliases and functional NamedTuple/TypedDict fields #22498
mypy reports Annotated[int] with no metadata when it is used as a variable or parameter annotation (Annotated[...] must have exactly one type argument and at least one annotation), but not when it is the right-hand side of an alias or a field type in the functional NamedTuple / TypedDict forms. The alias then silently means int.
The typing spec says "There must be at least one metadata element (Annotated[int] is not valid)", and CPython raises TypeError for it.
To Reproduce
fromtypingimportAnnotated, TypeAliasx: Annotated[int] =1# error: Annotated[...] must have exactly one type argument and at least one annotationA: TypeAlias=Annotated[int] # expected: error, nothing reportedB=Annotated[int] # expected: error, nothing reported
Same for type C = Annotated[int], N = NamedTuple("N", [("x", Annotated[int])]) and T = TypedDict("T", {"x": Annotated[int]}) (nothing reported in any of them).
Expected Behavior
The same error as for x: Annotated[int].
Actual Behavior
$ mypy --python-version 3.12 repro.py
repro.py:3: error: Annotated[...] must have exactly one type argument and at least one annotation [valid-type]
Found 1 error in 1 file (checked 1 source file)
Runtime (3.12): TypeError: Annotated[...] should be used with at least two arguments (a type and an annotation).
Where it seems to come from (from reading the source on master d1bad9f7, not debugged): mypy/exprtotype.py L125 replaces Annotated[X, ...] by X when args is non-empty, and the "at least one annotation" check (len(t.args) < 2) is in mypy/typeanal.py L744, which this path does not reach. PR #21503 added the and args there for the zero-argument crash; the one-argument case is still passed through.
Activity