You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In early 2014 (3.3), when argument clinic was added, we added support for turning func.__text_signature__ into an inspect.Signature object. However, the functionality to convert a string into a Signature was never exposed publicly. Here's a patch to do so.
Note that inspect.signature() will also support taking a string.
Note that there has been a little discussion of this in the past, particularly around the time that argument clinic was introduced: https://bugs.python.org/issue23967#msg241140.
Honestly, I don't recall exactly the concrete use case for which I opened this. :) I *think* it was related to applying a more restrictive signature onto an existing function (e.g. with a decorator).
I'd be fine with Signature.from_text(), but not with Signature constructor / signature() function accepting both callable and string arguments. Overall, I think that we ought to have a real need to add this new API, so unless there's a good (or any, really) use case I'd say we shouldn't merge this now.
This came up for me recently. I wanted to create a better signature for a built-in class's __init__ method by converting the signature from the underlying class. The text of the signature was easy to manipulate, but then I had no way to convert it to a signature object.
I worked around it by creating the signature object for the underlying class first, and then modifying it with replace(). That's not a bad work-around, but there were a couple extra considerations that I thought were slightly easier to handle when generating the text of the signature I wanted directly.
I agree that that's no need for inspect.signature() to understand the text format itself.
Activity
ericsnowcurrently commentedon Jul 4, 2019
In early 2014 (3.3), when argument clinic was added, we added support for turning func.__text_signature__ into an inspect.Signature object. However, the functionality to convert a string into a Signature was never exposed publicly. Here's a patch to do so.
Note that inspect.signature() will also support taking a string.
ericsnowcurrently commentedon Jul 4, 2019
Note that there has been a little discussion of this in the past, particularly around the time that argument clinic was introduced: https://bugs.python.org/issue23967#msg241140.
1st1 commentedon Feb 27, 2020
What's the actual use case for exposing this functionality?
ericsnowcurrently commentedon Mar 6, 2020
Honestly, I don't recall exactly the concrete use case for which I opened this. :) I *think* it was related to applying a more restrictive signature onto an existing function (e.g. with a decorator).
1st1 commentedon Mar 6, 2020
I'd be fine with
Signature.from_text(), but not withSignatureconstructor /signature()function accepting both callable and string arguments. Overall, I think that we ought to have a real need to add this new API, so unless there's a good (or any, really) use case I'd say we shouldn't merge this now.tungol commentedon Dec 8, 2024
This came up for me recently. I wanted to create a better signature for a built-in class's
__init__method by converting the signature from the underlying class. The text of the signature was easy to manipulate, but then I had no way to convert it to a signature object.I worked around it by creating the signature object for the underlying class first, and then modifying it with
replace(). That's not a bad work-around, but there were a couple extra considerations that I thought were slightly easier to handle when generating the text of the signature I wanted directly.I agree that that's no need for
inspect.signature()to understand the text format itself.