55
66import mypy .errorcodes as codes
77from mypy import message_registry
8- from mypy .nodes import IntExpr , StrExpr , UnaryExpr
8+ from mypy .nodes import DictExpr , Expression , IntExpr , StrExpr , UnaryExpr
99from mypy .plugin import (
1010 AttributeContext ,
1111 ClassDefContext ,
7575 TypedDictType ,
7676 TypeOfAny ,
7777 TypeVarType ,
78- UninhabitedType ,
7978 UnionType ,
8079 get_proper_type ,
8180 get_proper_types ,
@@ -225,9 +224,12 @@ def typed_dict_get_callback(ctx: MethodContext) -> Type:
225224 return ctx .default_return_type
226225
227226 default_type : Type
227+ default_arg : Expression | None
228228 if len (ctx .arg_types ) <= 1 or not ctx .arg_types [1 ]:
229+ default_arg = None
229230 default_type = NoneType ()
230231 elif len (ctx .arg_types [1 ]) == 1 and len (ctx .args [1 ]) == 1 :
232+ default_arg = ctx .args [1 ][0 ]
231233 default_type = ctx .arg_types [1 ][0 ]
232234 else :
233235 return ctx .default_return_type
@@ -242,14 +244,10 @@ def typed_dict_get_callback(ctx: MethodContext) -> Type:
242244 output_types .append (value_type )
243245 else :
244246 # HACK to deal with get(key, {})
245- proper_default = get_proper_type (default_type )
246247 if (
247- isinstance (vt := get_proper_type (value_type ), TypedDictType )
248- and isinstance (proper_default , Instance )
249- and proper_default .type .fullname == "builtins.dict"
250- and len (proper_default .args ) == 2
251- and isinstance (get_proper_type (proper_default .args [0 ]), UninhabitedType )
252- and isinstance (get_proper_type (proper_default .args [1 ]), UninhabitedType )
248+ isinstance (default_arg , DictExpr )
249+ and len (default_arg .items ) == 0
250+ and isinstance (vt := get_proper_type (value_type ), TypedDictType )
253251 ):
254252 output_types .append (vt .copy_modified (required_keys = set ()))
255253 else :
0 commit comments