Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

クローズ 初心者向け
#159,868 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

Lib/collections/__init__.pyと、.dataを取り出す既存のUserStringメソッドを読み、その後、issue #140911とPR #140945の関連する変更を確認してください。UserString引数を使ったstartswith()とendswith()の焦点を絞ったテストを追加し、報告された再現ケースも含めてください。両方がTrueを返し、通常のstrの動作が変わらないことをもって完了とします。

索引モデルが issue の本文から書いたものです。

説明

stdlib type-bug
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
  • gh-159869
主要言語
Python
スター
77.6k
フォーク
38.4k
平均マージ
20時間 19分
マージ済み PR(30日)
624

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。