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

BoundArguments facility to inject defaults #68378

Closed
pitrou opened this issue May 14, 2015 · 10 comments
Closed

BoundArguments facility to inject defaults #68378

pitrou opened this issue May 14, 2015 · 10 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented May 14, 2015

BPO 24190
Nosy @brettcannon, @pitrou, @larryhastings, @bitdancer, @1st1
Files
  • sig_ba_default.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-16.17:45:53.125>
    created_at = <Date 2015-05-14.10:20:00.056>
    labels = ['type-feature', 'library']
    title = 'BoundArguments facility to inject defaults'
    updated_at = <Date 2015-05-16.17:45:53.124>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2015-05-16.17:45:53.124>
    actor = 'yselivanov'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2015-05-16.17:45:53.125>
    closer = 'yselivanov'
    components = ['Library (Lib)']
    creation = <Date 2015-05-14.10:20:00.056>
    creator = 'pitrou'
    dependencies = []
    files = ['39372']
    hgrepos = []
    issue_num = 24190
    keywords = ['patch']
    message_count = 10.0
    messages = ['243167', '243170', '243177', '243200', '243202', '243204', '243205', '243210', '243341', '243342']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'pitrou', 'larry', 'r.david.murray', 'python-dev', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24190'
    versions = ['Python 3.5']

    @pitrou
    Copy link
    Member Author

    pitrou commented May 14, 2015

    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.

    @pitrou pitrou added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 14, 2015
    @pitrou
    Copy link
    Member Author

    pitrou commented May 14, 2015

    My example forgets the function declaration, which is:

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

    @bitdancer
    Copy link
    Member

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

    @1st1
    Copy link
    Member

    1st1 commented May 14, 2015

    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.

    @pitrou
    Copy link
    Member Author

    pitrou commented May 14, 2015

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

    @1st1
    Copy link
    Member

    1st1 commented May 14, 2015

    > Do you guys have any good use case for such method?

    A use case was given in bpo-22998.
    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.

    @pitrou
    Copy link
    Member Author

    pitrou commented May 14, 2015

    That sounds good to me, thank you!

    @1st1
    Copy link
    Member

    1st1 commented May 14, 2015

    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.

    @1st1 1st1 self-assigned this May 14, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 16, 2015

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

    @1st1
    Copy link
    Member

    1st1 commented May 16, 2015

    Thanks for the suggestion, Antoine!

    @1st1 1st1 closed this as completed May 16, 2015
    @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