python / python/mypy

Incorrect error when argument missing from function call unpacking TypedDict

Open
#12,755 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-calls topic-typed-dict
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When supplying arguments by unpacking a TypedDict, and the TypedDict does not supply all the parameters, mypy seems to treat it as only supplying the first parameter, even if that is the one that is missing.

To Reproduce

from typing import TypedDict

def my_function(foo: int, bar: int) -> None:
    pass

class MyTypedDict(TypedDict):
    # foo: int
    bar: int

    
a: MyTypedDict = {'bar': 1}

my_function(**a) # mypy - error: Missing positional argument "bar" in call to "my_function"
my_function(bar=1) # mypy - error: Missing positional argument "foo" in call to "my_function"

https://mypy-play.net/?mypy=0.931&python=3.11&flags=strict&gist=c72be0814d2e7684453f2a8825f2e986

Expected Behavior

The line my_function(**a) should result in mypy error error: Missing positional argument "foo" in call to "my_function", as it only supplies bar.

Actual Behavior

The line my_function(**a) resulted in mypy error error: Missing positional argument "bar" in call to "my_function".

When executed, python gave this error:

TypeError: my_function() missing 1 required positional argument: 'foo'

https://github.com/python/mypy/issues/11753

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied mypy-play TypedDict and function-unpacking reproducer and compare mypy's diagnostic with Python's TypeError. Trace the handling of **a calls and TypedDict keys, then add coverage showing that the missing argument is foo rather than bar.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.