-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
gh-137609: Update signatures of builtins in the documentation #137610
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
base: main
Are you sure you want to change the base?
gh-137609: Update signatures of builtins in the documentation #137610
Conversation
Show signatures that match the actual signatures or future multisignatures for all functions, classes and methods in the "builtins" module.
…uiltins This is to pair with pythonGH-137610.
…uiltins This is to pair with pythonGH-137610.
See also #137611. |
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 approve of the issue, the addition of /
s, the renamings, and most of the details. The main question for me is relative positioning of /
and *args
.
Fewer versus more lines is partly style preference and partly technical accuracy, and the signature needed to have an inspect.signature and to write a python version of the same or similar function, versus ease of understanding how to call the function. Are "future multisignatures" a real possibility?
Doc/library/functions.rst
Outdated
.. class:: bytearray() | ||
bytearray(source) | ||
bytearray(source, encoding) | ||
bytearray(source, encoding, errors) |
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 believe only two lines are needed.
.. class:: bytearray() | |
bytearray(source) | |
bytearray(source, encoding) | |
bytearray(source, encoding, errors) | |
.. class:: bytearray(source=b'') | |
bytearray(source, encoding, errors='strict') |
See also #137100, which is also about the text that follows.
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.
Some descriptions have separate signature for no argument, others merge it with a signature with one argument. See for example dict
which could be written as dict(mapping_or_iterable=(), **kwargs)
, but is written as three semantically different signatures. I tried to be more consistent and chose the former variant. But I have no such strong preference.
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 prefer fewer lines, and approve of the changes.
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'm not sure I agree here, source=b''
makes it less clear to me that source
also accepts e.g. iterables of integers, buffer-protocol, etc. I would suggest:
.. class:: bytearray()
bytearray(source, /)
bytearray(source, /, encoding, errors='strict')
Note I have suggested to annotate 'source' as positional-only -- I think this makes more sense to users than writing e.g. bytearray(source=my_numpy_array)
. My IDE & type-checkers also indicates that source=...
is an error, as it is annotated as positional-only in typeshed.
When you're done making the requested changes, leave the comment: |
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 have made the requested changes; please review again.
Doc/library/functions.rst
Outdated
.. class:: bytearray() | ||
bytearray(source) | ||
bytearray(source, encoding) | ||
bytearray(source, encoding, errors) |
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.
Some descriptions have separate signature for no argument, others merge it with a signature with one argument. See for example dict
which could be written as dict(mapping_or_iterable=(), **kwargs)
, but is written as three semantically different signatures. I tried to be more consistent and chose the former variant. But I have no such strong preference.
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Doc/library/functions.rst
Outdated
.. class:: bytearray() | ||
bytearray(source) | ||
bytearray(source, encoding) | ||
bytearray(source, encoding, errors) |
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'm not sure I agree here, source=b''
makes it less clear to me that source
also accepts e.g. iterables of integers, buffer-protocol, etc. I would suggest:
.. class:: bytearray()
bytearray(source, /)
bytearray(source, /, encoding, errors='strict')
Note I have suggested to annotate 'source' as positional-only -- I think this makes more sense to users than writing e.g. bytearray(source=my_numpy_array)
. My IDE & type-checkers also indicates that source=...
is an error, as it is annotated as positional-only in typeshed.
@@ -846,7 +844,7 @@ are always available. They are listed here in alphabetical order. | |||
|
|||
|
|||
.. _func-frozenset: | |||
.. class:: frozenset(iterable=set()) | |||
.. class:: frozenset(iterable=(), /) |
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.
The empty frozenset is (notionally) a singleton, similar to the empty tuple, so I think it's clearer to distinguish them here:
.. class:: frozenset(iterable=(), /) | |
.. class:: frozenset() | |
frozenset(iterable, /) |
@@ -1144,8 +1142,7 @@ are always available. They are listed here in alphabetical order. | |||
|
|||
|
|||
.. _func-list: | |||
.. class:: list() | |||
list(iterable) | |||
.. class:: list(iterable=(), /) |
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.
As with frozenset I think clearer to keep two lines here:
.. class:: list(iterable=(), /) | |
.. class:: list() | |
list(iterable, /) |
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.
Your suggestions are opposite to @terryjreedy's. I just changed the code in opposite direction.
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.
@terryjreedy what do you think? I would be -1 on combining here, I think it goes too far (even though it is technically accurate).
We currently have two lines for these container initialisers in the documentation, so it's not adding more bloat.
Doc/library/stdtypes.rst
Outdated
be removed - the name refers to the fact this method is usually used with | ||
ASCII characters. If omitted or ``None``, the *chars* argument defaults to | ||
removing ASCII whitespace. The *chars* argument is not a suffix; rather, | ||
ASCII characters. If omitted or ``None``, the *bytes* argument defaults to |
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 sentence no longer makes sense after changing the argument name
Doc/library/stdtypes.rst
Outdated
be removed - the name refers to the fact this method is usually used with | ||
ASCII characters. If omitted or ``None``, the *chars* argument defaults | ||
to removing ASCII whitespace. The *chars* argument is not a prefix; | ||
ASCII characters. If omitted or ``None``, the *bytes* argument defaults |
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 sentence no longer makes sense after changing the argument name
Doc/library/stdtypes.rst
Outdated
byte values to be removed - the name refers to the fact this method is | ||
usually used with ASCII characters. If omitted or ``None``, the *chars* | ||
argument defaults to removing ASCII whitespace. The *chars* argument is | ||
usually used with ASCII characters. If omitted or ``None``, the *bytes* |
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 sentence no longer makes sense after changing the argument name
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.
Could you please explain?
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.
Sorry, happy to explain. It currently says:
The chars argument is a binary sequence specifying the set of byte values to be removed - the name refers to the fact this method is usually used with ASCII characters.
This PR changes *chars*
to *bytes*
, which means the second half of the sentence doesn't make sense / no longer applies. It should be removed or changed.
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.
Ah, good catch. Then I'll restore chars
. This is not the best name, but this should be a separate issue.
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.
On other hand, bytes
is already used in the signature for long time. So this is in the scope of this PR.
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.
Thank you for your review @AA-Turner, especially for your catch of stop/end misnaming.
You suggestions about constructors with no argument are opposite to @terryjreedy's. I have no strong preference for now. I will agree to any consensus.
Could you please explain what is wrong with bytes.strip()
etc?
Doc/library/functions.rst
Outdated
@@ -1562,7 +1559,7 @@ are always available. They are listed here in alphabetical order. | |||
.. versionchanged:: 3.11 | |||
The ``'U'`` mode has been removed. | |||
|
|||
.. function:: ord(c) | |||
.. function:: ord(c, /) |
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.
Hmm, the following text and the docstring are incorrect, they should be changed in any case.
@@ -1144,8 +1142,7 @@ are always available. They are listed here in alphabetical order. | |||
|
|||
|
|||
.. _func-list: | |||
.. class:: list() | |||
list(iterable) | |||
.. class:: list(iterable=(), /) |
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.
Your suggestions are opposite to @terryjreedy's. I just changed the code in opposite direction.
Doc/library/stdtypes.rst
Outdated
byte values to be removed - the name refers to the fact this method is | ||
usually used with ASCII characters. If omitted or ``None``, the *chars* | ||
argument defaults to removing ASCII whitespace. The *chars* argument is | ||
usually used with ASCII characters. If omitted or ``None``, the *bytes* |
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.
Could you please explain?
Doc/library/functions.rst
Outdated
|
||
Given a string representing one Unicode character, return an integer | ||
The argument must be a one-character string or a :class:`bytes` or |
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 created a separate issue for this: #137668.
This reverts commit b159ccd.
00556ee
to
de15f70
Compare
Show signatures that match the actual signatures or future multisignatures for all functions, classes and methods in the "builtins" module.
📚 Documentation preview 📚: https://cpython-previews--137610.org.readthedocs.build/