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

Yury Selivanov yselivanov.ml at gmail.com
Thu Jun 7 03:16:37 CEST 2012


On 2012-06-06, at 9:00 PM, Nick Coghlan wrote:
> On Thu, Jun 7, 2012 at 10:52 AM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
>> Furthermore, using __signature__ as a cache may even cause problems.
>> If the Signature object is cached then any changes to the function
>> will not be reflected in the Signature object.  Certainly that's an
>> unlikely case, but it is a real case. If f.__signature__ is set, I'd
>> expect it to be either an explicitly set value or exactly the same as
>> the first time inspect.signature() was called for that function.  We
>> could make promises about that and do dynamic updates, etc., but it's
>> not useful enough to go to the trouble.  And without the guarantees, I
>> don't think using it as a cache is a good idea.  (And like I said,
>> allowing/using an explicitly set f.__signature__ is a good thing).
> 
> +1
> 
> Providing a defined mechanism to declare a public signature is good,
> but using that mechanism for implicit caching seems like a
> questionable idea. Even when it *is* cached, I'd be happier if
> inspect.signature() returned a copy rather than a direct reference to
> the original.

I'm leaning towards this too.  Besides, constructing a Signature
object isn't an expensive operation.

So, the idea for the 'signature(obj)' function is to first check if
'obj' has '__signature__' attribute set, if yes - return it, if no - 
create a new one (but don't cache).

I have a question about fixing 'functools.wraps()' - I'm not sure
we need to.  I see two solutions to the problem:

I) We fix 'functools.wraps' to do:

   'wrapper.__signature__ = signature(wrapped)'

II) We modify 'signature(obj)' function to do the following steps:

   1. check if obj has '__signature__' attribute. If yes - return it.

   2. check if obj has '__wrapped__' attribute.  If yes:
   obj = obj.__wrapped__; goto 1.

   3. Calculate new signature for obj and return it.

I think that the second (II) approach is better, as we don't
implicitly cache anything, and we don't calculate Signatures
on each 'functools.wraps' call.

-
Yury


More information about the Python-Dev mailing list