Skip to content

Annotated[int] (no metadata) is not reported in type aliases and functional NamedTuple/TypedDict fields #22498

Open

Description

@seunghyun-24

Bug Report

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

from typing import Annotated, TypeAlias

x: Annotated[int] = 1  # error: Annotated[...] must have exactly one type argument and at least one annotation
A: TypeAlias = Annotated[int]  # expected: error, nothing reported
B = 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.

Your Environment

  • Mypy version used: 2.4.1 (also master d1bad9f7)
  • Mypy command-line flags: none (--python-version 3.12)
  • Mypy configuration options: none
  • Python version used: 3.12

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions