New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement an informative BoundArguments.__repr__
#66737
Comments
Can you propose a format for it? I'm not sure that including all arguments and their reprs is a good idea, as it will make BA's repr too long. |
Something like <BoundArguments: foo=bar, boo=baz> I have no problem with truncation when it gets too long. |
How about we just list bound arguments names, without values? |
Em, that kind of defeats the purpose of describing the object, doesn't it? |
Yes and no ;) You can have partially bound args, you can bind just one argument and use defaults for the rest, etc. I agree that it's not an ideal solution, but it is a sane compromise. |
I think the point of |
Context: inspect.signature returns an inspect.Signature instance. Its .bind and .bind_partial methods return ab inspect.BoundArguments instance with a standard <...@ 0x...> representation.
>>> ba = inspect.signature(foo).bind(5)
>>> ba
<inspect.BoundArguments object at 0x0000000002C94080>
BAs already have 3 data access methods
>>> ba.arguments # I believe this describes the object pretty fully
OrderedDict([('a', 5)])
>>> ba.args
(5,)
>>> ba.kwargs
{} A possible proposal for this case would be <BoundArguments: a=5>. However, this contains the same info as ba.arguments but in a less accessible form. Listing all parameters (and their default values if any) would be wrong as the additional info is not part of the object. The doc says to use Signature.parameters for full info, and it shows how to do that. So I am negative on the proposal. |
Another thing I proposed in python-ideas is to have To be honest I don't see the point in having to access |
Big -1 on __getitem__
I agree. I'll take a look. |
.arguments returns a mutable (ordered) dict that can be modified. See the example in the doc |
New changeset a444464a2e87 by Yury Selivanov in branch 'default': |
May be omit names for positionalarguments? >>> def foo(a, *args, b=10, **kwargs): pass
...
>>> inspect.signature(foo).bind(1, 2, 3, b=4, c=5)
<BoundArguments at 0xb6eee9ec (a=1, args=(2, 3), b=4, kwargs={'c': 5})> I think it would look better as: <BoundArguments (1, 2, 3, b=4, c=5)> |
Here is an implementation. |
Actually, I like the current repr (minus the object ID, I'll update the code). The thing about BoundArguments is that '.arguments' is the main property. You use it to add more stuff to *args & **kwargs, or to add/remove positional/keyword arguments. Seeing a flattened call signature in the repr won't help you with debugging. Perhaps, we can implement __str__ using your approach. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: