Exception when processing special character strings
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
When a string is passed to plural which contains only a `'s` it will raise an IndexError Exception:
```python
import inflect
p = inflect.engine()
p.plural("'s")
```
In version 5.6.0:
```
Traceback (most recent call last):
File "", line 1, in
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:
```python
Traceback (most recent call last):
File "", line 1, in
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.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at inflect/__init__.py around lines 3153-3157 and reproduce the failure with p.plural("'s"). Trace how the apostrophe-s input is extracted and validated; done means the call no longer raises an IndexError or validation error and its expected result is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100