MCPcopy Create free account
hub / github.com/python/mypy / remove_unpack_kwargs

Method remove_unpack_kwargs

mypy/semanal.py:1109–1132  ·  view source on GitHub ↗
(self, defn: FuncDef, typ: CallableType)

Source from the content-addressed store, hash-verified

1107 self.pop_type_args(defn.type_args)
1108
1109 def remove_unpack_kwargs(self, defn: FuncDef, typ: CallableType) -> CallableType:
1110 if not typ.arg_kinds or typ.arg_kinds[-1] is not ArgKind.ARG_STAR2:
1111 return typ
1112 last_type = typ.arg_types[-1]
1113 if not isinstance(last_type, UnpackType):
1114 return typ
1115 p_last_type = get_proper_type(last_type.type)
1116 if not isinstance(p_last_type, TypedDictType):
1117 self.fail("Unpack item in ** parameter must be a TypedDict", last_type)
1118 new_arg_types = typ.arg_types[:-1] + [AnyType(TypeOfAny.from_error)]
1119 return typ.copy_modified(arg_types=new_arg_types)
1120 overlap = set(typ.arg_names) & set(p_last_type.items)
1121 # It is OK for TypedDict to have a key named 'kwargs'.
1122 overlap.discard(typ.arg_names[-1])
1123 if overlap:
1124 overlapped = ", ".join([f'"{name}"' for name in sorted(filter(None, overlap))])
1125 self.fail(
1126 f"Overlap between parameter names and ** TypedDict items: {overlapped}", defn
1127 )
1128 new_arg_types = typ.arg_types[:-1] + [AnyType(TypeOfAny.from_error)]
1129 return typ.copy_modified(arg_types=new_arg_types)
1130 # OK, everything looks right now, mark the callable type as using unpack.
1131 new_arg_types = typ.arg_types[:-1] + [p_last_type]
1132 return typ.copy_modified(arg_types=new_arg_types, unpack_kwargs=True)
1133
1134 def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: bool) -> None:
1135 """Check basic signature validity and tweak annotation of self/cls argument."""

Callers 1

analyze_func_defMethod · 0.95

Calls 9

failMethod · 0.95
get_proper_typeFunction · 0.90
AnyTypeClass · 0.90
isinstanceFunction · 0.85
setClass · 0.85
sortedFunction · 0.85
discardMethod · 0.80
copy_modifiedMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected