New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-37497: Add inspect.Signature.from_text(). #14579
base: main
Are you sure you want to change the base?
bpo-37497: Add inspect.Signature.from_text(). #14579
Conversation
@@ -1976,7 +1990,9 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): | |||
module = None | |||
|
|||
if not isinstance(module, ast.Module): | |||
raise ValueError("{!r} builtin has invalid signature".format(obj)) | |||
exc = ValueError(f"invalid signature {text!r}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commenting the if block on master and in this PR has no test failure so I guess this code was not tested. Perhaps a test could be added where __text_signature__
has a invalid syntax as signature to see if ValueError
is raised like below :
def test_python_function_invalid_text_signature(self):
def func(*args, **kwargs):
pass
func.__text_signature__ = '($self, **kwargs, a)'
with self.assertRaises(ValueError) as e:
sig = inspect.signature(func)
@@ -550,7 +550,7 @@ The Signature object represents the call signature of a callable object and its | |||
return annotation. To retrieve a Signature object, use the :func:`signature` | |||
function. | |||
|
|||
.. function:: signature(callable, \*, follow_wrapped=True) | |||
.. function:: signature(callable_or_text, \*, follow_wrapped=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't like the signature()
function to accept both callables and text. This introduces extra complexity to the API for a very narrow use case.
It also makes the function too dynamic: currently, if a text value is passed to signature()
by mistake the user would get an exception saying that a callable was expected. With this patch they'll likely get a ParseError.
IMO a public documented Signature.from_text()
API is enough.
clean_signature, self_parameter, last_positional_only = \ | ||
_signature_strip_non_python_syntax(s) | ||
(clean_signature, self_parameter, last_positional_only | ||
) = _signature_strip_non_python_syntax(text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd keep the \
:)
When you're done making the requested changes, leave the comment: |
https://bugs.python.org/issue37497