[issue24190] BoundArguments facility to inject defaults

2015-05-16 Thread Yury Selivanov

Yury Selivanov added the comment:

Thanks for the suggestion, Antoine!

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-16 Thread Roundup Robot

Roundup Robot added the comment:

New changeset ea61d8eb8a28 by Yury Selivanov in branch 'default':
Issue 24190: Add inspect.BoundArguments.apply_defaults() method.
https://hg.python.org/cpython/rev/ea61d8eb8a28

--
nosy: +python-dev

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Yury Selivanov

Yury Selivanov added the comment:

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.

--
assignee:  -> yselivanov
keywords: +patch
stage:  -> patch review
Added file: http://bugs.python.org/file39372/sig_ba_default.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Yury Selivanov

Yury Selivanov added the comment:

>> 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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Antoine Pitrou

Antoine Pitrou added the comment:

That sounds good to me, thank you!

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Antoine Pitrou

Antoine Pitrou added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Yury Selivanov

Yury Selivanov added the comment:

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.

--
nosy: +brett.cannon, larry

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread R. David Murray

R. David Murray added the comment:

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.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Antoine Pitrou

Antoine Pitrou added the comment:

My example forgets the function declaration, which is:

>>> def f(a, b=5, *c, d=5): pass
...

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24190] BoundArguments facility to inject defaults

2015-05-14 Thread Antoine Pitrou

New submission from Antoine Pitrou:

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.

--
components: Library (Lib)
messages: 243167
nosy: pitrou, yselivanov
priority: normal
severity: normal
status: open
title: BoundArguments facility to inject defaults
type: enhancement
versions: Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com