Repository navigation
[mypyc] Add primitives for str.capitalize, casefold, swapcase and title - #22413
Open
Jahongir-Qurbonov wants to merge 3 commits into
Open
Jahongir-Qurbonov wants to merge 3 commits into
Jahongir-Qurbonov wants to merge 3 commits into
Conversation
…sses For non-ASCII strings, look up the method on str instead of on the type of the object, so that an override in a str subclass can't make the result something other than a str.
Jahongir-Qurbonov
force-pushed
the
mypyc-str-case
branch
from
November 10, 2026 18:02
337a69c to
df6b2f0
Compare
13 of 14 tasks
# Conflicts: # mypyc/test-data/fixtures/ir.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add primitives for
str.capitalize(),str.casefold(),str.swapcase()andstr.title().ASCII strings use a direct fast path. Other strings fall back to calling the
strmethod, similar tostr.lower()/str.upper()on Python 3.13+, since the full Unicode case mappings (special casing, title case characters, final sigma, etc.) are not available through the public C API.Tests compare the results against the generic implementation (through
Any) for all ASCII strings of length 1 and 2 (and some of length 3), and various ASCII and non-ASCII strings. The result is always an exactstr, also forstrsubclasses. The fallback calls the method onstr(not on the type of the object), so a subclass that overrides one of these methods can't make the result something other than astr.Micro-benchmark (compiled code, 3.2M calls, best of 15, before -> after, Python 3.13):
capitalize()casefold()swapcase()title()Non-ASCII strings are roughly unchanged.
Part of mypyc/mypyc#644.