Skip to content

BoundArguments facility to inject defaults #68378

Closed

Description

@pitrou
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']

    Activity

    1. pitrou commented on May 14, 2015

      @pitrou
      MemberAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on May 14, 2015
    3. pitrou commented on May 14, 2015

      @pitrou
      MemberAuthor

      My example forgets the function declaration, which is:

      >>> def f(a, b=5, *c, d=5): pass
      ...
    4. bitdancer commented on May 14, 2015

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

    5. 1st1 commented on May 14, 2015

      @1st1
      Member

      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.

    6. pitrou commented on May 14, 2015

      @pitrou
      MemberAuthor

      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.

    7. 1st1 commented on May 14, 2015

      @1st1
      Member

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

    8. pitrou commented on May 14, 2015

      @pitrou
      MemberAuthor

      That sounds good to me, thank you!

    9. 1st1 commented on May 14, 2015

      @1st1
      Member

      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.

    10. self-assigned this
      on May 14, 2015
    11. python-dev commented on May 16, 2015

      python-devmannequin
      Mannequin

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

    12. 1st1 commented on May 16, 2015

      @1st1
      Member

      Thanks for the suggestion, Antoine!

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions