FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

collections.UserString.startswith() and endswith() reject UserString arguments · Issue #159868 · python/cpython · GitHub

Repository navigation

collections.UserString.startswith() and endswith() reject UserString arguments #159868

Closed

Description

Bug report

Bug description

collections.UserString.startswith() and endswith() raise TypeError when their prefix or suffix is another UserString.

This is inconsistent with UserString.removeprefix(), removesuffix(), find(), and index(), which accept UserString arguments.

Reproducer

from collections import UserString

text = UserString("hello world")

print(text.removeprefix(UserString("hello")))
print(text.removesuffix(UserString("world")))

for method, argument in (
    ("startswith", UserString("hello")),
    ("endswith", UserString("world")),
):
    try:
        print(method, getattr(text, method)(argument))
    except TypeError as exc:
        print(method, type(exc).__name__, exc)

Actual output

 world
hello 
startswith TypeError startswith first arg must be str or a tuple of str, not UserString
endswith TypeError endswith first arg must be str or a tuple of str, not UserString

Expected behavior

Both prefix/suffix checks return True, consistently with the related UserString methods accepting UserString arguments.

Implementation details

In Lib/collections/__init__.py, UserString.startswith() and endswith() delegate directly to the underlying str without unwrapping the argument’s .data. Related methods explicitly unwrap UserString arguments.

Issue #140911 and PR #140945 fixed a similar inconsistency in UserString.index() and rindex().

This report concerns accepting UserString arguments in UserString methods, not changing str.startswith() or str.endswith() or introducing arbitrary string coercion.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 20, 2026
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 20, 2026
  3. added a commit that references this issue on Oct 21, 2026
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

    stdlibStandard 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


      Back | FazBrowse Home | New Git URL