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
cmath.log has an invalid signature #89381
Comments
inspect.signature reports that the cmath.log function has an invalid signature: Python 3.11.0a0 (heads/fix-44954:d0ea569eb5, Aug 19 2021, 14:59:04) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cmath
>>> import inspect
>>> inspect.signature(cmath.log)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mdickinson/Python/cpython/Lib/inspect.py", line 3215, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdickinson/Python/cpython/Lib/inspect.py", line 2963, in from_callable
return _signature_from_callable(obj, sigcls=cls,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdickinson/Python/cpython/Lib/inspect.py", line 2432, in _signature_from_callable
return _signature_from_builtin(sigcls, obj,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdickinson/Python/cpython/Lib/inspect.py", line 2244, in _signature_from_builtin
return _signature_fromstr(cls, func, s, skip_bound_arg)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdickinson/Python/cpython/Lib/inspect.py", line 2114, in _signature_fromstr
raise ValueError("{!r} builtin has invalid signature".format(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: <built-in function log> builtin has invalid signature |
Issue bpo-43067 is similar. I'm not sure what the best solution is in this case:
log(z)
log(z, base)
|
See also bpo-36306 and bpo-29299. There may be nothing to be done here, but it would be nice if math.log and cmath.log at least behaved in the same way. A default of (BTW, just to forestall the suggestion, a default of |
Nope, that's nonsense, since the two-argument form simply divides by log(base), and while log(math.e) is mathematically not exactly 1 (its exact value, assuming IEEE 754 binary64, starts 0.9999999999999999468176229339410862948..., which is off from 1.0 by around 0.48 ulps), it's *just* close enough that with a well-behaved libm log implementation it's exactly 1.0 after rounding. I still don't like the idea of math.e as a default value here, but I'd have to work harder to identify why it makes me uneasy. |
The current Argument Clinic syntax for math.log() is: --------------
Return the logarithm of x to the given base. math.log.__text_signature__ is None. __text_signature__ is used by inspect.signature(). I guess that it's a bug in Argument Clinic. |
The underlying reason of failure for this issue is that the #101070 implements a workaround with base=None defaults (both for math and cmath), as it was suggested above (I hope) by @mdickinson. |
I'm not sure the failure is to do with module dict. Like Victor mentions, I think there's some bug or missing feature in Argument Clinic. If you manually modify the generated file to:
Then Edit: what skirpichev says is true of cmath, but not for math. For cmath we can just use cmath.e |
Actually, looking at it again, why do we need the optional group in mathmodule.c at all? Maybe I'm missing something simple, but removing the optional group should just make all the signature stuff work. I'll open a PR and you can tell me what I'm missing... |
The optional group in math.log seems unnecessary, as far as I can tell. Once we remove this, Argument Clinic knows what to do. For cmath, we just do what math is doing, which is use c_default = NULL (so the actual runtime logic is unchanged), but mark cmath.e as the fake default value.
The optional group in math.log seems unnecessary, as far as I can tell. Once we remove this, Argument Clinic knows what to do. For cmath, we just do what math is doing, which is use c_default = NULL (so the actual runtime logic is unchanged), but mark cmath.e as the fake default value.
After some debugging, it seems that the mentioned by @vstinner math.log issue is not a Clinic's bug, rather a feature. Here is a comment from the clinic.py:
Correct me, but it's not something that is planned to be in the Signature someday, right? Some searching suggests me that there are instead plans to convert such cases to arg=default notation (see this). |
Yes, that's correct. The thing that was confusing for me here is that an optional group is seemingly unnecessary for math.log — so when I posted my first comment I didn't even realise there was one! |
BTW, it seems the broken signature is not a bug for stdlib. See #101123. |
cmath.log has an invalid signature. |
5282f4f indeed, solved. |
FWIW, I object to these changes. They are changing the user experience negatively. It is incorrect to say these were "invalid". The functions predated signature objects. They aren't broken; rather, they are like the In Python 3.11, the doc entry read I haven't done a new speed measurement but presumably having to check for AFAICT, no actual user problem is being solved. No one have ever reported a usability issue here. Instead, the focus seems to be on shoe-horning functions into the argument clinic and signature arguments. When they two don't match (as in this case), there seems to be willingness to damage the function rather than on improving the capabilities of signature objects. Summary: I don't think This is permanent, forever API change. I don't think we have to live with the likes of |
@rhettinger Apologies if I merged it too quickly! I'm curious if you feel the other PR is useful: #101115 re: showing the base explicitly, @mdickinson expressed some concern about about implying that |
About benchmarks. On the main (666c084):
On the branch (was rebased wrt main):
Indeed, there is slight speed regression for the cmath.log. But for the math.log, there is a speed gain...
@rhettinger, that you would like here, as I guess, is the #101115, but with a symbolic constant in the signature/help instead, correct? Actually, this might work like this (same for the math module): diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 2038ac26e6..861a824d77 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -952,7 +952,7 @@ cmath_tanh_impl(PyObject *module, Py_complex z)
cmath.log
z as x: Py_complex
- base as y_obj: object = NULL
+ base as y_obj: object(c_default="NULL") = e
/
log(z[, base]) -> the logarithm of z to the given base. With that change we have:
But, unfortunately (and that's coming from the inspect module logic in RewriteSymbolics):
Something like this is possible with a tentative patch attached (with work with
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: