[Python-Dev] Updated PEP 362 (Function Signature Object)

Yury Selivanov yselivanov.ml at gmail.com
Wed Jun 6 04:51:30 CEST 2012


Nick,

On 2012-06-05, at 9:45 PM, Nick Coghlan wrote:
> Specific proposals:
> 
> - the goals of the PEP be expanded to include error checking of
> parameter binding for delayed calls and improve introspection of
> function wrappers that accept arbitrary arguments, rather than the
> more nebulous "improve introspection support for functions".

It's already supported, if I understand your request correctly.
'Signature.bind' already does all necessary arguments validation (see the unit tests).

Hence, `unittest.TestCase.addCleanup` may be rewritten as:

    def addCleanup(self, function, *args, **kwargs):
        # Bind *args & **kwargs. May raise a BindError in case
        # of incorrect callback arguments.
        bound_args = signature(function).bind(*args, **kwargs)
        self._cleanups.append((function, bound_args))

And later, in `doCleanups`:

    while self._cleanups:
        function, bound_args = self._cleanups.pop()
        part = lambda: function(*bound_args.args, **bound_args.kwargs)
        self._executeTestPart(part, outcome)
        ...

And same for `contextlib.ExitStack`:

    def callback(self, callback, *args, **kwds):
	cb_bound_args = signature(callback).bind(*args, **kwds)
        def _exit_wrapper(exc_type, exc, tb):
            callback(*cb_bound_args.args, **cb_bound_args.kwargs)
        ...

> - the main new API be added as "functools.signature" (any necessary
> infrastructure from inspect would also migrate to functools as private
> implementations. affected inspect APIs would either be updated to use
> the signature API, or else to just republish the functools
> implementation)

Only 'ismethod' and 'isfunction' from the 'inspect' module are used.  Those could be easily replaced with direct 'isinstance' calls, so we have no dependancy on inspect's guts.

As for moving Signature object to `functools`, we had this discussion with Brett, and here is what he suggested:

    Functools contains code that transforms what a function 
    does while inspect is about introspection. These objects are 
    all about introspection and not about transforming what a 
    function does.

> - "functools.update_wrapper" be enhanced to set "wrapper.__signature__
> = signature(wrapped)"

Big +1 on this one.  If you give me a green light on this, I'll add this change along with the unit tests to the patch.

> - At least contextlib and unittest be updated to use
> "functools.signature(f).bind(*args, **kwds)" for the relevant callback
> APIs


+1.

Thank you,
-
Yury



More information about the Python-Dev mailing list