MCPcopy Index your code
hub / github.com/RustPython/RustPython / _signature_get_partial

Function _signature_get_partial

Lib/inspect.py:1940–2028  ·  view source on GitHub ↗

Private helper to calculate how 'wrapped_sig' signature will look like after applying a 'functools.partial' object (or alike) on it.

(wrapped_sig, partial, extra_args=())

Source from the content-addressed store, hash-verified

1938
1939
1940def _signature_get_partial(wrapped_sig, partial, extra_args=()):
1941 """Private helper to calculate how 'wrapped_sig' signature will
1942 look like after applying a 'functools.partial' object (or alike)
1943 on it.
1944 """
1945
1946 old_params = wrapped_sig.parameters
1947 new_params = OrderedDict(old_params.items())
1948
1949 partial_args = partial.args or ()
1950 partial_keywords = partial.keywords or {}
1951
1952 if extra_args:
1953 partial_args = extra_args + partial_args
1954
1955 try:
1956 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1957 except TypeError as ex:
1958 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1959 raise ValueError(msg) from ex
1960
1961
1962 transform_to_kwonly = False
1963 for param_name, param in old_params.items():
1964 try:
1965 arg_value = ba.arguments[param_name]
1966 except KeyError:
1967 pass
1968 else:
1969 if param.kind is _POSITIONAL_ONLY:
1970 # If positional-only parameter is bound by partial,
1971 # it effectively disappears from the signature
1972 # However, if it is a Placeholder it is not removed
1973 # And also looses default value
1974 if arg_value is functools.Placeholder:
1975 new_params[param_name] = param.replace(default=_empty)
1976 else:
1977 new_params.pop(param_name)
1978 continue
1979
1980 if param.kind is _POSITIONAL_OR_KEYWORD:
1981 if param_name in partial_keywords:
1982 # This means that this parameter, and all parameters
1983 # after it should be keyword-only (and var-positional
1984 # should be removed). Here's why. Consider the following
1985 # function:
1986 # foo(a, b, *args, c):
1987 # pass
1988 #
1989 # "partial(foo, a='spam')" will have the following
1990 # signature: "(*, a='spam', b, c)". Because attempting
1991 # to call that partial with "(10, 20)" arguments will
1992 # raise a TypeError, saying that "a" argument received
1993 # multiple values.
1994 transform_to_kwonly = True
1995 # Set the new default value
1996 new_params[param_name] = param.replace(default=arg_value)
1997 else:

Callers 1

_signature_from_callableFunction · 0.85

Calls 8

popMethod · 0.95
move_to_endMethod · 0.95
valuesMethod · 0.95
OrderedDictClass · 0.90
bind_partialMethod · 0.80
itemsMethod · 0.45
formatMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected