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)
| 1422 | "was" if given == 1 and not kwonly_given else "were")) |
| 1423 | |
| 1424 | def getcallargs(func, /, *positional, **named): |
| 1425 | """Get the mapping of arguments to values. |
| 1426 | |
| 1427 | A dict is returned, with keys the function argument names (including the |
| 1428 | names of the * and ** arguments, if any), and values the respective bound |
| 1429 | values from 'positional' and 'named'.""" |
| 1430 | spec = getfullargspec(func) |
| 1431 | args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec |
| 1432 | f_name = func.__name__ |
| 1433 | arg2value = {} |
| 1434 | |
| 1435 | |
| 1436 | if ismethod(func) and func.__self__ is not None: |
| 1437 | # implicit 'self' (or 'cls' for classmethods) argument |
| 1438 | positional = (func.__self__,) + positional |
| 1439 | num_pos = len(positional) |
| 1440 | num_args = len(args) |
| 1441 | num_defaults = len(defaults) if defaults else 0 |
| 1442 | |
| 1443 | n = min(num_pos, num_args) |
| 1444 | for i in range(n): |
| 1445 | arg2value[args[i]] = positional[i] |
| 1446 | if varargs: |
| 1447 | arg2value[varargs] = tuple(positional[n:]) |
| 1448 | possible_kwargs = set(args + kwonlyargs) |
| 1449 | if varkw: |
| 1450 | arg2value[varkw] = {} |
| 1451 | for kw, value in named.items(): |
| 1452 | if kw not in possible_kwargs: |
| 1453 | if not varkw: |
| 1454 | raise TypeError("%s() got an unexpected keyword argument %r" % |
| 1455 | (f_name, kw)) |
| 1456 | arg2value[varkw][kw] = value |
| 1457 | continue |
| 1458 | if kw in arg2value: |
| 1459 | raise TypeError("%s() got multiple values for argument %r" % |
| 1460 | (f_name, kw)) |
| 1461 | arg2value[kw] = value |
| 1462 | if num_pos > num_args and not varargs: |
| 1463 | _too_many(f_name, args, kwonlyargs, varargs, num_defaults, |
| 1464 | num_pos, arg2value) |
| 1465 | if num_pos < num_args: |
| 1466 | req = args[:num_args - num_defaults] |
| 1467 | for arg in req: |
| 1468 | if arg not in arg2value: |
| 1469 | _missing_arguments(f_name, req, True, arg2value) |
| 1470 | for i, arg in enumerate(args[num_args - num_defaults:]): |
| 1471 | if arg not in arg2value: |
| 1472 | arg2value[arg] = defaults[i] |
| 1473 | missing = 0 |
| 1474 | for kwarg in kwonlyargs: |
| 1475 | if kwarg not in arg2value: |
| 1476 | if kwonlydefaults and kwarg in kwonlydefaults: |
| 1477 | arg2value[kwarg] = kwonlydefaults[kwarg] |
| 1478 | else: |
| 1479 | missing += 1 |
| 1480 | if missing: |
| 1481 | _missing_arguments(f_name, kwonlyargs, False, arg2value) |
nothing calls this directly
no test coverage detected