Skip to content

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

Open
1 of 5 tasks
ekohilas opened this issue Mar 30, 2025 · 4 comments
Open
1 of 5 tasks

Update function signatures to use * and / as needed #131885

ekohilas opened this issue Mar 30, 2025 · 4 comments
Labels
docs Documentation in the Doc dir

Comments

@ekohilas
Copy link
Contributor

ekohilas commented Mar 30, 2025

@ekohilas ekohilas added the docs Documentation in the Doc dir label Mar 30, 2025
skirpichev added a commit to skirpichev/cpython that referenced this issue Mar 30, 2025
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         |
picnixz pushed a commit that referenced this issue Mar 30, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 30, 2025
…no keyword arguments (pythonGH-128208)

(cherry picked from commit edfbd8c)

Co-authored-by: Adam Dangoor <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 30, 2025
…no keyword arguments (pythonGH-128208)

(cherry picked from commit edfbd8c)

Co-authored-by: Adam Dangoor <[email protected]>
picnixz pushed a commit that referenced this issue Mar 30, 2025
… no keyword arguments (GH-128208) (#131893)

gh-131885: Document that `dict.setdefault` and `dict.get` take no keyword arguments (GH-128208)
(cherry picked from commit edfbd8c)

Co-authored-by: Adam Dangoor <[email protected]>
picnixz pushed a commit that referenced this issue Mar 30, 2025
… no keyword arguments (GH-128208) (#131894)

gh-131885: Document that `dict.setdefault` and `dict.get` take no keyword arguments (GH-128208)
(cherry picked from commit edfbd8c)

Co-authored-by: Adam Dangoor <[email protected]>
@chris-eibl
Copy link
Member

Regarding range, please see also #125897.

@skirpichev
Copy link
Member

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 len(obj) in documentation, instead of len(obj, /). Note that first form preferred also by some Python implementations (e.g. PyPy).

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. max() will be (as in #117671, merge conflicts fixed in skirpichev#7):

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

@ekohilas
Copy link
Contributor Author

I trust @rhettinger teaching experience that slash/star-enabled signatures are less readable for newcomers.

I could understand why that would be the case previously, if you didn't know what / meant!
I would think now with the tooltips present on mouseover for * and /, that would no longer be the case, as newcomers could dig into what that means.
As it stands, the lack of / and * in signatures is worse, because users that have seen or understood different types of argument usage would be confused why a function behaves as if it has a / but isn't documented as such.

artifacts of lacking the AC in past.

Could you clarify what AC means here?

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?

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.

Yes this is great!
If docstrings are used as docs, they could be the same as the docs and signatures in the docs themselves?
No need for having to define the undocumented syntax already used, and give newcomers another thing to learn.

@skirpichev
Copy link
Member

I would think now with the tooltips present on mouseover for * and /

There is no tooltips in help() output.

Could you clarify what AC means here?

https://devguide.python.org/development-tools/clinic/

If docstrings are used as docs, they could be the same as the docs and signatures in the docs themselves?

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.

seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

3 participants