mypy
mypy copied to clipboard
TypedDict.setdefault gives error when type is a TypeVar
Bug Report
When a function argument is annotated using a TypeVar with a bound that is a TypedDict, using setdefault on it leads to an error.
To Reproduce
from typing import TypedDict, TypeVar
class TD(TypedDict, total=False):
foo: int
bar: bool
_T = TypeVar("_T", bound=TD)
def func(td: _T) -> None:
td.setdefault("bar", True)
Actual Behavior
testit.py:10: error: Argument 1 to "setdefault" of "TypedDict" has incompatible type "str"; expected "Never" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 1.11.1 (compiled: yes)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): no config file - Python version used: 3.12.3