Skip to content

Report a one-argument Annotated in type aliases and functional forms - #22500

Open
echoes666 wants to merge 1 commit into
python:masterfrom
echoes666:fix-annotated-one-arg-in-alias
Open

echoes666 wants to merge 1 commit into
python:masterfrom
echoes666:fix-annotated-one-arg-in-alias

Conversation

@echoes666

Copy link
Copy Markdown

Problem

exprtotype strips Annotated[X, ...] down to X whenever the subscript has at least one argument:

if base_fullname is not None and base_fullname in ANNOTATED_TYPE_NAMES and args:
    return expr_to_unanalyzed_type(args[0], ...)

A one-argument Annotated[int] is invalid (the typing spec: "There must be at least one metadata element (Annotated[int] is not valid)"), but it is rewritten to int before typeanal can reject it. The arity check in typeanal (len(t.args) < 2) only runs for annotations that don't go through this path, so only plain variable/parameter annotations report the error:

x: Annotated[int] = 1        # error: Annotated[...] must have exactly one type argument ...
A: TypeAlias = Annotated[int]  # silently means int
B = Annotated[int]             # silently means int
type C = Annotated[int]        # silently means int
N = NamedTuple("N", [("x", Annotated[int])])
T = TypedDict("T", {"x": Annotated[int]})

CPython raises TypeError for all of these at runtime.

Fix

Require at least two arguments before stripping the metadata, so the one-argument form is left as an Annotated[...] unbound type and reaches the existing arity check in typeanal.

Test plan

New cases in test-data/unit/check-annotated.test: testAnnotatedBadOneArgInTypeAlias (implicit + explicit alias), testAnnotatedBadOneArgInPEP695TypeAlias, testAnnotatedBadOneArgInFunctionalForms (functional NamedTuple/TypedDict).

pytest mypy/test/testcheck.py -k "Annotated" -q
# 72 passed

Against the unpatched mypy/exprtotype.py the three new cases fail with Actual: empty (no error reported), which is the behaviour in the issue.

Fixes #22498

…orms

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.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@echoes666

Copy link
Copy Markdown
Author

Full type-checking suite against the patched tree:

pytest mypy/test/testcheck.py mypy/test/testsemanal.py -q
# 8923 passed, 34 skipped, 7 xfailed in 443.61s (7:23)

(The 34 skips and 7 xfails are pre-existing; the run includes the three new cases.)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant