Skip to content

SERVER_TIMESTAMP should survive deep copies (dataclasses.asdict) #820

@mgraczyk

Description

@mgraczyk

Problem

Currently SERVER_TIMESTAMP does not work if it has been copied via copy.deepcopy.
This is especially important for dataclasses.

Here is an example script to demonstrate the problem

from google.cloud.firestore_v1.transforms import Sentinel from google.cloud.firestore_v1.transforms import SERVER_TIMESTAMP import dataclasses @dataclasses.dataclass class Example: timestamp: type[SERVER_TIMESTAMP] = SERVER_TIMESTAMP example = Example() assert example.timestamp is SERVER_TIMESTAMP asdict = dataclasses.asdict(example) assert asdict['timestamp'] is SERVER_TIMESTAMP

Currently the second assertion fails because the Sentinel has been deep copied.

Traceback (most recent call last): File "/Users/michael/dev/rfp-tool/test.py", line 15, in <module> assert asdict['timestamp'] is SERVER_TIMESTAMP ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError 

Solution

The Sentinel class should define dunder __copy__ and __deepcopy__ methods.

diff --git a/google/cloud/firestore_v1/transforms.py b/google/cloud/firestore_v1/transforms.py index f1361c9..ef0def8 100644 --- a/google/cloud/firestore_v1/transforms.py +++ b/google/cloud/firestore_v1/transforms.py @@ -26,6 +26,15 @@ class Sentinel(object): def __repr__(self): return "Sentinel: {}".format(self.description) + def __copy__(self): + # Sentinel identity should be preserved across copies. + return self + + def __deepcopy__(self): + # Sentinel identity should be preserved across deep copies. + return self + + DELETE_FIELD = Sentinel("Value used to delete a field in a document.") 

I will send a PR attached to this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: firestoreIssues related to the googleapis/python-firestore API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions