fix(vars): compile title() with Python's str.title semantics

DI
2d ago
+97-3

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

StringVar.title() compiles to s.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' '), so in the browser a word only starts after a space. Python's str.title() starts a word after any character that has no case. The same value comes out differently on the page:

  • "jean-luc picard" renders as Jean-luc Picard. Python gives Jean-Luc Picard.
  • "hello\nworld" renders as Hello\nworld, and a tab doesn't start a word either. Python gives Hello\nWorld.
  • A letter outside the BMP at the start of a word isn't upper-cased, because charAt(0) takes half of it: Deseret 𐐨𐐨 stays as it is, where Python gives 𐐀𐐨.

title() now compiles to pyTitle(s), a helper in $/utils/state next to pyStrip and pyMod, and the var carries the import. It follows CPython's title loop one code point at a time: a character is upper-cased after one that isn't cased (\p{Cased}, the property str.title() goes by) and lower-cased after one that is. A capital sigma gets the final form by the rule lower() uses, which looks past case-ignorable characters on both sides, so ΟΔΟΣ becomes Οδος and ΟΣ'Α becomes Οσ'Α, as in Python.

It follows Python where Python is surprising too: "they're" becomes They'Re and "1st" becomes 1St, as str.title() does. That is what someone who writes State.name.title() gets from the same call in Python, and it's the choice #7638 made for %.

I compared the helper with CPython 3.12's str.title() on 300,000 random strings: 200,000 mostly ASCII letters, spaces, hyphens, apostrophes, digits, tabs and newlines, with characters from all of Unicode mixed in, and 100,000 built from capital and small sigma, other Greek letters and case-ignorable characters. The current expression differs from Python on 186,418 of them, pyTitle on 78. All 78 come from characters whose title case is not their upper case, such as the ligature fi, ß, Greek letters with ypogegrammeni, Georgian letters and the Latin digraphs like dž. JavaScript has no title-case mapping, and the current expression upper-cases those the same way.

#7902 (open) passes title() an empty string in place of None and pins the current expression in one of its tests. The two change different lines, but whichever lands second needs that expected string changed to pyTitle(...).

Tests:

  • test_title_compiles_to_python_title_semantics in tests/units/reflex_base/vars/test_sequence.py checks the compiled expression and the pyTitle import. It fails on main.
  • The reflex-frontend vitest suite tests pyTitle against values taken from Python's str.title(): hyphens, newlines and tabs, apostrophes, digits, mixed case, the final sigma (including across an apostrophe or a combining mark) and letters outside the BMP. Both tests fail on main, and the public API test lists the new export.
  • uv run pytest tests/units (Python 3.12, Linux): 16,984 passed and 75 skipped, with tests/units/utils/test_exec.py run on its own: 65 pass, and test_run_granian_backend_refuses_requests_while_the_app_is_broken fails in my container on main as well (main fails it and one other granian test there). npm --prefix packages/reflex-frontend run check passes, and pre-commit run on the changed files passes.
  • A bugfix news fragment for reflex-base.

I used Claude Code to find this, write the fix and run the comparison above.

🤖 Generated with Claude Code