You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The recipe to inject default values in a BoundArguments instance is given in the doc, but it's not trivial. Furthermore, it's actually incomplete: it doesn't handle any star-arguments, e.g.:
>>> sig = inspect.signature(f)
>>> ba = sig.bind(2, d=4)
>>> for param in sig.parameters.values():
... if (param.name notin ba.arguments
... and param.default isnot param.empty):
... ba.arguments[param.name] = param.default
...
>>> ba.arguments
OrderedDict([('a', 2), ('d', 4), ('b', 5)])
See bpo-22998. The more complete and thus more complex example in the last message makes it look like including this in the library might be a good idea.
Well, the docs example only binds explicit defaults in function signature. Implicit defaults for *args and **kwargs (() and {}) aren't usually useful (in my opinion).
Do you guys have any good use case for such method? I use the Signature API extensively for argument types validation and for serialization of RPC calls, but I never needed this functionality from BoundArguments.
Well, the docs example only binds explicit defaults in function
signature. Implicit defaults for *args and **kwargs (() and {})
aren't usually useful (in my opinion).
When the defaults are filled I expect ba.arguments to be "complete",
that is have a value for every signature parameter. Otherwise I have to
special-case *args and **kwargs.
Do you guys have any good use case for such method?
A use case was given in bpo-22998.
My use case is JIT-compiling functions and function calls in Numba. We
reimplement the function calls ourselves, so need a complete mapping of
arguments to values.
> Do you guys have any good use case for such method?
A use case was given in bpo-22998.
My use case is JIT-compiling functions and function calls in Numba. We
reimplement the function calls ourselves, so need a complete mapping of
arguments to values.
This is a great use case ;-) Let's add it.
I propose the following method: BoundArguments.apply_defaults()
It will iterate through its parent Signature's parameters and assign default values to BoundArguments.arguments (when an arg is missing), including setting '()' for *args, and '{}' for **kwargs.
Activity
pitrou commentedon May 14, 2015
The recipe to inject default values in a BoundArguments instance is given in the doc, but it's not trivial. Furthermore, it's actually incomplete: it doesn't handle any star-arguments, e.g.:
ba['c'] would ideally contain the empty tuple.
pitrou commentedon May 14, 2015
My example forgets the function declaration, which is:
bitdancer commentedon May 14, 2015
See bpo-22998. The more complete and thus more complex example in the last message makes it look like including this in the library might be a good idea.
1st1 commentedon May 14, 2015
Well, the docs example only binds explicit defaults in function signature. Implicit defaults for *args and **kwargs (
()and{}) aren't usually useful (in my opinion).Do you guys have any good use case for such method? I use the Signature API extensively for argument types validation and for serialization of RPC calls, but I never needed this functionality from BoundArguments.
pitrou commentedon May 14, 2015
Le 14/05/2015 17:45, Yury Selivanov a écrit :
When the defaults are filled I expect ba.arguments to be "complete",
that is have a value for every signature parameter. Otherwise I have to
special-case *args and **kwargs.
A use case was given in bpo-22998.
My use case is JIT-compiling functions and function calls in Numba. We
reimplement the function calls ourselves, so need a complete mapping of
arguments to values.
1st1 commentedon May 14, 2015
This is a great use case ;-) Let's add it.
I propose the following method: BoundArguments.apply_defaults()
It will iterate through its parent Signature's parameters and assign default values to BoundArguments.arguments (when an arg is missing), including setting '()' for *args, and '{}' for **kwargs.
If you're OK with this, I can draft a patch.
pitrou commentedon May 14, 2015
That sounds good to me, thank you!
1st1 commentedon May 14, 2015
FWIW it wasn't as easy as I thought it would be :) You were right, docs example is very basic.
Please take a look at the attached patch.
python-dev commentedon May 16, 2015
New changeset ea61d8eb8a28 by Yury Selivanov in branch 'default':
bpo-24190: Add inspect.BoundArguments.apply_defaults() method.
https://hg.python.org/cpython/rev/ea61d8eb8a28
1st1 commentedon May 16, 2015
Thanks for the suggestion, Antoine!