MCPcopy Index your code
hub / github.com/python/mypy / apply_function_plugin

Method apply_function_plugin

mypy/checkexpr.py:1214–1285  ·  view source on GitHub ↗

Use special case logic to infer the return type of a specific named function/method. Caller must ensure that a plugin hook exists. There are two different cases: - If object_type is None, the caller must ensure that a function hook exists for fullname. - If object

(
        self,
        callee: CallableType,
        arg_kinds: list[ArgKind],
        arg_types: list[Type],
        arg_names: Sequence[str | None] | None,
        formal_to_actual: list[list[int]],
        args: list[Expression],
        fullname: str,
        object_type: Type | None,
        context: Context,
    )

Source from the content-addressed store, hash-verified

1212 return None
1213
1214 def apply_function_plugin(
1215 self,
1216 callee: CallableType,
1217 arg_kinds: list[ArgKind],
1218 arg_types: list[Type],
1219 arg_names: Sequence[str | None] | None,
1220 formal_to_actual: list[list[int]],
1221 args: list[Expression],
1222 fullname: str,
1223 object_type: Type | None,
1224 context: Context,
1225 ) -> Type:
1226 """Use special case logic to infer the return type of a specific named function/method.
1227
1228 Caller must ensure that a plugin hook exists. There are two different cases:
1229
1230 - If object_type is None, the caller must ensure that a function hook exists
1231 for fullname.
1232 - If object_type is not None, the caller must ensure that a method hook exists
1233 for fullname.
1234
1235 Return the inferred return type.
1236 """
1237 num_formals = len(callee.arg_types)
1238 formal_arg_types: list[list[Type]] = [[] for _ in range(num_formals)]
1239 formal_arg_exprs: list[list[Expression]] = [[] for _ in range(num_formals)]
1240 formal_arg_names: list[list[str | None]] = [[] for _ in range(num_formals)]
1241 formal_arg_kinds: list[list[ArgKind]] = [[] for _ in range(num_formals)]
1242 for formal, actuals in enumerate(formal_to_actual):
1243 for actual in actuals:
1244 formal_arg_types[formal].append(arg_types[actual])
1245 formal_arg_exprs[formal].append(args[actual])
1246 if arg_names:
1247 formal_arg_names[formal].append(arg_names[actual])
1248 else:
1249 formal_arg_names[formal].append(None)
1250 formal_arg_kinds[formal].append(arg_kinds[actual])
1251
1252 if object_type is None:
1253 # Apply function plugin
1254 callback = self.plugin.get_function_hook(fullname)
1255 assert callback is not None # Assume that caller ensures this
1256 return callback(
1257 FunctionContext(
1258 arg_types=formal_arg_types,
1259 arg_kinds=formal_arg_kinds,
1260 callee_arg_names=callee.arg_names,
1261 arg_names=formal_arg_names,
1262 default_return_type=callee.ret_type,
1263 args=formal_arg_exprs,
1264 context=context,
1265 api=self.chk,
1266 )
1267 )
1268 else:
1269 # Apply method plugin
1270 method_callback = self.plugin.get_method_hook(fullname)
1271 assert method_callback is not None # Assume that caller ensures this

Callers 1

check_callable_callMethod · 0.95

Calls 9

FunctionContextClass · 0.90
get_proper_typeFunction · 0.90
MethodContextClass · 0.90
lenFunction · 0.85
rangeClass · 0.85
enumerateFunction · 0.85
appendMethod · 0.80
get_function_hookMethod · 0.45
get_method_hookMethod · 0.45

Tested by

no test coverage detected