Skip to content

Revert "Have namedtuple __replace__ return Self" #17496

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
TYPED_NAMEDTUPLE_NAMES,
AnyType,
CallableType,
Instance,
LiteralType,
TupleType,
Type,
Expand Down Expand Up @@ -632,10 +631,9 @@ def add_method(
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
)
if self.options.python_version >= (3, 13):
type_vars = [tv for tv in info.defn.type_vars]
add_method(
"__replace__",
ret=Instance(info, type_vars),
ret=None,
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
)

Expand Down
16 changes: 1 addition & 15 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1407,23 +1407,9 @@ from typing import NamedTuple
class A(NamedTuple):
x: int

replaced = A(x=0).__replace__(x=1)
reveal_type(replaced) # N: Revealed type is "__main__.A"

A(x=0).__replace__(x=1)
A(x=0).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "A" has incompatible type "str"; expected "int"
A(x=0).__replace__(y=1) # E: Unexpected keyword argument "y" for "__replace__" of "A"

from typing import TypeVar, Generic

T = TypeVar("T")

class GenericA(NamedTuple, Generic[T]):
x: T

replaced_2 = GenericA(x=0).__replace__(x=1)
reveal_type(replaced_2) # N: Revealed type is "__main__.GenericA"
GenericA(x=0).__replace__(x="abc") # E: Argument "x" to "__replace__" of "GenericA" has incompatible type "str"; expected "int"

[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

Expand Down
Loading