Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ericsnowcurrently
Copy link
Member

@ericsnowcurrently ericsnowcurrently commented Jul 4, 2019

@ericsnowcurrently ericsnowcurrently changed the title Add inspect.Signature.from_text(). bpo-37497: Add inspect.Signature.from_text(). Jul 4, 2019
@@ -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}")
Copy link
Member

@tirkarthi tirkarthi Jul 4, 2019

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)

@brettcannon brettcannon added the type-feature A feature request or enhancement label Jul 5, 2019
@csabella csabella requested review from methane and 1st1 Feb 27, 2020
@@ -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)
Copy link
Member

@1st1 1st1 Feb 27, 2020

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)
Copy link
Member

@1st1 1st1 Feb 27, 2020

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 \ :)

@bedevere-bot
Copy link

bedevere-bot commented Feb 27, 2020

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants