TypedDict totality ignored when unpacked for function call
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When unpacking a typeddict for a function call, mypy doesn't seem to check the totality of the typeddict, and instead assumes the keys are all present.
from typing import TypedDict
def my_function(foo: int, bar: int, baz: str) -> None:
pass
class MyTypedDict(TypedDict, total=False):
foo: int
bar: int
baz: str
a: MyTypedDict = {'bar': 1}
my_function(**a) # Should be error here, as a doesn't contain foo or baz
https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=5444e1100000a8f1678fe9cbcfc23921
Expected Behavior
Mypy should not assume that keys are present when the TypedDict has total=False, and treat it as potentially having any combination of they keys present.
Actual Behavior
Mypy allows this, but when executed it gives the following error:
Traceback (most recent call last):
File "<path>\typeddict1.py", line 17, in <module>
my_function(**a) # Should be error here, as a doesn't contain foo or baz
TypeError: my_function() missing 2 required positional arguments: 'foo' and 'baz'
#11753
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the TypedDict total=False unpacking example from the issue or its mypy-play link, then trace the function-call argument checking for TypedDict unpacking. Done means mypy reports an error when optional keys may be missing, while preserving valid behavior for required keys.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100