Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 4332378

Browse files
feat: add context manager support in client (#156)
- [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: googleapis/googleapis@787f8c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 95025fa commit 4332378

15 files changed

Lines changed: 187 additions & 4 deletions

File tree

google/cloud/datalabeling_v1beta1/services/data_labeling_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,6 +3264,12 @@ async def list_evaluation_jobs(
32643264
# Done; return the response.
32653265
return response
32663266

3267+
async def __aenter__(self):
3268+
return self
3269+
3270+
async def __aexit__(self, exc_type, exc, tb):
3271+
await self.transport.close()
3272+
32673273

32683274
try:
32693275
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/datalabeling_v1beta1/services/data_labeling_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,7 @@ def __init__(
479479
client_cert_source_for_mtls=client_cert_source_func,
480480
quota_project_id=client_options.quota_project_id,
481481
client_info=client_info,
482-
always_use_jwt_access=(
483-
Transport == type(self).get_transport_class("grpc")
484-
or Transport == type(self).get_transport_class("grpc_asyncio")
485-
),
482+
always_use_jwt_access=True,
486483
)
487484

488485
def create_dataset(
@@ -3355,6 +3352,19 @@ def list_evaluation_jobs(
33553352
# Done; return the response.
33563353
return response
33573354

3355+
def __enter__(self):
3356+
return self
3357+
3358+
def __exit__(self, type, value, traceback):
3359+
"""Releases underlying transport's resources.
3360+
3361+
.. warning::
3362+
ONLY use as a context manager if the transport is NOT shared
3363+
with other clients! Exiting the with block will CLOSE the transport
3364+
and may cause errors in other clients!
3365+
"""
3366+
self.transport.close()
3367+
33583368

33593369
try:
33603370
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/datalabeling_v1beta1/services/data_labeling_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,15 @@ def _prep_wrapped_messages(self, client_info):
537537
),
538538
}
539539

540+
def close(self):
541+
"""Closes resources associated with the transport.
542+
543+
.. warning::
544+
Only call this method if the transport is NOT shared
545+
with other clients - this may cause errors in other clients!
546+
"""
547+
raise NotImplementedError()
548+
540549
@property
541550
def operations_client(self) -> operations_v1.OperationsClient:
542551
"""Return the client designed to process long-running operations."""

google/cloud/datalabeling_v1beta1/services/data_labeling_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,5 +1224,8 @@ def list_evaluation_jobs(
12241224
)
12251225
return self._stubs["list_evaluation_jobs"]
12261226

1227+
def close(self):
1228+
self.grpc_channel.close()
1229+
12271230

12281231
__all__ = ("DataLabelingServiceGrpcTransport",)

google/cloud/datalabeling_v1beta1/services/data_labeling_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,5 +1266,8 @@ def list_evaluation_jobs(
12661266
)
12671267
return self._stubs["list_evaluation_jobs"]
12681268

1269+
def close(self):
1270+
return self.grpc_channel.close()
1271+
12691272

12701273
__all__ = ("DataLabelingServiceGrpcAsyncIOTransport",)

google/cloud/datalabeling_v1beta1/types/annotation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Annotation(proto.Message):
121121

122122
class AnnotationValue(proto.Message):
123123
r"""Annotation value for an example.
124+
124125
Attributes:
125126
image_classification_annotation (google.cloud.datalabeling_v1beta1.types.ImageClassificationAnnotation):
126127
Annotation value for image classification
@@ -204,6 +205,7 @@ class AnnotationValue(proto.Message):
204205

205206
class ImageClassificationAnnotation(proto.Message):
206207
r"""Image classification annotation definition.
208+
207209
Attributes:
208210
annotation_spec (google.cloud.datalabeling_v1beta1.types.AnnotationSpec):
209211
Label of image.
@@ -248,6 +250,7 @@ class NormalizedVertex(proto.Message):
248250

249251
class BoundingPoly(proto.Message):
250252
r"""A bounding polygon in the image.
253+
251254
Attributes:
252255
vertices (Sequence[google.cloud.datalabeling_v1beta1.types.Vertex]):
253256
The bounding polygon vertices.
@@ -258,6 +261,7 @@ class BoundingPoly(proto.Message):
258261

259262
class NormalizedBoundingPoly(proto.Message):
260263
r"""Normalized bounding polygon.
264+
261265
Attributes:
262266
normalized_vertices (Sequence[google.cloud.datalabeling_v1beta1.types.NormalizedVertex]):
263267
The bounding polygon normalized vertices.
@@ -294,6 +298,7 @@ class ImageBoundingPolyAnnotation(proto.Message):
294298

295299
class Polyline(proto.Message):
296300
r"""A line with multiple line segments.
301+
297302
Attributes:
298303
vertices (Sequence[google.cloud.datalabeling_v1beta1.types.Vertex]):
299304
The polyline vertices.
@@ -304,6 +309,7 @@ class Polyline(proto.Message):
304309

305310
class NormalizedPolyline(proto.Message):
306311
r"""Normalized polyline.
312+
307313
Attributes:
308314
normalized_vertices (Sequence[google.cloud.datalabeling_v1beta1.types.NormalizedVertex]):
309315
The normalized polyline vertices.
@@ -316,6 +322,7 @@ class NormalizedPolyline(proto.Message):
316322

317323
class ImagePolylineAnnotation(proto.Message):
318324
r"""A polyline for the image annotation.
325+
319326
Attributes:
320327
polyline (google.cloud.datalabeling_v1beta1.types.Polyline):
321328
@@ -336,6 +343,7 @@ class ImagePolylineAnnotation(proto.Message):
336343

337344
class ImageSegmentationAnnotation(proto.Message):
338345
r"""Image segmentation annotation.
346+
339347
Attributes:
340348
annotation_colors (Sequence[google.cloud.datalabeling_v1beta1.types.ImageSegmentationAnnotation.AnnotationColorsEntry]):
341349
The mapping between rgb color and annotation
@@ -360,6 +368,7 @@ class ImageSegmentationAnnotation(proto.Message):
360368

361369
class TextClassificationAnnotation(proto.Message):
362370
r"""Text classification annotation.
371+
363372
Attributes:
364373
annotation_spec (google.cloud.datalabeling_v1beta1.types.AnnotationSpec):
365374
Label of the text.
@@ -372,6 +381,7 @@ class TextClassificationAnnotation(proto.Message):
372381

373382
class TextEntityExtractionAnnotation(proto.Message):
374383
r"""Text entity extraction annotation.
384+
375385
Attributes:
376386
annotation_spec (google.cloud.datalabeling_v1beta1.types.AnnotationSpec):
377387
Label of the text entities.
@@ -389,6 +399,7 @@ class TextEntityExtractionAnnotation(proto.Message):
389399

390400
class SequentialSegment(proto.Message):
391401
r"""Start and end position in a sequence (e.g. text segment).
402+
392403
Attributes:
393404
start (int):
394405
Start position (inclusive).
@@ -425,6 +436,7 @@ class TimeSegment(proto.Message):
425436

426437
class VideoClassificationAnnotation(proto.Message):
427438
r"""Video classification annotation.
439+
428440
Attributes:
429441
time_segment (google.cloud.datalabeling_v1beta1.types.TimeSegment):
430442
The time segment of the video to which the
@@ -464,6 +476,7 @@ class ObjectTrackingFrame(proto.Message):
464476

465477
class VideoObjectTrackingAnnotation(proto.Message):
466478
r"""Video object tracking annotation.
479+
467480
Attributes:
468481
annotation_spec (google.cloud.datalabeling_v1beta1.types.AnnotationSpec):
469482
Label of the object tracked in this
@@ -487,6 +500,7 @@ class VideoObjectTrackingAnnotation(proto.Message):
487500

488501
class VideoEventAnnotation(proto.Message):
489502
r"""Video event annotation.
503+
490504
Attributes:
491505
annotation_spec (google.cloud.datalabeling_v1beta1.types.AnnotationSpec):
492506
Label of the event in this annotation.
@@ -503,6 +517,7 @@ class VideoEventAnnotation(proto.Message):
503517

504518
class AnnotationMetadata(proto.Message):
505519
r"""Additional information associated with the annotation.
520+
506521
Attributes:
507522
operator_metadata (google.cloud.datalabeling_v1beta1.types.OperatorMetadata):
508523
Metadata related to human labeling.

0 commit comments

Comments
 (0)