Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions stdlib/@tests/test_cases/check_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import sys
from typing import Any, Generic, TypeVar
from typing_extensions import Self, assert_type


Expand All @@ -19,3 +20,20 @@ def __replace__(self, val: int) -> Self:
obj = ReplaceableClass(42)
cpy = copy.replace(obj, val=23)
assert_type(cpy, ReplaceableClass)


_T_co = TypeVar("_T_co", covariant=True)


class Box(Generic[_T_co]):
def __init__(self, value: _T_co, /) -> None:
self.value = value

def __replace__(self, value: Any) -> Box[Any]:
return Box(value)
Comment thread
jorenham marked this conversation as resolved.
Outdated


if sys.version_info >= (3, 13):
box1: Box[int] = Box(42)
box2 = copy.replace(box1, val="spam")
assert_type(box2, Box[Any])
11 changes: 6 additions & 5 deletions stdlib/copy.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import sys
from typing import Any, Protocol, TypeVar, type_check_only
from typing_extensions import Self
from typing_extensions import ParamSpec

__all__ = ["Error", "copy", "deepcopy"]

_T = TypeVar("_T")
_SR = TypeVar("_SR", bound=_SupportsReplace)
_Tss = ParamSpec("_Tss")
_RT_co = TypeVar("_RT_co", covariant=True)

@type_check_only
class _SupportsReplace(Protocol):
class _SupportsReplace(Protocol[_Tss, _RT_co]):
# In reality doesn't support args, but there's no other great way to express this.
def __replace__(self, *args: Any, **kwargs: Any) -> Self: ...
def __replace__(self, /, *_: _Tss.args, **changes: _Tss.kwargs) -> _RT_co: ...
Comment thread
jorenham marked this conversation as resolved.
Outdated

# None in CPython but non-None in Jython
PyStringMap: Any
Expand All @@ -21,7 +22,7 @@ def copy(x: _T) -> _T: ...

if sys.version_info >= (3, 13):
__all__ += ["replace"]
def replace(obj: _SR, /, **changes: Any) -> _SR: ...
def replace(obj: _SupportsReplace[..., _RT_co], /, **changes: Any) -> _RT_co: ...
Comment thread
jorenham marked this conversation as resolved.
Outdated

class Error(Exception): ...

Expand Down
Loading