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

invalid signature for math.hypot #101123

Closed
skirpichev opened this issue Jan 18, 2023 · 5 comments · Fixed by #126235
Closed

invalid signature for math.hypot #101123

skirpichev opened this issue Jan 18, 2023 · 5 comments · Fixed by #126235
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@skirpichev
Copy link
Member

skirpichev commented Jan 18, 2023

>>> import math
>>> import inspect
>>> inspect.signature(math.hypot) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sk/src/cpython/Lib/inspect.py", line 3295, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sk/src/cpython/Lib/inspect.py", line 3039, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sk/src/cpython/Lib/inspect.py", line 2531, in _signature_from_callable
    return _signature_from_builtin(sigcls, obj,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sk/src/cpython/Lib/inspect.py", line 2330, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <built-in function hypot>

This patch works:

diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 1342162fa7..0e610eb9cb 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2805,7 +2805,9 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
 #undef NUM_STACK_ELEMS
 
 PyDoc_STRVAR(math_hypot_doc,
-             "hypot(*coordinates) -> value\n\n\
+             "hypot($module, *coordinates)\n\
+--\n\
+\n\
 Multidimensional Euclidean distance from the origin to a point.\n\
 \n\
 Roughly equivalent to:\n\

Linked PRs

@skirpichev skirpichev added the type-bug An unexpected behavior, bug, or error label Jan 18, 2023
@rhettinger
Copy link
Contributor

We don't have a rule that all docstrings have to be in a format edible by inspect? There are many docstrings that don't. This is just a simple *args* function and I would rather not garbage-up the docstring with the funky looking text_signature notation.

@rhettinger rhettinger closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
@skirpichev
Copy link
Member Author

This is just a simple args function and I would rather not garbage-up the docstring with the funky looking text_signature notation.

@rhettinger, could you explain a bit more what it does break?

Here is how new docstring looks (just like other similar functions in the module, e.g. gcd and lcm, and how the rst docs for the hypot looks too):

>>> help(math.hypot)
Help on built-in function hypot in module math:

hypot(*coordinates)
    Multidimensional Euclidean distance from the origin to a point.

    Roughly equivalent to:
        sqrt(sum(x**2 for x in coordinates))

    For a two dimensional point (x, y), gives the hypotenuse
    using the Pythagorean theorem:  sqrt(x*x + y*y).

    For example, the hypotenuse of a 3/4/5 right triangle is:

        >>> hypot(3.0, 4.0)
        5.0

Here is the old:

>>> help(math.hypot)
Help on built-in function hypot in module math:

hypot(...)
    hypot(*coordinates) -> value

    Multidimensional Euclidean distance from the origin to a point.

    Roughly equivalent to:
        sqrt(sum(x**2 for x in coordinates))

    For a two dimensional point (x, y), gives the hypotenuse
    using the Pythagorean theorem:  sqrt(x*x + y*y).

    For example, the hypotenuse of a 3/4/5 right triangle is:

        >>> hypot(3.0, 4.0)
        5.0

@skirpichev
Copy link
Member Author

FYI: AC stuff is fast with #30312.

@skirpichev skirpichev added type-feature A feature request or enhancement extension-modules C modules in the Modules dir and removed type-bug An unexpected behavior, bug, or error labels Oct 27, 2024
@skirpichev
Copy link
Member Author

@rhettinger, I'll reopen this as a feature request.

AC now supports *args processing, but it's coming with a slight performance penalty. One can be mitigated, using approach from #90370; I'm working on patch. With this, gcd/lcm could be converted to AC too (now these functions use "funky looking text_signature notation" (c) in docstrings).

@skirpichev skirpichev reopened this Oct 27, 2024
skirpichev added a commit to skirpichev/cpython that referenced this issue Oct 28, 2024
Current patch partially address issue, working in case when all
arguments either positional-only or vararg.

This also converts gcd(), lcm() and hypot() functions of the math module
to the Argument Clinic.  Fix issue python#101123.

Objects/setobject.c and Modules/gcmodule.c adapted.  This fixes slight
performance regression for set methods, introduced by
python#115112:

| Benchmark            | ref    | patch                |
|----------------------|:------:|:--------------------:|
| set().update(s1, s2) | 354 ns | 264 ns: 1.34x faster |

Benchmark code:
```py
import pyperf
s1, s2 = {1}, {2}
runner = pyperf.Runner()
runner.bench_func('set().update(s1, s2)', set().update, s1, s2)
```
@skirpichev
Copy link
Member Author

skirpichev commented Oct 28, 2024

#126064 should fix this

pr is ready: #126235

@skirpichev skirpichev self-assigned this Oct 28, 2024
skirpichev added a commit to skirpichev/cpython that referenced this issue Oct 31, 2024
@skirpichev skirpichev removed their assignment Oct 31, 2024
erlend-aasland pushed a commit that referenced this issue Oct 31, 2024
…ic (#126235)

This implicitly fixes the math.hypot signature, which was previously
incomprehensible to inspect.signature().
picnixz pushed a commit to picnixz/cpython that referenced this issue Dec 8, 2024
…t Clinic (python#126235)

This implicitly fixes the math.hypot signature, which was previously
incomprehensible to inspect.signature().
ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
…t Clinic (python#126235)

This implicitly fixes the math.hypot signature, which was previously
incomprehensible to inspect.signature().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants