fix(strutils): stop singularize() mangling words ending in 'ss'#418
Open
Sanjays2402 wants to merge 1 commit into
Open
fix(strutils): stop singularize() mangling words ending in 'ss'#418Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
singularize() blindly stripped the trailing 's' from any word ending in
's' that did not match the earlier '-ies'/'-ses' branches, so words that
are already singular and end in a double 's' were corrupted:
singularize('glass') -> 'glas'
singularize('boss') -> 'bos'
singularize('class') -> 'clas'
singularize('kiss') -> 'kis'
singularize('address')-> 'addres'
No English plural ends in 'ss' -- the plural of these words ends in
'sses' (glass -> glasses), which is already handled by the preceding
`word.endswith('es') and word[-3] == 's'` branch. This also broke the
idempotency implied by the function's own docstring, which states
singularize('Glasses') == 'Glass': feeding that canonical singular back
in produced 'Glas', and applying singularize() defensively to an
already-singular word degraded it further ('boss' -> 'bos' -> 'bo').
Fix: add an `elif word.endswith('ss')` guard before the final
`word[:-1]` fallback that returns the word unchanged. Real '-sses'
plurals are unaffected because that branch runs first, so
singularize('glasses') still returns 'glass'. The single-'s' ambiguous
cases (e.g. 'bus') are intentionally left alone; they are
indistinguishable from a regular plural without a dictionary.
Adds test_singularize_double_s covering the double-'s' singular words,
case preservation, the still-correct '-sses' plurals, and idempotency.
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.
Summary
strutils.singularize()corrupts words that are already singular and end in a doubles(glass,boss,class,kiss,address,business, ...), returningglas,bos,clas,kis,addres,busines.Root cause
singularize()ends with a catch-all that strips the final character from any word ending insthat didn't match the-ies/-sesbranches:No English plural ends in
ss. The plural of these words ends insses(glass->glasses), which is already handled correctly by the precedingword.endswith('es') and word[-3] == 's'branch. Only words that are already singular and end inssfall through to the blindword[:-1].This also violated the idempotency implied by the function's own docstring (
singularize('Glasses') == 'Glass'): feeding that canonical singular back in produced'Glas', and applyingsingularize()defensively to an already-singular word degraded it further ('boss'->'bos'->'bo').Fix
Add an
elif word.endswith('ss')guard before the final fallback that returns the word unchanged (+7 lines). Because the-ssesbranch runs first, real plurals are unaffected.Before / after
glassglasglassbossbosbossclassclasclasskisskiskissaddressaddresaddressbusinessbusinesbusinessglasses(plural)glassglass(unchanged)bosses(plural)bossboss(unchanged)chances,cats,activitiesThe single-
sambiguous cases (e.g.bus) are intentionally left alone — they are indistinguishable from a regular plural (cats->cat) without a dictionary, so this PR does not touch them.Tests
Added
test_singularize_double_scovering the double-ssingular words, case preservation (Glass,BOSS), the still-correct-ssesplurals, and idempotency.Proven to guard the bug (regression test fails on unpatched source, passes with the fix):
Full suite: 438 passed (was 437). All 28
strutilsdocstring doctests pass. No linter is configured in CI; the change is pure stdlib control flow valid on every supported Python (3.7-3.14).