Skip to content

UserString subtypes parameterless __init__ #131857

@Lordfirespeed

Description

@Lordfirespeed

Description

Subtypes of UserString cannot be initialized without a seq argument as the builtin str can.

Reproduction

from collections import UserString

class MyString(UserString):
    pass
>>> str()
''
>>> MyString()
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    MyString()
    ~~~~~~~~^^
TypeError: UserString.__init__() missing 1 required positional argument: 'seq'

Workaround

from collections import UserString

class MyString(UserString):
    def __init__(self, seq: object = str()) -> None:
        super().__init__(seq)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions