Issue24190
Created on 2015-05-14 10:20 by pitrou, last changed 2015-05-16 17:45 by yselivanov. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| sig_ba_default.patch | yselivanov, 2015-05-14 16:56 | review | ||
| Messages (10) | |||
|---|---|---|---|
| msg243167 - (view) | Author: Antoine Pitrou (pitrou) * | Date: 2015-05-14 10:19 | |
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 not in ba.arguments
... and param.default is not param.empty):
... ba.arguments[param.name] = param.default
...
>>> ba.arguments
OrderedDict([('a', 2), ('d', 4), ('b', 5)])
ba['c'] would ideally contain the empty tuple.
|
|||
| msg243170 - (view) | Author: Antoine Pitrou (pitrou) * | Date: 2015-05-14 10:22 | |
My example forgets the function declaration, which is: >>> def f(a, b=5, *c, d=5): pass ... |
|||
| msg243177 - (view) | Author: R. David Murray (r.david.murray) * | Date: 2015-05-14 11:33 | |
See issue 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. |
|||
| msg243200 - (view) | Author: Yury Selivanov (yselivanov) * | Date: 2015-05-14 15:45 | |
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.
|
|||
| msg243202 - (view) | Author: Antoine Pitrou (pitrou) * | Date: 2015-05-14 15:53 | |
Le 14/05/2015 17:45, Yury Selivanov a écrit :
>
> 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 issue22998.
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.
|
|||
| msg243204 - (view) | Author: Yury Selivanov (yselivanov) * | Date: 2015-05-14 15:59 | |
>> Do you guys have any good use case for such method? > A use case was given in issue22998. > 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. If you're OK with this, I can draft a patch. |
|||
| msg243205 - (view) | Author: Antoine Pitrou (pitrou) * | Date: 2015-05-14 16:00 | |
That sounds good to me, thank you! |
|||
| msg243210 - (view) | Author: Yury Selivanov (yselivanov) * | Date: 2015-05-14 16:56 | |
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. |
|||
| msg243341 - (view) | Author: Roundup Robot (python-dev) | Date: 2015-05-16 17:45 | |
New changeset ea61d8eb8a28 by Yury Selivanov in branch 'default': Issue 24190: Add inspect.BoundArguments.apply_defaults() method. https://hg.python.org/cpython/rev/ea61d8eb8a28 |
|||
| msg243342 - (view) | Author: Yury Selivanov (yselivanov) * | Date: 2015-05-16 17:45 | |
Thanks for the suggestion, Antoine! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2015-05-16 17:45:53 | yselivanov | set | status: open -> closed resolution: fixed messages: + msg243342 stage: patch review -> resolved |
| 2015-05-16 17:45:27 | python-dev | set | nosy:
+ python-dev messages: + msg243341 |
| 2015-05-14 16:56:09 | yselivanov | set | files:
+ sig_ba_default.patch messages: + msg243210 assignee: yselivanov |
| 2015-05-14 16:00:45 | pitrou | set | messages: + msg243205 |
| 2015-05-14 15:59:47 | yselivanov | set | messages: + msg243204 |
| 2015-05-14 15:53:41 | pitrou | set | messages: + msg243202 |
| 2015-05-14 15:45:40 | yselivanov | set | nosy:
+ brett.cannon, larry messages: + msg243200 |
| 2015-05-14 11:33:04 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg243177 |
| 2015-05-14 10:22:29 | pitrou | set | messages: + msg243170 |
| 2015-05-14 10:20:00 | pitrou | create | |