Skip to content

Commit 1161803

Browse files
Fix placeholder deep copy (#611)
* Fix placeholder deep copy * Add annotations, __repr__ and __slots__ Co-authored-by: James Hilton-Balfe <[email protected]> * Fix annotation * Fix typing --------- Co-authored-by: James Hilton-Balfe <[email protected]>
1 parent 8d25c96 commit 1161803

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/betterproto/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,22 @@ class Casing(builtin_enum.Enum):
169169
SNAKE = snake_case #: A snake_case sterilization function.
170170

171171

172-
PLACEHOLDER: Any = object()
172+
class Placeholder:
173+
__slots__ = ()
174+
175+
def __repr__(self) -> str:
176+
return "<PLACEHOLDER>"
177+
178+
def __copy__(self) -> Self:
179+
return self
180+
181+
def __deepcopy__(self, _) -> Self:
182+
return self
183+
184+
185+
# We can't simply use object() here because pydantic automatically performs deep-copy of mutable default values
186+
# See #606
187+
PLACEHOLDER: Any = Placeholder()
173188

174189

175190
@dataclasses.dataclass(frozen=True)
@@ -206,7 +221,7 @@ def dataclass_field(
206221
) -> dataclasses.Field:
207222
"""Creates a dataclass field with attached protobuf metadata."""
208223
return dataclasses.field(
209-
default=None if optional else PLACEHOLDER,
224+
default=None if optional else PLACEHOLDER, # type: ignore
210225
metadata={
211226
"betterproto": FieldMetadata(
212227
number, proto_type, map_types, group, wraps, optional

0 commit comments

Comments
 (0)