GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [mypyc] Add primitives for str.capitalize, casefold, swapcase and title by Jahongir-Qurbonov · Pull Request #22413 · python/mypy

Conversation

@Jahongir-Qurbonov

@Jahongir-Qurbonov Jahongir-Qurbonov commented Nov 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Add primitives for str.capitalize(), str.casefold(), str.swapcase() and str.title().

ASCII strings use a direct fast path. Other strings fall back to calling the str method, similar to str.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 exact str, also for str subclasses. The fallback calls the method on str (not on the type of the object), so a subclass that overrides one of these methods can't make the result something other than a str.

Micro-benchmark (compiled code, 3.2M calls, best of 15, before -> after, Python 3.13):

Method Short ASCII (11 chars) Long ASCII (176 chars)
capitalize() 0.257s -> 0.082s 1.755s -> 0.251s
casefold() 0.125s -> 0.081s 0.285s -> 0.250s
swapcase() 0.221s -> 0.087s 1.439s -> 0.416s
title() 0.259s -> 0.085s 2.026s -> 0.886s

Non-ASCII strings are roughly unchanged.

Part of mypyc/mypyc#644.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jahongir-Qurbonov added 2 commits November 10, 2026 22:46
@Jahongir-Qurbonov
[mypyc] Add primitives for str.capitalize, casefold, swapcase and title
edf54f1
@Jahongir-Qurbonov
Always return an exact str from case methods on overriding str subcla…
df6b2f0
…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
Jahongir-Qurbonov force-pushed the mypyc-str-case branch from 337a69c to df6b2f0 Compare November 10, 2026 18:02
@Jahongir-Qurbonov Jahongir-Qurbonov mentioned this pull request Nov 10, 2026
More primitives mypyc/mypyc#644
Open
13 of 14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@Jahongir-Qurbonov
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.