Skip to content

Commit e708813

Browse files
use original DictExpr test isntead of type-based
1 parent 27e50e2 commit e708813

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

mypy/plugins/default.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import mypy.errorcodes as codes
77
from mypy import message_registry
8-
from mypy.nodes import IntExpr, StrExpr, UnaryExpr
8+
from mypy.nodes import DictExpr, Expression, IntExpr, StrExpr, UnaryExpr
99
from mypy.plugin import (
1010
AttributeContext,
1111
ClassDefContext,
@@ -75,7 +75,6 @@
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

Comments
 (0)