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

Improve Ghostwriter for max() builtin and similar signatures #2977

Closed
Zac-HD opened this issue May 25, 2021 · 0 comments · Fixed by #3097
Closed

Improve Ghostwriter for max() builtin and similar signatures #2977

Zac-HD opened this issue May 25, 2021 · 0 comments · Fixed by #3097
Labels
enhancement it's not broken, but we want it to be better

Comments

@Zac-HD
Copy link
Member

Zac-HD commented May 25, 2021

Some builtin functions don't support inspect.signature(), which the ghostwriter works around by trying to parse their docstring with regex:

if (
isinstance(func, (types.BuiltinFunctionType, types.BuiltinMethodType))
and hasattr(func, "__doc__")
and isinstance(func.__doc__, str)
):
# inspect.signature doesn't work on all builtin functions or methods.
# In such cases, including the operator module on Python 3.6, we can try
# to reconstruct simple signatures from the docstring.
pattern = rf"^{func.__name__}\(([a-z]+(, [a-z]+)*)(, \\)?\)"
args = re.match(pattern, func.__doc__)
if args is None:
raise
params = [
# Note that we assume that the args are positional-only regardless of
# whether the signature shows a `/`, because this is often the case.
inspect.Parameter(name=name, kind=inspect.Parameter.POSITIONAL_ONLY)
for name in args.group(1).split(", ")
]

However, max.__doc__ highlights a few things not handled by the current pattern:

max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
  1. square brackets used to indicate optional arguments - we want to allow-but-ignore these
  2. * indicating that following arguments are keyword-only - we should still assume that positional args are pos-only, but assuming that kwonly args are pos-only is silly
  3. "handle" multiple signatures by using the first that we can understand - i.e. change "match first line" to "search all matching lines" and keep the first
@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant