Skip to content

optype.copy

For the copy standard library, optype.copy provides the following runtime-checkable interfaces:

copy standard library optype.copy
function type method
copy.copy(_) -> R __copy__() -> R CanCopy[+R]
copy.deepcopy(_, memo={}) -> R __deepcopy__(memo, /) -> R CanDeepcopy[+R]
copy.replace(_, /, **changes) -> R __replace__(**changes) -> R CanReplace[+R]

Note

copy.replace requires python>=3.13, but optype.copy.CanReplace is available in all versions of Python.

In practice, it makes sense that a copy of an instance is the same type as the original. But because typing.Self cannot be used as a type argument, this difficult to properly type. Instead, you can use the optype.copy.Can{}Self types, which are the runtime-checkable equivalents of the following (non-expressible) aliases:

type CanCopySelf = CanCopy[Self]
type CanDeepcopySelf = CanDeepcopy[Self]
type CanReplaceSelf = CanReplace[Self]