python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.41k stars 2.82k forks source link

MyPy fails to check `TypedDict.update` call with only explicit keyword arguments #17750

Open gareth-rees opened 1 month ago

gareth-rees commented 1 month ago

Bug Report

Calls to TypedDict.update don't type-check, even if all the updates are specified using keyword arguments.

To Reproduce

Type-check this program:

from typing import TypedDict

class Args(TypedDict, total=False):
    a: int
    b: str

args = Args()
args.update(a=123, b="abc")

Expected Behavior

The code should type-check, since all arguments to TypedDict.update were passed as keyword arguments, and the types of the arguments are correct.

Actual Behavior

$ mypy test.py
test.py:8: error: Unexpected keyword argument "a" for "update" of "TypedDict"  [call-arg]
/lib/python3.10/site-packages/mypy/typeshed/stdlib/typing.pyi:894: note: "update" of "TypedDict" defined here
test.py:8: error: Unexpected keyword argument "b" for "update" of "TypedDict"  [call-arg]
/lib/python3.10/site-packages/mypy/typeshed/stdlib/typing.pyi:894: note: "update" of "TypedDict" defined here
Found 2 errors in 1 file (checked 1 source file)

Your Environment

Alexandre-SCHOEPP commented 3 weeks ago

Had the same issue on 1.10.1 on python 3.12 just now. Pylance seems to handle this specific case better.