google-cloud-ndb 2.7.1__tar.gz → 2.7.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. {google_cloud_ndb-2.7.1/google_cloud_ndb.egg-info → google_cloud_ndb-2.7.2}/PKG-INFO +1 -1
  2. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/client.py +95 -12
  3. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/model.py +15 -4
  4. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/version.py +1 -1
  5. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2/google_cloud_ndb.egg-info}/PKG-INFO +1 -1
  6. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/AUTHORS +0 -0
  7. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/LICENSE +0 -0
  8. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/README.md +0 -0
  9. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/__init__.py +0 -0
  10. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_batch.py +0 -0
  11. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_cache.py +0 -0
  12. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_datastore_api.py +0 -0
  13. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_datastore_query.py +0 -0
  14. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_datastore_types.py +0 -0
  15. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_eventloop.py +0 -0
  16. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_gql.py +0 -0
  17. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_legacy_entity_pb.py +0 -0
  18. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_legacy_protocol_buffer.py +0 -0
  19. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_options.py +0 -0
  20. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_remote.py +0 -0
  21. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_retry.py +0 -0
  22. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/_transaction.py +0 -0
  23. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/blobstore.py +0 -0
  24. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/context.py +0 -0
  25. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/django_middleware.py +0 -0
  26. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/exceptions.py +0 -0
  27. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/global_cache.py +0 -0
  28. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/key.py +0 -0
  29. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/metadata.py +0 -0
  30. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/msgprop.py +0 -0
  31. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/polymodel.py +0 -0
  32. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/query.py +0 -0
  33. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/stats.py +0 -0
  34. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/tasklets.py +0 -0
  35. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google/cloud/ndb/utils.py +0 -0
  36. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google_cloud_ndb.egg-info/SOURCES.txt +0 -0
  37. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google_cloud_ndb.egg-info/dependency_links.txt +0 -0
  38. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google_cloud_ndb.egg-info/not-zip-safe +0 -0
  39. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google_cloud_ndb.egg-info/requires.txt +0 -0
  40. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/google_cloud_ndb.egg-info/top_level.txt +0 -0
  41. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/setup.cfg +0 -0
  42. {google_cloud_ndb-2.7.1 → google_cloud_ndb-2.7.2}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: google-cloud-ndb
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: NDB library for Google Cloud Datastore
5
5
  Home-page: https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-ndb
6
6
  Author: Google LLC
@@ -16,11 +16,16 @@
16
16
 
17
17
  import contextlib
18
18
  import os
19
+ from collections.abc import Mapping
20
+ from typing import Any
19
21
 
20
22
  import google.api_core.client_options
21
23
  import grpc
22
24
  import requests
25
+ from google.api_core import universe
23
26
  from google.api_core.gapic_v1 import client_info
27
+ from google.auth import compute_engine
28
+ from google.auth import credentials as auth_credentials
24
29
  from google.cloud.datastore_v1.services.datastore.transports import (
25
30
  grpc as datastore_grpc,
26
31
  )
@@ -31,6 +36,18 @@ from google.cloud.ndb import __version__
31
36
  from google.cloud.ndb import context as context_module
32
37
  from google.cloud.ndb import key as key_module
33
38
 
39
+ try:
40
+ # google.auth.aio was added in google-auth 2.28.0; older supported versions
41
+ # only provide synchronous AnonymousCredentials.
42
+ from google.auth.aio import credentials as _aio_credentials
43
+
44
+ _ANONYMOUS_CREDENTIAL_TYPES: tuple[type, ...] = (
45
+ auth_credentials.AnonymousCredentials,
46
+ _aio_credentials.AnonymousCredentials,
47
+ )
48
+ except ImportError: # pragma: NO COVER
49
+ _ANONYMOUS_CREDENTIAL_TYPES = (auth_credentials.AnonymousCredentials,)
50
+
34
51
  _CLIENT_INFO = client_info.ClientInfo(
35
52
  user_agent="google-cloud-ndb/{}".format(__version__)
36
53
  )
