This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: BoundArguments facility to inject defaults
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: brett.cannon, larry, pitrou, python-dev, r.david.murray, yselivanov
Priority: normal Keywords: patch

Created on 2015-05-14 10:20 by pitrou, last changed 2022-04-11 14:58 by admin. 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2015-05-14 16:00
That sounds good to me, thank you!
msg243210 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) 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) (Python triager) 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) * (Python committer) Date: 2015-05-16 17:45
Thanks for the suggestion, Antoine!
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68378
2015-05-16 17:45:53yselivanovsetstatus: open -> closed
resolution: fixed
messages: + msg243342

stage: patch review -> resolved
2015-05-16 17:45:27python-devsetnosy: + python-dev
messages: + msg243341
2015-05-14 16:56:09yselivanovsetfiles: + sig_ba_default.patch
messages: + msg243210

assignee: yselivanov
keywords: + patch
stage: patch review
2015-05-14 16:00:45pitrousetmessages: + msg243205
2015-05-14 15:59:47yselivanovsetmessages: + msg243204
2015-05-14 15:53:41pitrousetmessages: + msg243202
2015-05-14 15:45:40yselivanovsetnosy: + brett.cannon, larry
messages: + msg243200
2015-05-14 11:33:04r.david.murraysetnosy: + r.david.murray
messages: + msg243177
2015-05-14 10:22:29pitrousetmessages: + msg243170
2015-05-14 10:20:00pitroucreate