Calls to `.get` on TypeVars bound to TypedDict narrow to `object`
Author: chadhpaineCreated Feb 5, 2024Updated Sep 21, 2026
Labelsbug
Bug Report
When using dict.get to access specific members of TypeVar entries bound to TypedDict, mypy always narrows the type to object where, without the TypeVar, we expect the union of the underlying type and None.
To Reproduce
from typing import TypedDict, TypeVar
class MyDict(TypedDict):
my_attributes: list[str]
def take_my_dict(d: MyDict):
reveal_type(d['my_attributes'])
reveal_type(d.get('my_attributes'))
MyDictT = TypeVar('MyDictT', bound=MyDict)
def take_my_typevar(d: MyDictT):
reveal_type(d['my_attributes'])
reveal_type(d.get('my_attributes'))Expected Behavior
main.py:7: note: Revealed type is "builtins.list[builtins.str]"
main.py:8: note: Revealed type is "Union[builtins.list[builtins.str], None]"
main.py:13: note: Revealed type is "builtins.list[builtins.str]"
main.py:14: note: Revealed type is "Union[builtins.list[builtins.str], None]"Actual Behavior
main.py:7: note: Revealed type is "builtins.list[builtins.str]"
main.py:8: note: Revealed type is "Union[builtins.list[builtins.str], None]"
main.py:13: note: Revealed type is "builtins.list[builtins.str]"
=> main.py:14: note: Revealed type is "builtins.object"Your Environment
- Mypy version used: 1.8.0
- Mypy command-line flags: n/a
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.12
Source: python/mypy