Skip to content

Wrong value returned for new object of a class derived from list #139172

@mhirasuna

Description

@mhirasuna

Bug report

Bug description:

# Add a code block here, if required
class L (list):
    def __new__ (cls,value):
        if isinstance(value,str):
            if ',' in value:
                collect = []
                for item in value.split(','):
                    collect.append(item)
                value = collect
        return super().__new__(cls,value)

print(L("1,2"))

Expected output: ['1', '2']
Got instead: ['1', ',', '2']

If I don't cast the return value, I get the correct value but the type would not be what I want.

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions