-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
gh-131885: Updates docs to make max and min iterable param positional only #131868
base: main
Are you sure you want to change the base?
Conversation
Actually, I'm finding more examples of these cases such as map and range. If the above looks good I'll add more commits to include those too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I'm finding more examples of these cases such as map and range. If the above looks good I'll add more commits to include those too.
Previously similar change was reverted: #99476 See also #125945
While devguide suggests now using correct function signatures, I'm not sure we shold push this that way.
We definitely need an issue, e.g. to adjust all signatures on the builtin functions page. We should also consider if we could actually simplify function signatures to avoid '/'
and/or '*'
syntax. See https://discuss.python.org/t/81003
CC @AA-Turner
@@ -2019,7 +2019,7 @@ builtin_min(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *k | |||
} | |||
|
|||
PyDoc_STRVAR(min_doc, | |||
"min(iterable, *[, default=obj, key=func]) -> value\n\ | |||
"min(iterable, /, *[, default=obj, key=func]) -> value\n\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid Python syntax. Same for max().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the issue being that the square brackets make it invalid syntax, and that without this change, the documentation would still be wrong.
The reason for change here is to ensure that the web docs align with the code docs, similar to sorted:
Line 2575 in 5a3f13b
"sorted($module, iterable, /, *, key=None, reverse=False)\n" |
Would it be more correct to instead write:
"min(iterable, /, *[, default=obj, key=func]) -> value\n\ | |
"min(iterable, /, *, key=None) -> value\n\ | |
min(iterable, /, *, default, key=None) -> value\n\ | |
min(arg1, arg2, *args, /, *, key=None) -> value\n\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@skirpichev Could you please help me understand why -> value
should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rationale is just same as for sphinx docs: it's not a valid syntax (you will get a NameError trying to parse such signature with inspect.signature()
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the issue being that the square brackets make it invalid syntax, and that without this change, the documentation would still be wrong.
Actually, it's a valid Sphinx syntax. It's the "legacy" way of writing signatures. Many built-ins are written that way. As for ->
, I think it was meant for "readability" even if it's unparsable. I would personally leave this untouched in this PR (namely, only add positional-only marker but leave out the ->
). In a follow-up, we could clean-up the way "invalid" syntaxes, but this is something different than adding the markers.
Thanks for these references!
Thank you for linking this read! I too would like to see simplifying these built-in function signatures, and have functions like As I understand it, doing so would be a one day door that would require additional thought and decisions, as if it went through and started to be used, it would no longer be backwards compatible. Until then, this seems like the next best step. It's what the Editorial board has agreed on, it brings consistency, and reverting these changes in the future if and when they are simplified would be easy. As it stands, without these changes:
|
@@ -2019,7 +2019,7 @@ builtin_min(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *k | |||
} | |||
|
|||
PyDoc_STRVAR(min_doc, | |||
"min(iterable, *[, default=obj, key=func]) -> value\n\ | |||
"min(iterable, /, *[, default=obj, key=func]) -> value\n\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"min(iterable, /, *[, default=obj, key=func]) -> value\n\ | |
"min(iterable, /, *, key=None)\n\ | |
min(iterable, /, *, default, key=None)\n\ | |
min(arg1, arg2, *args, /, key=None)\n\ |
just like sphinx docs
Co-authored-by: Evan Kohilas <ekohilas@users.noreply.github.com>
Created issue: #131885 |
Personally, I read this is as requirement and not a suggestion. Namely,
Here, the
So I think it's fine to accept those changes. The reversal of previous changes was mostly done because it was incorrect, albeit |
@@ -2036,7 +2036,7 @@ builtin_max(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *k | |||
} | |||
|
|||
PyDoc_STRVAR(max_doc, | |||
"max(iterable, *[, default=obj, key=func]) -> value\n\ | |||
"max(iterable, /, *[, default=obj, key=func]) -> value\n\ | |||
max(arg1, arg2, *args, *[, key=func]) -> value\n\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a change as well.
@@ -2019,7 +2019,7 @@ builtin_min(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *k | |||
} | |||
|
|||
PyDoc_STRVAR(min_doc, | |||
"min(iterable, *[, default=obj, key=func]) -> value\n\ | |||
"min(iterable, /, *[, default=obj, key=func]) -> value\n\ | |||
min(arg1, arg2, *args, *[, key=func]) -> value\n\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a change as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion was above;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups, missed it :')
Probably, yes. But rather than mechanically accept "complex" signatures - we should revisit on case-by-case basis if we could simplify them. E.g. support
@picnixz, are you about #99476? Reversion was argued not by correctness: #98340 |
Wait, were you talking about #100547 or another reversal? |
Ah no I see. Ok you were talking about another PR. My bad. Well, the EB decision is quite recent and the reversal was done before that decision so I think it doesn't apply here. |
For this reason, I'm not sure the tracking issue is helpful -- it will encourage people to create many PRs to 'fix' the issue, when it is not just a mechanical transformation at hand. |
This is the reason why I didn't add the "easy" label. But tracking is fine I think. I can add a warning note to say that it's not an easy one as the transformation is not just mechanical |
Someone could comment on issue to clarify it further. |
I've added a red caution box, hopefully it will be seen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid mixing C-style [,optional-arg]
with the /
etc markers.
I think we should also not conflate changes to the documentation, which support a wider range of syntax, with changes to function signatures in docstrings, which are interpreted by pydoc
, help()
, etc. See Raymond's Discourse post on improving support for signatures, and Serhiy's draft work on multi-signature support for inspect
.
For now, please revert the changes to bltinmodule.c
, and we can start with iterative improvements to the rST documentation.
A
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be put in the comfy chair! |
Thanks! One thought; maybe we could mention that it's better to start with cases, when function signatures are simple, i.e. can be expressed with usual syntax for function definition in Python (e.g. @AA-Turner, honestly I don't sure I get why we should keep old signatures in docstrings. First, we will have different signatures in the sphinx docs wrt docstrings - as an eternal source of bugs. Second,
Why not? I think it's a good idea to keep them in sync. BTW, I don't think that "C-style" signatures are interpreted somewhere.
I doubt that current shortcomings of the inspect module really relevant here. Yes, in the given example we can't accurately describe each function just by one signature with a correct Python syntax. But can with multiple, just as it's proposed in sphinx docs! That's how it looks on top of the #117671 (I did some conflict resolution 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.
>>> help(min)
Help on built-in function min in module builtins:
min(iterable, /, *, key=None)
min(iterable, /, *, default, key=None)
min(arg1, arg2, /, *args, key=None)
With a single iterable argument, return its smallest 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 smallest argument.
>>> import inspect
>>> inspect.signatures(max)
<MultiSignature (iterable, /, *, key=None)|(iterable, /, *, default, key=None)|(arg1, arg2, /, *args, key=None)> As you can see, function signatures exactly follow to proposal in this pr. |
This PR updates the documentation of
max
andmin
to match their implementation like howsorted
's implementation matches it's documentation.Sorted's function definition in the documentation is as follows:
sorted(iterable, /, *, key=None, reverse=False)
When attempted to call with
iterable
as a keyword argument, the following occurs:However, looking at the documentation for
max
:max(iterable, *, key=None)
And comparing to their implementation:
The same follows for
min
📚 Documentation preview 📚: https://cpython-previews--131868.org.readthedocs.build/
*
and/
as needed #131885