Skip to content

Revert __signature__ to being a cache, remove string and callable support #116110

Closed
@Gouvernathor

Description

@Gouvernathor

Feature or enhancement

Proposal:

#100039 modified the behavior of __signature__ as used by the inspect.signature function :

  • if the fields contains a Signature object, it is returned by the function (that doesn't change)
  • if it contains None, it is ignored (that doesn't change)
  • if it contains a string, it is converted to a signature (new)
  • if it contains a callable, it is called with no parameters and expected to return either a signature object or a string which will be treated as the above (new)
  • if it contains any other value, a TypeError is raised (this used to include strings and callables, it doesn't anymore)

As for strings :
The __text_signature__ attribute's purpose is to pass a string to inform the signature. It's also only used for C-written functions, and iirc there's no way to use it in pure python : I understand that, but if the ability to specify a text override of inspect.signature must happen, then it should go through __text_signature__ and not __signature__.

As for callables :
The Signature object qualifies, and the signature function inspects, callables. So, if when asked for the signature of a given callable you return another callable, it would make more sense to consider the second callable's signature as the signature of the first callable, than considering the second callable to return the signature of the first. And why no parameters ? Why not pass the first callable as parameter ? Or the actual signature of the first callable ? That implementation raises a lot of questions of arbitrariness, which makes it a bad design in my opinion.

As for the purpose :
The use case, which was adding signature overrides to Enums, does not require any change to the inspect module, in fact a perfectly good alternative implementation can be found in #115937. It should also make for a considerably faster execution.

I suggest reverting these changes.

Links to previous discussion of this feature:

This was discussed in #115937 and #116086 in which it was advised to separate the question of the implementation from that of the documentation.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions