-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
I'm playing around with TypedDict
and the **
operator, trying to use mypy to help avoid typos or type errors when constructing some JSON-type data. I am aware from reading some other bug reports and articles that mypy does not currently understand the keys created by a **
expansion as being literals, even if it could confirm the keys are know (e.g. because they come from a function that returns a TypedDict
.
However, i was a bit surprised to find that if i do use **
, mypy does not notice if i also include an invalid literal key, or use the wrong type for a valid literal key. It seems to me like it should be possible to tell that a mistake is being made, even if we don't know whether the key might eventually be overwritten.
To Reproduce
from typing import TypedDict, Literal
class FlavourProfile(TypedDict, total=False):
sweetness: Literal["lots", "some", "none"]
tartness: int
my_fruit: FlavourProfile = {
"sweetness": "very", # incompatible type
"grapelikeness": "some", # extra key
**FlavourProfile(tartness=1), # type: ignore[misc]
}
Expected Behavior
- I thought
mypy
would ignore the errorerror: Expected TypedDict key to be string literal [misc]
- I still expected
mypy
to notice issues with the literal keys and values i specified
Actual Behavior
Mypy sees no errors at all: Success: no issues found in 1 source file
Your Environment
- Mypy version used: 0.991
- Mypy command-line flags:
python -m mypy scratch.py
- Mypy configuration options from
mypy.ini
(and other config files): - Python version used: 3.10.6
Additional Discussion Thoughts
- i noticed that if i comment out the line with
**
, mypy only tells me about the second error (extra key) and not the first error. is the issue that only one of the typing errors is reported, and since i'm ignoring the last error it has nothing left to report?