Skip to content

invalid signature for math.hypot #101123

Description

@skirpichev
>>> 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

Activity

  1. rhettinger commented on Jan 19, 2023

    @rhettinger
    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.

  2. skirpichev commented on Jan 19, 2023

    @skirpichev
    MemberAuthor

    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
    
  3. skirpichev commented on Feb 8, 2023

    @skirpichev
    MemberAuthor

    FYI: AC stuff is fast with #30312.

  4. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Oct 27, 2024
  5. skirpichev commented on Oct 27, 2024

    @skirpichev
    MemberAuthor

    @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).

  6. added a commit that references this issue on Oct 28, 2024
  7. skirpichev commented on Oct 28, 2024

    @skirpichev
    MemberAuthor

    #126064 should fix this

    pr is ready: #126235

  8. self-assigned this
    on Oct 28, 2024
  9. added a commit that references this issue on Oct 31, 2024
  10. removed their assignment
    on Oct 31, 2024
  11. added a commit that references this issue on Oct 31, 2024
  12. added a commit that references this issue on Dec 8, 2024
  13. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions