Skip to content

Correct str.maketrans and collections.UserString.maketrans #9611

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

Merged
merged 2 commits into from
Jan 29, 2023

Conversation

AlexWaygood
Copy link
Member

Refs #9608.

If the third argument for str.maketrans is not specified, dict[int, int] is returned. If the third argument is specified, it must be str, not None:

>>> str.maketrans('a', 'b')
{97: 98}
>>> str.maketrans('a', 'b', None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: maketrans() argument 3 must be str, not None

This all aligns with the documentation: https://docs.python.org/3/library/stdtypes.html#str.maketrans

At runtime, collections.UserString.maketrans is just a simple alias for str.maketrans: https://github.com/python/cpython/blob/666c0840dcac9941fa41ec619fef8d45cd849a0b/Lib/collections/__init__.py#L1527. We can do the same in the stub, and should do the same in the stub; it will ensure that the two methods remain in sync. (Currently the parameters for UserString.maketrans are incorrectly marked as positional-or-keyword, for example -- they are in fact positional-only.)

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja merged commit dd3aedd into python:main Jan 29, 2023
@AlexWaygood AlexWaygood deleted the str-maketrans branch January 29, 2023 19:49
Avasam pushed a commit to Avasam/typeshed that referenced this pull request Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants