Skip to content
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

Closed
cool-RR mannequin opened this issue Oct 3, 2014 · 14 comments
Closed

Implement an informative BoundArguments.__repr__ #66737

cool-RR mannequin opened this issue Oct 3, 2014 · 14 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@cool-RR
Copy link
Mannequin

cool-RR mannequin commented Oct 3, 2014

BPO 22547
Nosy @terryjreedy, @ncoghlan, @larryhastings, @cool-RR, @serhiy-storchaka, @1st1
Files
  • BoundArguments_repr_alt.patch
  • 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:

    assignee = 'https://github.com/1st1'
    closed_at = <Date 2015-05-14.22:47:54.408>
    created_at = <Date 2014-10-03.13:19:59.397>
    labels = ['type-feature', 'library']
    title = 'Implement an informative `BoundArguments.__repr__`'
    updated_at = <Date 2015-05-15.16:42:36.815>
    user = 'https://github.com/cool-RR'

    bugs.python.org fields:

    activity = <Date 2015-05-15.16:42:36.815>
    actor = 'yselivanov'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2015-05-14.22:47:54.408>
    closer = 'yselivanov'
    components = ['Library (Lib)']
    creation = <Date 2014-10-03.13:19:59.397>
    creator = 'cool-RR'
    dependencies = []
    files = ['39380']
    hgrepos = []
    issue_num = 22547
    keywords = ['patch']
    message_count = 14.0
    messages = ['228332', '228333', '228334', '228335', '228337', '228338', '228376', '228377', '228378', '228381', '243228', '243249', '243250', '243278']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'larry', 'cool-RR', 'python-dev', 'serhiy.storchaka', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue22547'
    versions = ['Python 3.5']

    @cool-RR cool-RR mannequin added the stdlib Python modules in the Lib dir label Oct 3, 2014
    @1st1
    Copy link
    Member

    1st1 commented Oct 3, 2014

    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.

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented Oct 3, 2014

    Something like <BoundArguments: foo=bar, boo=baz>

    I have no problem with truncation when it gets too long.

    @1st1
    Copy link
    Member

    1st1 commented Oct 3, 2014

    How about we just list bound arguments names, without values?

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented Oct 3, 2014

    Em, that kind of defeats the purpose of describing the object, doesn't it?

    @1st1
    Copy link
    Member

    1st1 commented Oct 3, 2014

    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.

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented Oct 3, 2014

    I think the point of __init__ is to know what the object is, and if you're hiding the values then someone has to access the attributes to find out the values and that sucks.

    @terryjreedy
    Copy link
    Member

    Context: inspect.signature returns an inspect.Signature instance. Its .bind and .bind_partial methods return ab inspect.BoundArguments instance with a standard <...@ 0x...> representation.

    >> import inspect
    >> def foo(a, b=10): pass

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

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented Oct 3, 2014

    Another thing I proposed in python-ideas is to have __getitem__ delegate to .arguments, so this proposal is similar in spirit, because I want to have __repr__ show information from .arguments.

    To be honest I don't see the point in having to access .arguments manually for anything. BoundArguments is all about arguments. I don't see a reason to waste anyone's time with accessing an attribute on it to get to the arguments. There isn't much more to get on the thing. So this applies both to repr and getitem.

    @1st1
    Copy link
    Member

    1st1 commented Oct 3, 2014

    Another thing I proposed in python-ideas is to have __getitem__ delegate to .arguments, so this proposal is similar in spirit, because I want to have __repr__ show information from .arguments.

    Big -1 on __getitem__

    To be honest I don't see the point in having to access .arguments manually for anything. BoundArguments is all about arguments.

    I agree. I'll take a look.

    @1st1 1st1 self-assigned this Oct 3, 2014
    @terryjreedy
    Copy link
    Member

    .arguments returns a mutable (ordered) dict that can be modified. See the example in the doc
    https://docs.python.org/3.4/library/inspect.html#inspect.BoundArguments

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 14, 2015

    New changeset a444464a2e87 by Yury Selivanov in branch 'default':
    bpo-22547: Implement informative __repr__ for inspect.BoundArguments
    https://hg.python.org/cpython/rev/a444464a2e87

    @1st1 1st1 closed this as completed May 14, 2015
    @1st1 1st1 added the type-feature A feature request or enhancement label May 14, 2015
    @serhiy-storchaka
    Copy link
    Member

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

    @serhiy-storchaka
    Copy link
    Member

    Here is an implementation.

    @1st1
    Copy link
    Member

    1st1 commented May 15, 2015

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants