T90REAL · GitHub

Bug report

Bug description:

UserString.rindex raises a TypeError when its sub argument is a UserString object. This behavior is inconsistent with UserString.rfind and most other string methods.

from collections import UserString
us = UserString("hello world")
sub = UserString("world")
assert us.rfind(sub) == us.rfind(sub.data)
assert us.rindex(sub) == us.rindex(sub.data)
Traceback (most recent call last):
  File "/src/test.py", line 7, in <module>
    assert us.rindex(sub) == us.rindex(sub.data)
           ^^^^^^^^^^^^^^
  File "/py312/lib/python3.12/collections/__init__.py", line 1553, in rindex
    return self.data.rindex(sub, start, end)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: must be str, not UserString

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