Skip to content

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

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Nov 2, 2025
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 2, 2025
  3. picnixz commented on Nov 2, 2025

    @picnixz
    Member

    I can see that there is an isinstance check in rfind. It's legitimate to assume that we need one in rindex.

  4. serhiy-storchaka commented on Nov 3, 2025

    @serhiy-storchaka
    Member

    For reference, __contains__, count, find and replace were fixed in 98b349f. But rfind was only fixed in da2ecaf, 2.5 years later. index and rindex were missed again.

  5. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Nov 3, 2025
  6. added a commit that references this issue on Nov 25, 2025
  7. added 2 commits that reference this issue on Nov 25, 2025
  8. added 2 commits that reference this issue on Nov 25, 2025
  9. added a commit that references this issue on Dec 6, 2025
  10. added a commit that references this issue on Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs 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