@@ -38,6 +55,64 @@ _CLIENT_INFO = client_info.ClientInfo(
38
55
  DATASTORE_API_HOST = "datastore.googleapis.com"
39
56
 
40
57
 
58
+ # TODO(universe-domain): Replace with
59
+ # google.api_core.universe.resolve_client_universe once
60
+ # testing/constraints-3.10.txt exceeds google-api-core 2.44.0.
61
+ def _resolve_client_universe(client_options=None):
62
+ """Resolve a client's universe domain from ``client_options`` or the environment."""
63
+ if client_options is None:
64
+ options_universe = None
65
+ elif isinstance(client_options, Mapping):
66
+ options_universe = client_options.get("universe_domain")
67
+ elif isinstance(client_options, google.api_core.client_options.ClientOptions):
68
+ options_universe = client_options.universe_domain
69
+ else:
70
+ raise TypeError(
71
+ "client_options must be a ClientOptions instance, Mapping, or None; "
72
+ f"got {type(client_options).__name__}"
73
+ )
74
+ return universe.determine_domain(
75
+ options_universe,
76
+ os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN"),
77
+ ).strip()
78
+
79
+
80
+ # TODO(universe-domain): Replace with
81
+ # google.api_core.universe.validate_credentials_universe once
82
+ # testing/constraints-3.10.txt exceeds google-api-core 2.44.0.
83
+ def _validate_credentials_universe(
84
+ client_universe: str,
85
+ credentials: Any,
86
+ ) -> None:
87
+ """Validate that resolved client credentials match ``client_universe``.
88
+
89
+ Call on ``self._credentials`` after ``super().__init__()`` or Application
90
+ Default Credentials resolution rather than on the raw constructor
91
+ ``credentials`` argument (which is ``None`` before ADC loads), and never
92
+ inside ``Credentials.before_request()``. Exempts ``None``, synchronous and
93
+ asynchronous ``AnonymousCredentials``, and ``compute_engine.Credentials``
94
+ whose universe domain is not yet cached in memory (avoiding synchronous
95
+ Metadata Server HTTP requests during client initialization).
96
+
97
+ Args:
98
+ client_universe: Resolved universe domain of the client.
99
+ credentials: Resolved credentials bound to the client, or ``None`` (in
100
+ which case validation is skipped).
101
+
102
+ Raises:
103
+ google.api_core.universe.UniverseMismatchError: If ``credentials`` has
104
+ an in-memory universe domain configured for a different universe
105
+ than ``client_universe``.
106
+ """
107
+ if credentials is None or isinstance(credentials, _ANONYMOUS_CREDENTIAL_TYPES):
108
+ return
109
+ if isinstance(credentials, compute_engine.Credentials) and not getattr(
110
+ credentials, "_universe_domain_cached", False
111
+ ):
112
+ return
113
+ universe.compare_domains(client_universe, credentials)
114
+
115
+
41
116
  def _get_gcd_project():
42
117
  """Gets the GCD application ID if it can be inferred."""
43
118
  return os.getenv(environment_vars.GCD_DATASET)
@@ -104,27 +179,29 @@ class Client(google_client.ClientWithProject):
104
179
  database=None,
105
180
  ):
106
181
  self.namespace = namespace
107
- self.host = os.environ.get(environment_vars.GCD_HOST, DATASTORE_API_HOST)
108
182
  self.client_info = _CLIENT_INFO
109
183
  self._client_options = client_options
110
184
  self.database = database
111
185
 
112
186
  # Use insecure connection when using Datastore Emulator, otherwise
113
187
  # use secure connection
114
- emulator = bool(os.environ.get(environment_vars.GCD_HOST))
188
+ emulator_host = os.environ.get(environment_vars.GCD_HOST)
189
+ emulator = bool(emulator_host)
115
190
  self.secure = not emulator
116
191
 
117
- # Use Datastore API host from client_options if provided, otherwise use default
118
- api_endpoint = DATASTORE_API_HOST
119
- if client_options is not None:
120
- if isinstance(client_options, dict):
121
- client_options = google.api_core.client_options.from_dict(
122
- client_options
123
- )
124
- if client_options.api_endpoint:
125
- api_endpoint = client_options.api_endpoint
192
+ if isinstance(client_options, Mapping):
193
+ client_options = google.api_core.client_options.from_dict(client_options)
194
+
195
+ universe_domain = _resolve_client_universe(client_options)
196
+ self._universe_domain = universe_domain
197
+
198
+ # Use Datastore API host from client_options if provided, otherwise
199
+ # template using the resolved universe_domain.
200
+ api_endpoint = f"datastore.{universe_domain}"
201
+ if client_options is not None and client_options.api_endpoint:
202
+ api_endpoint = client_options.api_endpoint
126
203
 
