inflect icon indicating copy to clipboard operation
inflect copied to clipboard

`engine.plural` raises unexpected `IndexError`

Open Harmon758 opened this issue 2 years ago • 4 comments

This might be considered invalid input, but engine.plural accepts multiple words. When doing so, it can raise an unexpected IndexError:

>>> import inflect
>>> engine = inflect.engine()
>>> engine.plural("I'm only here for a minute, John.")
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 "C:\Program Files\Python39\lib\site-packages\inflect\__init__.py", line 2403, in plural
    plural = self.postprocess(
  File "C:\Program Files\Python39\lib\site-packages\inflect\__init__.py", line 2375, in postprocess
    result[index] = result[index].capitalize()
IndexError: list index out of range

This seems to be because engine.postprocess expects the inflected it's passed to be the same number of words as the orig it's passed, but that's not necessarily the case.

Harmon758 avatar Jan 07 '23 12:01 Harmon758

Facing same issue, it's working is strange Screenshot 2023-01-18 at 4 46 54 PM

VeenaPulicharla avatar Jan 18 '23 11:01 VeenaPulicharla

That seems like invalid input to me. What would you expect when pluralizing a phrase?

jaraco avatar Jan 19 '23 03:01 jaraco

@jaraco, I wouldn't expect it to break and could you please explain why it's is not raising the error in the first run and raising in the second run, there is no much difference in the input except spaces

VeenaPulicharla avatar Jan 19 '23 08:01 VeenaPulicharla

could you please explain why it's is not raising the error in the first run and raising in the second run, there is no much difference in the input except spaces

I'm not familiar enough with the implementation to know what's expected here. I would need to trace the inputs through the code and try to infer what the code is trying to do. Harmon has determined that sometimes the length of the words being processed is a mismatch before and after postprocess. There may be a bug there, but I'm not confident that this use case should be supported at all.

I wouldn't expect it to break

I think I would expect it to break, if it can't provide a reasonable response. Perhaps there should be a check and if the input is something unexpected (like pipes or long phrases), it should just raise a ValueError early.

Regardless, this project is community-supported, so I'll leave this one for someone to investigate and propose a solution.

jaraco avatar Jan 20 '23 16:01 jaraco