Skip to content

Commit 6ba4ca7

Browse files
committed
copy.replace regression test for asymmetric __replace__
1 parent b93847d commit 6ba4ca7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

stdlib/@tests/test_cases/check_copy.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import copy
44
import sys
5+
from typing import Any, Generic, TypeVar
56
from typing_extensions import Self, assert_type
67

78

@@ -19,3 +20,20 @@ def __replace__(self, val: int) -> Self:
1920
obj = ReplaceableClass(42)
2021
cpy = copy.replace(obj, val=23)
2122
assert_type(cpy, ReplaceableClass)
23+
24+
25+
_T_co = TypeVar("_T_co", covariant=True)
26+
27+
28+
class Box(Generic[_T_co]):
29+
def __init__(self, value: _T_co, /) -> None:
30+
self.value = value
31+
32+
def __replace__(self, value: Any) -> Box[Any]:
33+
return Box(value)
34+
35+
36+
if sys.version_info >= (3, 13):
37+
box1: Box[int] = Box(42)
38+
box2 = copy.replace(box1, val="spam")
39+
assert_type(box2, Box[Any])

0 commit comments

Comments
 (0)