Skip to content

Commit c27f4f5

Browse files
authored
Support namedtuple.__replace__ in Python 3.13 (#17259)
1 parent 1c83463 commit c27f4f5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mypy/semanal_namedtuple.py

+6
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ def add_method(
623623
ret=selftype,
624624
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
625625
)
626+
if self.options.python_version >= (3, 13):
627+
add_method(
628+
"__replace__",
629+
ret=selftype,
630+
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
631+
)
626632

627633
def make_init_arg(var: Var) -> Argument:
628634
default = default_items.get(var.name, None)

test-data/unit/check-namedtuple.test

+14
Original file line numberDiff line numberDiff line change
@@ -1398,3 +1398,17 @@ class Test3(NamedTuple, metaclass=type): # E: Unexpected keyword argument "meta
13981398
...
13991399
[builtins fixtures/tuple.pyi]
14001400
[typing fixtures/typing-namedtuple.pyi]
1401+
1402+
1403+
[case testNamedTupleDunderReplace]
1404+
# flags: --python-version 3.13
1405+
from typing import NamedTuple
1406+
1407+
class A(NamedTuple):
1408+
x: int
1409+
1410+
A(x=0).__replace__(x=1)
1411+
A(x=0).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "A" has incompatible type "str"; expected "int"
1412+
A(x=0).__replace__(y=1) # E: Unexpected keyword argument "y" for "__replace__" of "A"
1413+
[builtins fixtures/tuple.pyi]
1414+
[typing fixtures/typing-namedtuple.pyi]

0 commit comments

Comments
 (0)