127
- self.host = os.environ.get(environment_vars.GCD_HOST, api_endpoint)
204
+ self.host = emulator_host or api_endpoint
128
205
 
129
206
  if emulator:
130
207
  # When using the emulator, in theory, the client shouldn't need to
@@ -153,6 +230,7 @@ class Client(google_client.ClientWithProject):
153
230
  ],
154
231
  )
155
232
  else:
233
+ _validate_credentials_universe(self._universe_domain, self._credentials)
156
234
  user_agent = self.client_info.to_user_agent()
157
235
  channel = _helpers.make_secure_channel(
158
236
  self._credentials, user_agent, self.host
@@ -238,6 +316,11 @@ class Client(google_client.ClientWithProject):
238
316
  # Finish up any work left to do on the event loop
239
317
  context.eventloop.run()
240
318
 
319
+ @property
320
+ def universe_domain(self):
321
+ """The universe domain used by the client."""
322
+ return self._universe_domain
323
+
241
324
  @property
242
325
  def _http(self):
243
326
  """Getter for object used for HTTP transport.
@@ -352,6 +352,17 @@ Rollback = exceptions.Rollback
352
352
  _getfullargspec = inspect.getfullargspec
353
353
 
354
354
 
355
+ def _utcnow():
356
+ """Return the current UTC time as a naive :class:`~datetime.datetime`.
357
+
358
+ ``datetime.datetime.utcnow()`` is deprecated since Python 3.12 and
359
+ scheduled for removal. Its replacement, ``datetime.datetime.now(timezone.utc)``,
360
+ returns an offset-aware value, but ``ndb`` stores and compares naive
361
+ datetimes presumed to be UTC, so the offset is stripped again here.
362
+ """
363
+ return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
364
+
365
+
355
366
  class KindError(exceptions.BadValueError):
356
367
  """Raised when an implementation for a kind can't be found.
357
368
 
@@ -2092,7 +2103,7 @@ class Property(ModelAttribute):
2092
2103
  # A custom 'meaning' for compressed properties.
2093
2104
  _MEANING_URI_COMPRESSED = "ZLIB"
2094
2105
  # The Epoch (a zero POSIX timestamp).
2095
- _EPOCH = datetime.datetime.utcfromtimestamp(0)
2106
+ _EPOCH = datetime.datetime(1970, 1, 1)
2096
2107
  # This is awkward but there seems to be no faster way to inspect
2097
2108
  # what union member is present. datastore_types.FromPropertyPb(),
2098
2109
  # the undisputed authority, has the same series of if-elif blocks.
@@ -3933,7 +3944,7 @@ class DateTimeProperty(Property):
3933
3944
 
3934
3945
  Subclasses will override this to return different forms of "now".
3935
3946
  """
3936
- return datetime.datetime.utcnow()
3947
+ return _utcnow()
3937
3948
 
3938
3949
  def _prepare_for_put(self, entity):
3939
3950
  """Sets the current timestamp when "auto" is set.
@@ -4055,7 +4066,7 @@ class DateProperty(DateTimeProperty):
4055
4066
  @staticmethod
4056
4067
  def _now():
4057
4068
  """datetime.datetime: Return current date."""
4058
- return datetime.datetime.utcnow().date()
4069
+ return _utcnow().date()
4059
4070
 
4060
4071
 
4061
4072
  class TimeProperty(DateTimeProperty):
@@ -4124,7 +4135,7 @@ class TimeProperty(DateTimeProperty):
4124
4135
  @staticmethod
4125
4136
  def _now():
4126
4137
  """datetime.datetime: Return current time."""
4127
- return datetime.datetime.utcnow().time()
4138
+ return _utcnow().time()
4128
4139
 
4129
4140
 
4130
4141
  class StructuredProperty(Property):
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
  #
15
- __version__ = "2.7.1"
15
+ __version__ = "2.7.2"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: google-cloud-ndb
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: NDB library for Google Cloud Datastore
5
5
  Home-page: https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-ndb
6
6
  Author: Google LLC