Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion inflect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3007,8 +3007,14 @@ def _handle_long_compounds(self, word: Words, count: int) -> Union[str, None]:
Handles the plural and singular for compound `Words` that
have three or more words, based on the given count.

When the candidate word is already in the requested form,
``self._sinoun`` / ``self._plnoun`` returns ``False``; fall back
to the original word so the joined phrase remains a string. (#222)

>>> engine()._handle_long_compounds(Words("pair of scissors"), 2)
'pairs of scissors'
>>> engine()._handle_long_compounds(Words("pair of scissors"), 1)
'pair of scissors'
>>> engine()._handle_long_compounds(Words("men beyond hills"), 1)
'man beyond hills'
"""
Expand All @@ -3017,7 +3023,7 @@ def _handle_long_compounds(self, word: Words, count: int) -> Union[str, None]:
" ".join(
itertools.chain(
leader,
[inflection(cand, count), prep], # type: ignore[operator]
[inflection(cand, count) or cand, prep],
trailer,
)
)
Expand Down
5 changes: 5 additions & 0 deletions newsfragments/222.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix ``TypeError`` raised by ``singular_noun`` for compound phrases ending
in an uninflected word (e.g. ``"pair of scissors"``). When the candidate
word is already singular, ``_sinoun`` returns ``False``; the long-compound
handler now falls back to the original word so the joined phrase remains
a string.
12 changes: 12 additions & 0 deletions tests/test_compounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def test_compound_4():
assert p.singular_noun("case of diapers") == "case of diapers"


def test_singular_noun_already_singular_long_compound():
"""
Compound phrases routed through ``_handle_long_compounds`` whose
candidate word is already singular previously raised ``TypeError``
because ``_sinoun`` returned ``False`` and that fell into ``str.join``.
See https://github.com/jaraco/inflect/issues/222.
"""
assert p.singular_noun("pair of scissors") == "pair of scissors"
# The pluralized form should still round-trip back to the singular.
assert p.singular_noun("pairs of scissors") == "pair of scissors"


def test_unit_handling_degree():
test_cases = {
"degree celsius": "degrees celsius",
Expand Down