Skip to content

collections.UserString.rindex() fails to accept UserString as sub argument #140911

@T90REAL

Description

@T90REAL

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixeseasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions