Skip to content

Fix str_title_case skipping later lowercase-exception words after a leading occurrence#371

Open
patchwright wants to merge 1 commit into
jkwill87:mainfrom
patchwright:fix-title-case-repeated-exception
Open

Fix str_title_case skipping later lowercase-exception words after a leading occurrence#371
patchwright wants to merge 1 commit into
jkwill87:mainfrom
patchwright:fix-title-case-repeated-exception

Conversation

@patchwright

Copy link
Copy Markdown

Problem

In str_title_case, the lowercase-exception pass bails out of the whole
loop the first time a match is found at the start of the string:

for pos in findall(string_lower, exception):
    is_start = pos < 2
    if is_start:
        break        # aborts every remaining occurrence of this word

findall yields every position of the exception word. The intent of the
is_start guard is to leave the leading word capitalized — but break
abandons all later occurrences too, so a small word that appears once at the
start and again later never gets lowercased:

>>> from mnamer.utils import str_title_case
>>> str_title_case("the cat in the hat")
'The Cat in The Hat'      # expected: 'The Cat in the Hat'
>>> str_title_case("a man and a plan")
'A Man and A Plan'        # expected: 'A Man and a Plan'

Fix

Use continue instead of break, so only the leading occurrence is skipped
and later ones are still processed. One-word change.

Tests

Added test_str_title_case__lower__repeated_after_start covering a
lowercase-exception word that recurs after the start (the cat in the hat,
its uppercase variant, and a man and a plan). Verified it fails on the
previous code and passes with the fix. The full local suite (347 tests)
passes, and ruff check / ruff format --check are clean.

The lowercase-exception pass used `break` when an exception word matched
at the start of the string, which abandoned every *later* occurrence of
that word instead of just the leading one. A small word appearing once at
the start and again later was therefore never lowercased:

    str_title_case('the cat in the hat')  -> 'The Cat in The Hat'
    str_title_case('a man and a plan')    -> 'A Man and A Plan'

Use `continue` so only the leading occurrence is skipped. Adds a
regression test covering a lowercase-exception word that recurs after the
start (verified to fail on the previous code and pass with the fix).
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.

1 participant