diff --git a/mypy/semanal_namedtuple.py b/mypy/semanal_namedtuple.py index f051c4ee36e9..753deafe103b 100644 --- a/mypy/semanal_namedtuple.py +++ b/mypy/semanal_namedtuple.py @@ -623,6 +623,12 @@ def add_method( ret=selftype, args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars], ) + if self.options.python_version >= (3, 13): + add_method( + "__replace__", + ret=selftype, + args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars], + ) def make_init_arg(var: Var) -> Argument: default = default_items.get(var.name, None) diff --git a/test-data/unit/check-namedtuple.test b/test-data/unit/check-namedtuple.test index 5e7c730162d8..a0d984b30279 100644 --- a/test-data/unit/check-namedtuple.test +++ b/test-data/unit/check-namedtuple.test @@ -1398,3 +1398,17 @@ class Test3(NamedTuple, metaclass=type): # E: Unexpected keyword argument "meta ... [builtins fixtures/tuple.pyi] [typing fixtures/typing-namedtuple.pyi] + + +[case testNamedTupleDunderReplace] +# flags: --python-version 3.13 +from typing import NamedTuple + +class A(NamedTuple): + x: int + +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" +[builtins fixtures/tuple.pyi] +[typing fixtures/typing-namedtuple.pyi]