Add copy.replace for 3.13 (#12262)

This commit is contained in:
Max Muoto
2024-07-22 20:34:34 -05:00
committed by GitHub
parent 128d8eb904
commit 2909053047
2 changed files with 34 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
from __future__ import annotations
import copy
import sys
from typing_extensions import Self, assert_type
class ReplaceableClass:
def __init__(self, val: int) -> None:
self.val = val
def __replace__(self, val: int) -> Self:
cpy = copy.copy(self)
cpy.val = val
return cpy
if sys.version_info >= (3, 13):
obj = ReplaceableClass(42)
cpy = copy.replace(obj, val=23)
assert_type(cpy, ReplaceableClass)