-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Update function signatures to use *
and /
as needed
#131885
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
Comments
Now function docstrings are in sync with the module docs, for example: ```pycon >>> import cmath, inspect >>> help(cmath.sin) Help on built-in function sin in module cmath: sin(z) Return the sine of z. >>> inspect.signature(cmath.sin) <Signature (z)> ``` As a side effect, this allows `sin(z=1.23)` calls. The price is slight performance penalty (maybe this can be fixed on AC side): | Benchmark | ref | patch | |----------------|:------:|:--------------------:| | sin(1.1) | 417 ns | 428 ns: 1.03x slower | | sin(1j) | 414 ns | 422 ns: 1.02x slower | | log(2.3) | 390 ns | 409 ns: 1.05x slower | | log(1+2j) | 449 ns | 468 ns: 1.04x slower | | log(2.1, 3.2) | 601 ns | 630 ns: 1.05x slower | | Geometric mean | (ref) | 1.04x slower |
…no keyword arguments (pythonGH-128208) (cherry picked from commit edfbd8c) Co-authored-by: Adam Dangoor <[email protected]>
…no keyword arguments (pythonGH-128208) (cherry picked from commit edfbd8c) Co-authored-by: Adam Dangoor <[email protected]>
Regarding |
My 2c on how this can be addressed. First, probably we shouldn't just mechanically change docs to reflect actual signatures. I trust @rhettinger teaching experience that slash/star-enabled signatures are less readable for newcomers. On another hand, IMO such signatures for C-coded functions usually seems to be artifacts of lacking the AC in past. Now we can use positional-or-keyword arguments in C and leave boilerplate code to the AC. Why not do this? Then e.g. we can use plain-and-simple The price is some performance degradation (see #131886), which might be AC bug. Also, argument names will be part of the API. Second, I think that EB decision covers only sphinx docs, @nedbat ? If so, we also have docstrings for C-coded functions, where in some cases function signatures are documented by some non-Python syntax, nowhere (i.e. in docs) actually documented. min/max functions are examples. What we should do here? Proper solution, probably, requires solving #73536. But that seems to be rather an issue for the inspect module. I think that already now we can start fixing such docstrings to use multiple signatures (each being a valid Python function signature!), just as sphinx docs. So, the outcome for e.g. >>> help(max)
Help on built-in function max in module builtins:
max(iterable, /, *, key=None)
max(iterable, /, *, default, key=None)
max(arg1, arg2, /, *args, key=None)
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more positional arguments, return the largest argument.
Sure, we can invent a more dense syntax for such signatures. But I doubt it worth: 1) new syntax will require an explanation 2) save us line or two in each case, at maximum. |
I could understand why that would be the case previously, if you didn't know what
Could you clarify what AC means here?
Yes this is great! |
There is no tooltips in help() output.
https://devguide.python.org/development-tools/clinic/
In general, sphinx docs and docstrings are different. Later less verbose, miss examples, etc. But wrt functions signatures they could be same and, probably, should. |
…no keyword arguments (python#128208)
Documentation
Caution
For new contributors, please do not consider this issue as an "easy" one. While the changes may seem mechanical, they are not necessarily trivial as this requires to know what really happens (both at a Python and a C level).
This is tracks cases where changes need to be made to enact the Editorial Board's decision for function signature markup.
min()
max()
range()
map()
dict.get()
,dict.setdefault()
See also:
Linked PRs
dict.setdefault
anddict.get
take no keyword arguments (GH-128208) #131893dict.setdefault
anddict.get
take no keyword arguments (GH-128208) #131894/
fordecimal.Context
methods #131990/
forcodecs
functions #131992*
forcode.InteractiveConsole
#132029The text was updated successfully, but these errors were encountered: