|
1 | 1 | """Data types for ``dataclassish``.""" |
2 | 2 |
|
3 | | -__all__ = ("DataclassInstance", "CanCopyReplace", "F") |
| 3 | +__all__ = ("DataclassInstance", "F") |
4 | 4 |
|
5 | 5 | from dataclasses import dataclass |
6 | 6 | from typing import Any, ClassVar, Generic, Protocol, TypeVar, runtime_checkable |
7 | | -from typing_extensions import Self |
8 | 7 |
|
9 | 8 |
|
10 | 9 | @runtime_checkable |
@@ -72,65 +71,3 @@ class F(Generic[V]): |
72 | 71 | """ |
73 | 72 |
|
74 | 73 | value: V |
75 | | - |
76 | | - |
77 | | -# =================================================================== |
78 | | - |
79 | | - |
80 | | -@runtime_checkable |
81 | | -class CanCopyReplace(Protocol): |
82 | | - """Protocol for objects that implement the ``__replace__`` method. |
83 | | -
|
84 | | - This is used by ``copy.replace`` (Python 3.13+) to replace fields of an |
85 | | - object. This is a generalization of the ``dataclasses.replace`` function. |
86 | | -
|
87 | | - Examples |
88 | | - -------- |
89 | | - >>> from dataclassish import CanCopyReplace |
90 | | -
|
91 | | - >>> class Point: |
92 | | - ... def __init__(self, x, y): |
93 | | - ... self.x = x |
94 | | - ... self.y = y |
95 | | - ... def __replace__(self, **changes): |
96 | | - ... return Point(**(self.__dict__ | changes)) |
97 | | - ... def __repr__(self): |
98 | | - ... return f"Point(x={self.x}, y={self.y})" |
99 | | -
|
100 | | - >>> issubclass(Point, CanCopyReplace) |
101 | | - True |
102 | | -
|
103 | | - >>> point = Point(1.0, 2.0) |
104 | | - >>> isinstance(point, CanCopyReplace) |
105 | | - True |
106 | | -
|
107 | | - The ``__replace__`` method was introduced in Python 3.13 to bring |
108 | | - ``dataclasses.replace``-like functionality to any implementing object. The |
109 | | - method is publicly exposed via the ``copy.replace`` function. |
110 | | -
|
111 | | - % invisible-code-block: python |
112 | | - % |
113 | | - % import sys |
114 | | -
|
115 | | - % skip: start if(sys.version_info < (3, 13), reason="py3.13+") |
116 | | -
|
117 | | - >>> import copy |
118 | | - >>> copy.replace(point, x=3.0) |
119 | | - Point(x=3.0, y=2.0) |
120 | | -
|
121 | | - % skip: end |
122 | | -
|
123 | | - """ |
124 | | - |
125 | | - def __replace__(self: Self, /, **changes: Any) -> Self: |
126 | | - """Replace the fields of the object. |
127 | | -
|
128 | | - This method should return a new object with the fields replaced. |
129 | | -
|
130 | | - Args: |
131 | | - **changes: Field names and their new values. |
132 | | -
|
133 | | - Returns: |
134 | | - A new object with the specified fields replaced. |
135 | | -
|
136 | | - """ |
0 commit comments