jaraco / inflect

Correctly generate plurals, ordinals, indefinite articles; convert numbers to words
https://pypi.org/project/inflect
MIT License
957 stars 107 forks source link

Exception when processing special character strings #185

Open szelenka opened 1 year ago

szelenka commented 1 year ago

When a string is passed to plural which contains only a 's it will raise an IndexError Exception:

import inflect

p = inflect.engine()
p.plural("'s")

In version 5.6.0:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/_src/sqbu/voicea/wordcloud/.venv/lib/python3.11/site-packages/inflect/__init__.py", line 2376, in plural
    self._pl_special_adjective(word, count)
  File "/opt/_src/sqbu/voicea/wordcloud/.venv/lib/python3.11/site-packages/inflect/__init__.py", line 3102, in _pl_special_adjective
    trailing_s = "" if pl[-1] == "s" else "s"
                       ~~^^^^
IndexError: string index out of range

In version 6.0.4, it changes to a pydantic validation error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pydantic/decorator.py", line 40, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 134, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 206, in pydantic.decorator.ValidatedFunction.execute
  File "/opt/_src/sqbu/voicea/wordcloud/.venv/lib/python3.11/site-packages/inflect/__init__.py", line 2404, in plural
    self._pl_special_adjective(word, count)
  File "/opt/_src/sqbu/voicea/wordcloud/.venv/lib/python3.11/site-packages/inflect/__init__.py", line 3155, in _pl_special_adjective
    pl = self.plural_noun(mo.group(1))
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/decorator.py", line 40, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 133, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 130, in pydantic.decorator.ValidatedFunction.init_model_instance
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for PluralNoun
text
  ensure this value has at least 1 characters (type=value_error.any_str.min_length; limit_value=1)

https://github.com/jaraco/inflect/blob/main/inflect/__init__.py#L3153-L3157

I would expect the method to return an empty string, or otherwise catch this type of error; since technically the input is a two character string (i.e. 's) but the validation strips out some of that to assert.