Skip to content

collections.UserString.replace() rejects the count keyword #161970

Open

Description

@Broyojo

Bug report

Since 3.13, str.replace() accepts count as a keyword argument (gh-106487), and 3.15 did the same for bytes.replace() (gh-147856). collections.UserString.replace() names that parameter maxsplit instead, so the keyword form that works on str raises TypeError on a UserString. It looks like the name was copied from split() a long time ago.

from collections import UserString

print('aaa'.replace('a', 'b', count=1))
print(UserString('aaa').replace('a', 'b', count=1))

Actual:

baa
Traceback (most recent call last):
  ...
TypeError: UserString.replace() got an unexpected keyword argument 'count'

Expected: baa printed twice. (Conversely, UserString('aaa').replace('a', 'b', maxsplit=1) works although str.replace has no such parameter.)

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: corelibs-userstring-replace-count-keyword

Linked PRs

Activity

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