Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

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

Aberta Para iniciantes
#159,868 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
backend

Direção de pesquisa

Leia Lib/collections/__init__.py e os métodos existentes de UserString que desembrulham .data; em seguida, examine a alteração relacionada na issue #140911 e no PR #140945. Adicione testes específicos para startswith() e endswith() com argumentos UserString, incluindo o caso de reprodução relatado; o trabalho estará concluído quando ambos retornarem True e o comportamento de str comum permanecer inalterado.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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
Linguagem predominante
Python
Estrelas
77.6k
Forks
38.4k
Merge médio
1d 1h
PRs com merge (30d)
589

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.