Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namedtuple.__replace__ should return Self #17497

Closed
max-muoto opened this issue Jul 6, 2024 · 2 comments
Closed

namedtuple.__replace__ should return Self #17497

max-muoto opened this issue Jul 6, 2024 · 2 comments
Labels
bug mypy got something wrong topic-named-tuple

Comments

@max-muoto
Copy link
Contributor

__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.

@max-muoto max-muoto added the bug mypy got something wrong label Jul 6, 2024
@ilevkivskyi
Copy link
Member

Do you have an actual test case that fails? Also maybe read a comment on add_method() that says

            ret: Type | None,  # None means use (patched) self-type

@max-muoto
Copy link
Contributor Author

Do you have an actual test case that fails? Also maybe read a comment on add_method() that says

            ret: Type | None,  # None means use (patched) self-type

Apologies, didn't realize that would happen by default - I think this is fine to close out in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-named-tuple
Projects
None yet
Development

No branches or pull requests

3 participants