Skip to content

Commit

Permalink
Support namedtuple.__replace__ in Python 3.13 (#17259)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed May 18, 2024
1 parent 1c83463 commit c27f4f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit c27f4f5

Please sign in to comment.