MCPcopy
hub / github.com/python/mypy / check_argument_count

Method check_argument_count

mypy/checkexpr.py:2423–2501  ·  view source on GitHub ↗

Check that there is a value for all required arguments to a function. Also check that there are no duplicate values for arguments. Report found errors using 'messages' if it's not None. If 'messages' is given, 'context' must also be given. Return False if there were any err

(
        self,
        callee: CallableType,
        actual_types: list[Type],
        actual_kinds: list[ArgKind],
        actual_names: Sequence[str | None] | None,
        formal_to_actual: list[list[int]],
        context: Context | None,
        object_type: Type | None = None,
        callable_name: str | None = None,
    )

Source from the content-addressed store, hash-verified

2421 return True
2422
2423 def check_argument_count(
2424 self,
2425 callee: CallableType,
2426 actual_types: list[Type],
2427 actual_kinds: list[ArgKind],
2428 actual_names: Sequence[str | None] | None,
2429 formal_to_actual: list[list[int]],
2430 context: Context | None,
2431 object_type: Type | None = None,
2432 callable_name: str | None = None,
2433 ) -> bool:
2434 """Check that there is a value for all required arguments to a function.
2435
2436 Also check that there are no duplicate values for arguments. Report found errors
2437 using 'messages' if it's not None. If 'messages' is given, 'context' must also be given.
2438
2439 Return False if there were any errors. Otherwise return True
2440 """
2441 if context is None:
2442 # Avoid "is None" checks
2443 context = TempNode(AnyType(TypeOfAny.special_form))
2444
2445 # TODO(jukka): We could return as soon as we find an error if messages is None.
2446
2447 # Collect dict of all actual arguments matched to formal arguments, with occurrence count
2448 all_actuals: dict[int, int] = {}
2449 for actuals in formal_to_actual:
2450 for a in actuals:
2451 all_actuals[a] = all_actuals.get(a, 0) + 1
2452
2453 ok, is_unexpected_arg_error = self.check_for_extra_actual_arguments(
2454 callee, actual_types, actual_kinds, actual_names, all_actuals, context
2455 )
2456
2457 # Check for too many or few values for formals.
2458 for i, kind in enumerate(callee.arg_kinds):
2459 mapped_args = formal_to_actual[i]
2460 if kind.is_required() and not mapped_args and not is_unexpected_arg_error:
2461 # No actual for a mandatory formal
2462 if kind.is_positional():
2463 self.msg.too_few_arguments(callee, context, actual_names)
2464 if object_type and callable_name and "." in callable_name:
2465 self.missing_classvar_callable_note(object_type, callable_name, context)
2466 else:
2467 argname = callee.arg_names[i] or "?"
2468 self.msg.missing_named_argument(callee, context, argname)
2469 ok = False
2470 elif not kind.is_star() and is_duplicate_mapping(
2471 mapped_args, actual_types, actual_kinds
2472 ):
2473 if self.chk.in_checked_function() or isinstance(
2474 get_proper_type(actual_types[mapped_args[0]]), TupleType
2475 ):
2476 self.msg.duplicate_argument_value(callee, i, context)
2477 ok = False
2478 elif (
2479 kind.is_named()
2480 and mapped_args

Calls 15

TempNodeClass · 0.90
AnyTypeClass · 0.90
get_proper_typeFunction · 0.90
enumerateFunction · 0.85
is_duplicate_mappingFunction · 0.85
isinstanceFunction · 0.85
lenFunction · 0.85
sumFunction · 0.85
is_requiredMethod · 0.80
is_positionalMethod · 0.80

Tested by

no test coverage detected