Repository navigation
Conversation
…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.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Author
|
Full type-checking suite against the patched tree: (The 34 skips and 7 xfails are pre-existing; the run includes the three new cases.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
exprtotypestripsAnnotated[X, ...]down toXwhenever the subscript has at least one argument: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 tointbeforetypeanalcan reject it. The arity check intypeanal(len(t.args) < 2) only runs for annotations that don't go through this path, so only plain variable/parameter annotations report the error:CPython raises
TypeErrorfor 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 intypeanal.Test plan
New cases in
test-data/unit/check-annotated.test:testAnnotatedBadOneArgInTypeAlias(implicit + explicit alias),testAnnotatedBadOneArgInPEP695TypeAlias,testAnnotatedBadOneArgInFunctionalForms(functionalNamedTuple/TypedDict).Against the unpatched
mypy/exprtotype.pythe three new cases fail withActual:empty (no error reported), which is the behaviour in the issue.Fixes #22498