Skip to content

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Aug 10, 2025

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/

Show signatures that match the actual signatures or future multisignatures
for all functions, classes and methods in the "builtins" module.
@serhiy-storchaka serhiy-storchaka added docs Documentation in the Doc dir skip news needs backport to 3.13 bugs and security fixes labels Aug 10, 2025
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label Aug 10, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Aug 10, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Aug 10, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Aug 10, 2025
@serhiy-storchaka
Copy link
Member Author

See also #137611.

Copy link
Member

@terryjreedy terryjreedy left a 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?

Comment on lines 185 to 188
.. class:: bytearray()
bytearray(source)
bytearray(source, encoding)
bytearray(source, encoding, errors)
Copy link
Member

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.

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

@bedevere-app
Copy link

bedevere-app bot commented Aug 10, 2025

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Copy link
Member Author

@serhiy-storchaka serhiy-storchaka left a 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.

Comment on lines 185 to 188
.. class:: bytearray()
bytearray(source)
bytearray(source, encoding)
bytearray(source, encoding, errors)
Copy link
Member Author

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.

AA-Turner and others added 2 commits August 12, 2025 00:30
@serhiy-storchaka serhiy-storchaka force-pushed the docs-builtins-signatures branch from 00556ee to de15f70 Compare August 13, 2025 10:39
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this!

@@ -1944,8 +1940,10 @@ are always available. They are listed here in alphabetical order.
single: string; str() (built-in function)

.. _func-str:
.. class:: str(object='')
str(object=b'', encoding='utf-8', errors='strict')
.. class:: str(*, encoding='utf-8', errors='strict')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's technically true that you can pass just the encoding or errors kwargs (like str(encoding='ebcdic')), but it's not practically useful, since you just get an empty string whatever encoding you pass, and I fear it's confusing to users to put this here.

The old docs here feel clearer even if they're not as precise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be technically correct.

Several years ago I proposed to deprecate this case, but my proposition was rejected. If it is worth to continue supporting this in code, it is worth to document it. Note that values of encoding and errors are ignored:

>>> str(encoding='spam', errors='ham')
''

@@ -846,7 +844,7 @@ are always available. They are listed here in alphabetical order.


.. _func-frozenset:
.. class:: frozenset(iterable=set())
.. class:: frozenset(iterable=(), /)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either approach could work but I like the way it's in the current PR; it's better to have fewer distinct signatures.

@serhiy-storchaka
Copy link
Member Author

Reverted changes for signatures with start/end parameters, as this is a separate non-trivial issue (#138134).

@serhiy-storchaka serhiy-storchaka merged commit 65fb4d1 into python:main Aug 25, 2025
27 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Aug 25, 2025
@serhiy-storchaka serhiy-storchaka deleted the docs-builtins-signatures branch August 25, 2025 12:28
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 25, 2025
…ythonGH-137610)

Show signatures that match the actual signatures or future multisignatures
for all functions, classes and methods in the "builtins" module.
(cherry picked from commit 65fb4d1)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 65fb4d11a0f3f3e4aea373bd84918e4445cbfe67 3.13

@bedevere-app
Copy link

bedevere-app bot commented Aug 25, 2025

GH-138137 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Aug 25, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Aug 25, 2025
…ation (pythonGH-137610)

Show signatures that match the actual signatures or future multisignatures
for all functions, classes and methods in the "builtins" module.
(cherry picked from commit 65fb4d1)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Aug 25, 2025

GH-138138 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Aug 25, 2025
serhiy-storchaka added a commit that referenced this pull request Aug 25, 2025
…H-137610) (GH-138138)

Show signatures that match the actual signatures or future multisignatures
for all functions, classes and methods in the "builtins" module.
(cherry picked from commit 65fb4d1)
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 skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants