MCPcopy Index your code
hub / github.com/python/cpython / getcallargs

Function getcallargs

Lib/inspect.py:1448–1506  ·  view source on GitHub ↗

Get the mapping of arguments to values. A dict is returned, with keys the function argument names (including the names of the * and ** arguments, if any), and values the respective bound values from 'positional' and 'named'.

(func, /, *positional, **named)

Source from the content-addressed store, hash-verified

1446 "was" if given == 1 and not kwonly_given else "were"))
1447
1448def getcallargs(func, /, *positional, **named):
1449 """Get the mapping of arguments to values.
1450
1451 A dict is returned, with keys the function argument names (including the
1452 names of the * and ** arguments, if any), and values the respective bound
1453 values from 'positional' and 'named'."""
1454 spec = getfullargspec(func)
1455 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1456 f_name = func.__name__
1457 arg2value = {}
1458
1459
1460 if ismethod(func) and func.__self__ is not None:
1461 # implicit 'self' (or 'cls' for classmethods) argument
1462 positional = (func.__self__,) + positional
1463 num_pos = len(positional)
1464 num_args = len(args)
1465 num_defaults = len(defaults) if defaults else 0
1466
1467 n = min(num_pos, num_args)
1468 for i in range(n):
1469 arg2value[args[i]] = positional[i]
1470 if varargs:
1471 arg2value[varargs] = tuple(positional[n:])
1472 possible_kwargs = set(args + kwonlyargs)
1473 if varkw:
1474 arg2value[varkw] = {}
1475 for kw, value in named.items():
1476 if kw not in possible_kwargs:
1477 if not varkw:
1478 raise TypeError("%s() got an unexpected keyword argument %r" %
1479 (f_name, kw))
1480 arg2value[varkw][kw] = value
1481 continue
1482 if kw in arg2value:
1483 raise TypeError("%s() got multiple values for argument %r" %
1484 (f_name, kw))
1485 arg2value[kw] = value
1486 if num_pos > num_args and not varargs:
1487 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1488 num_pos, arg2value)
1489 if num_pos < num_args:
1490 req = args[:num_args - num_defaults]
1491 for arg in req:
1492 if arg not in arg2value:
1493 _missing_arguments(f_name, req, True, arg2value)
1494 for i, arg in enumerate(args[num_args - num_defaults:]):
1495 if arg not in arg2value:
1496 arg2value[arg] = defaults[i]
1497 missing = 0
1498 for kwarg in kwonlyargs:
1499 if kwarg not in arg2value:
1500 if kwonlydefaults and kwarg in kwonlydefaults:
1501 arg2value[kwarg] = kwonlydefaults[kwarg]
1502 else:
1503 missing += 1
1504 if missing:
1505 _missing_arguments(f_name, kwonlyargs, False, arg2value)

Callers

nothing calls this directly

Calls 7

getfullargspecFunction · 0.85
ismethodFunction · 0.85
setFunction · 0.85
_too_manyFunction · 0.85
_missing_argumentsFunction · 0.85
enumerateFunction · 0.85
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…