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

inspect.getcallargs() #47385

Closed
gsakkis mannequin opened this issue Jun 18, 2008 · 10 comments
Closed

inspect.getcallargs() #47385

gsakkis mannequin opened this issue Jun 18, 2008 · 10 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@gsakkis
Copy link
Mannequin

gsakkis mannequin commented Jun 18, 2008

BPO 3135
Nosy @benjaminp
Files
  • getcallargs.patch
  • getcallargs2.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 = None
    closed_at = <Date 2010-03-30.17:59:44.555>
    created_at = <Date 2008-06-18.22:16:52.950>
    labels = ['type-feature', 'library']
    title = 'inspect.getcallargs()'
    updated_at = <Date 2010-03-30.17:59:44.553>
    user = 'https://bugs.python.org/gsakkis'

    bugs.python.org fields:

    activity = <Date 2010-03-30.17:59:44.553>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-03-30.17:59:44.555>
    closer = 'benjamin.peterson'
    components = ['Library (Lib)']
    creation = <Date 2008-06-18.22:16:52.950>
    creator = 'gsakkis'
    dependencies = []
    files = ['16587', '16591']
    hgrepos = []
    issue_num = 3135
    keywords = ['patch']
    message_count = 10.0
    messages = ['68378', '87787', '87788', '101303', '101326', '101327', '101339', '101341', '101348', '101951']
    nosy_count = 3.0
    nosy_names = ['gsakkis', 'benjamin.peterson', 'Alexander.Belopolsky']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3135'
    versions = ['Python 2.7']

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented Jun 18, 2008

    I'd like to propose a new function for inclusion to the inspect module
    -- getcallargs(func, *args, **kwds) -- that returns a dict which maps
    the formal arguments of a function (or other callable) to the values
    passed as args and kwds, just as Python has to do when calling
    func(*args, **kwds). For example:

    >>> def func(a, b='foo', c=None, *x, **y):
    ...         pass
    >>> sorted(getcallargs(func, 5, z=3, b=2).items())
     [('a', 5), ('b', 2), ('c', None), ('x', ()), ('y', {'z': 3})]

    This is handy when writing decorators, or more generally when one would
    want to do some minimal type checking without actually calling the function.

    I have posted a recipe at
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/551779; I can
    clean it up and submit a proper patch if it's deemed useful enough for
    the stdlib.

    @gsakkis gsakkis mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 18, 2008
    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented May 15, 2009

    I updated the recipe to also return a missing_args tuple - the tuple
    of the formal parameters whose value was not provided. This is useful in
    cases where one want to distinguish f() from f(None) given "def f(x=None)".

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented May 15, 2009

    Also updated url: http://code.activestate.com/recipes/551779/

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented Mar 19, 2010

    I reverted the function to the original API (return just the dict with the bindings), cleaned it up, wrote thorough unit tests and made a patch against Python 2.7a4.

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented Mar 19, 2010

    Renamed the Testcase classes to conform with the rest in test_inspect.py, added a few more tests for tuple args and patched against the latest trunk (r79086).

    @benjaminp
    Copy link
    Contributor

    The patch will also need docs in inspect.rst.

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented Mar 19, 2010

    • Added docs in inspect.rst
    • Fixed TypeError message for zero-arg functions ("takes no arguments" instead of "takes exactly 0 arguments") + added test.

    @benjaminp
    Copy link
    Contributor

    Would you upload this patch to Rietveld for review?

    @gsakkis
    Copy link
    Mannequin Author

    gsakkis mannequin commented Mar 19, 2010

    Uploaded at http://codereview.appspot.com/659041/show

    @benjaminp
    Copy link
    Contributor

    Applied in r79500. Note I removed the error checking that a bound method received an instance of the class as the first argument because that error checking is a function of the calling of the function, not the binding of the arguments.

    @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

    1 participant