Skip to content

namedtuple.__replace__ should return Self #17497

Closed
@max-muoto

Description

@max-muoto

__replace__ was added onto NamedTuple as a part of 3.13 related changes, however, instead of returning None, it should return Self. Looking in cpython we can see this is the case.

Furthermore, this is important for compatibility with copy.replace. In python/typeshed#12262 we're adding a protocol that requires replaceable objects implement:

class _SupportsReplace(Protocol):
    def __replace__(self, *args: Any, **kwargs: Any) -> Self: ...

With namedtuple.__replace__ returning None, users will return into false positives here, when in reality it should work just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions