Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Cerrado Apto para principiantes
#159,868 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Lee Lib/collections/__init__.py y los métodos existentes de UserString que desenvuelven .data, y luego examina el cambio relacionado en issue #140911 y PR #140945. Añade pruebas específicas para startswith() y endswith() con argumentos UserString, incluido el caso de reproducción reportado; el trabajo está terminado cuando ambos devuelvan True y el comportamiento de str ordinario no cambie.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
Python
Estrellas
77.6k
Forks
38.4k
Merge medio
20 h 19 min
PR fusionados (30 d)
624

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.