This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getcallargs()
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: Alexander.Belopolsky, benjamin.peterson, gsakkis
Priority: normal Keywords: patch

Created on 2008-06-18 22:16 by gsakkis, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
getcallargs.patch gsakkis, 2010-03-19 13:22
getcallargs2.patch gsakkis, 2010-03-19 18:12
Messages (10)
msg68378 - (view) Author: George Sakkis (gsakkis) Date: 2008-06-18 22:16
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.
msg87787 - (view) Author: George Sakkis (gsakkis) Date: 2009-05-15 01:46
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)".
msg87788 - (view) Author: George Sakkis (gsakkis) Date: 2009-05-15 01:47
Also updated url: http://code.activestate.com/recipes/551779/
msg101303 - (view) Author: George Sakkis (gsakkis) Date: 2010-03-19 03:30
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.
msg101326 - (view) Author: George Sakkis (gsakkis) Date: 2010-03-19 13:22
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).
msg101327 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-03-19 13:50
The patch will also need docs in inspect.rst.
msg101339 - (view) Author: George Sakkis (gsakkis) Date: 2010-03-19 18:12
- Added docs in inspect.rst
- Fixed TypeError message for zero-arg functions ("takes no arguments" instead of "takes exactly 0 arguments") + added test.
msg101341 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-03-19 21:34
Would you upload this patch to Rietveld for review?
msg101348 - (view) Author: George Sakkis (gsakkis) Date: 2010-03-19 22:31
Uploaded at http://codereview.appspot.com/659041/show
msg101951 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-03-30 17:59
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.
History
Date User Action Args
2022-04-11 14:56:35adminsetgithub: 47385
2010-03-30 17:59:44benjamin.petersonsetstatus: open -> closed
resolution: accepted
messages: + msg101951
2010-03-19 22:31:13gsakkissetmessages: + msg101348
2010-03-19 21:34:47benjamin.petersonsetmessages: + msg101341
2010-03-19 18:12:56gsakkissetfiles: + getcallargs2.patch

messages: + msg101339
2010-03-19 13:50:59benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg101327
2010-03-19 13:22:55gsakkissetfiles: + getcallargs.patch

messages: + msg101326
2010-03-19 13:20:33gsakkissetfiles: - getcallargs.patch
2010-03-19 05:05:33Alexander.Belopolskysetnosy: + Alexander.Belopolsky
2010-03-19 03:30:31gsakkissetfiles: + getcallargs.patch
keywords: + patch
messages: + msg101303
2009-05-15 01:47:40gsakkissetmessages: + msg87788
2009-05-15 01:46:39gsakkissetmessages: + msg87787
versions: + Python 2.7, - Python 2.6
2008-06-18 22:16:53gsakkiscreate