Skip to content

Commit

Permalink
Revert "Have namedtuple __replace__ return Self" (#17496)
Browse files Browse the repository at this point in the history
Reverts #17475
  • Loading branch information
ilevkivskyi committed Jul 6, 2024
1 parent ec00fb8 commit 2e0b868
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 18 deletions.
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

0 comments on commit 2e0b868

Please sign in to comment.