From 6e750e8d655cc6ae7967ff5e105b6f64dd05c4c3 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:42:27 +0000 Subject: [PATCH] feat: add context manager support in client (#267) - [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: https://github.com/googleapis/googleapis/commit/787f8c9a731f44e74a90b9847d48659ca9462d10 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9 --- .../translation_service/async_client.py | 6 +++ .../services/translation_service/client.py | 18 +++++-- .../translation_service/transports/base.py | 9 ++++ .../translation_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../translate_v3/types/translation_service.py | 32 ++++++++++++ .../translation_service/async_client.py | 6 +++ .../services/translation_service/client.py | 18 +++++-- .../translation_service/transports/base.py | 9 ++++ .../translation_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/translation_service.py | 32 ++++++++++++ .../translate_v3/test_translation_service.py | 50 +++++++++++++++++++ .../test_translation_service.py | 50 +++++++++++++++++++ 14 files changed, 234 insertions(+), 8 deletions(-) diff --git a/google/cloud/translate_v3/services/translation_service/async_client.py b/google/cloud/translate_v3/services/translation_service/async_client.py index 731ac806..6ce5fc70 100644 --- a/google/cloud/translate_v3/services/translation_service/async_client.py +++ b/google/cloud/translate_v3/services/translation_service/async_client.py @@ -1123,6 +1123,12 @@ async def delete_glossary( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/translate_v3/services/translation_service/client.py b/google/cloud/translate_v3/services/translation_service/client.py index d3904047..0816e3f6 100644 --- a/google/cloud/translate_v3/services/translation_service/client.py +++ b/google/cloud/translate_v3/services/translation_service/client.py @@ -349,10 +349,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def translate_text( @@ -1276,6 +1273,19 @@ def delete_glossary( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/translate_v3/services/translation_service/transports/base.py b/google/cloud/translate_v3/services/translation_service/transports/base.py index b4208c1a..c4ade0c9 100644 --- a/google/cloud/translate_v3/services/translation_service/transports/base.py +++ b/google/cloud/translate_v3/services/translation_service/transports/base.py @@ -241,6 +241,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/translate_v3/services/translation_service/transports/grpc.py b/google/cloud/translate_v3/services/translation_service/transports/grpc.py index 0afb71eb..57e64267 100644 --- a/google/cloud/translate_v3/services/translation_service/transports/grpc.py +++ b/google/cloud/translate_v3/services/translation_service/transports/grpc.py @@ -549,5 +549,8 @@ def delete_glossary( ) return self._stubs["delete_glossary"] + def close(self): + self.grpc_channel.close() + __all__ = ("TranslationServiceGrpcTransport",) diff --git a/google/cloud/translate_v3/services/translation_service/transports/grpc_asyncio.py b/google/cloud/translate_v3/services/translation_service/transports/grpc_asyncio.py index fd726d9d..b0153f6a 100644 --- a/google/cloud/translate_v3/services/translation_service/transports/grpc_asyncio.py +++ b/google/cloud/translate_v3/services/translation_service/transports/grpc_asyncio.py @@ -557,5 +557,8 @@ def delete_glossary( ) return self._stubs["delete_glossary"] + def close(self): + return self.grpc_channel.close() + __all__ = ("TranslationServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/translate_v3/types/translation_service.py b/google/cloud/translate_v3/types/translation_service.py index b84ba2d6..eb582334 100644 --- a/google/cloud/translate_v3/types/translation_service.py +++ b/google/cloud/translate_v3/types/translation_service.py @@ -86,6 +86,7 @@ class TranslateTextGlossaryConfig(proto.Message): class TranslateTextRequest(proto.Message): r"""The request message for synchronous translation. + Attributes: contents (Sequence[str]): Required. The content of the input in string @@ -176,6 +177,7 @@ class TranslateTextRequest(proto.Message): class TranslateTextResponse(proto.Message): r""" + Attributes: translations (Sequence[google.cloud.translate_v3.types.Translation]): Text translation responses with no glossary applied. This @@ -197,6 +199,7 @@ class TranslateTextResponse(proto.Message): class Translation(proto.Message): r"""A single translation response. + Attributes: translated_text (str): Text translated into the target language. @@ -232,6 +235,7 @@ class Translation(proto.Message): class DetectLanguageRequest(proto.Message): r"""The request message for language detection. + Attributes: parent (str): Required. Project or location to make a call. Must refer to @@ -287,6 +291,7 @@ class DetectLanguageRequest(proto.Message): class DetectedLanguage(proto.Message): r"""The response message for language detection. + Attributes: language_code (str): The BCP-47 language code of source content in @@ -302,6 +307,7 @@ class DetectedLanguage(proto.Message): class DetectLanguageResponse(proto.Message): r"""The response message for language detection. + Attributes: languages (Sequence[google.cloud.translate_v3.types.DetectedLanguage]): The most probable language detected by the @@ -317,6 +323,7 @@ class DetectLanguageResponse(proto.Message): class GetSupportedLanguagesRequest(proto.Message): r"""The request message for discovering supported languages. + Attributes: parent (str): Required. Project or location to make a call. Must refer to @@ -362,6 +369,7 @@ class GetSupportedLanguagesRequest(proto.Message): class SupportedLanguages(proto.Message): r"""The response message for discovering supported languages. + Attributes: languages (Sequence[google.cloud.translate_v3.types.SupportedLanguage]): A list of supported language responses. This @@ -403,6 +411,7 @@ class SupportedLanguage(proto.Message): class GcsSource(proto.Message): r"""The Google Cloud Storage location for the input content. + Attributes: input_uri (str): Required. Source data URI. For example, @@ -414,6 +423,7 @@ class GcsSource(proto.Message): class InputConfig(proto.Message): r"""Input configuration for BatchTranslateText request. + Attributes: mime_type (str): Optional. Can be "text/plain" or "text/html". For ``.tsv``, @@ -450,6 +460,7 @@ class InputConfig(proto.Message): class GcsDestination(proto.Message): r"""The Google Cloud Storage location for the output content. + Attributes: output_uri_prefix (str): Required. The bucket used in 'output_uri_prefix' must exist @@ -465,6 +476,7 @@ class GcsDestination(proto.Message): class OutputConfig(proto.Message): r"""Output configuration for BatchTranslateText request. + Attributes: gcs_destination (google.cloud.translate_v3.types.GcsDestination): Google Cloud Storage destination for output content. For @@ -552,6 +564,7 @@ class OutputConfig(proto.Message): class DocumentInputConfig(proto.Message): r"""A document translation request input config. + Attributes: content (bytes): Document's content represented as a stream of @@ -582,6 +595,7 @@ class DocumentInputConfig(proto.Message): class DocumentOutputConfig(proto.Message): r"""A document translation request output config. + Attributes: gcs_destination (google.cloud.translate_v3.types.GcsDestination): Optional. Google Cloud Storage destination for the @@ -653,6 +667,7 @@ class DocumentOutputConfig(proto.Message): class TranslateDocumentRequest(proto.Message): r"""A document translation request. + Attributes: parent (str): Required. Location to make a regional call. @@ -744,6 +759,7 @@ class TranslateDocumentRequest(proto.Message): class DocumentTranslation(proto.Message): r"""A translated document message. + Attributes: byte_stream_outputs (Sequence[bytes]): The array of translated documents. It is @@ -769,6 +785,7 @@ class DocumentTranslation(proto.Message): class TranslateDocumentResponse(proto.Message): r"""A translated document response message. + Attributes: document_translation (google.cloud.translate_v3.types.DocumentTranslation): Translated document. @@ -804,6 +821,7 @@ class TranslateDocumentResponse(proto.Message): class BatchTranslateTextRequest(proto.Message): r"""The batch translation request. + Attributes: parent (str): Required. Location to make a call. Must refer to a caller's @@ -880,6 +898,7 @@ class BatchTranslateTextRequest(proto.Message): class BatchTranslateMetadata(proto.Message): r"""State metadata for the batch translation operation. + Attributes: state (google.cloud.translate_v3.types.BatchTranslateMetadata.State): The state of the operation. @@ -948,6 +967,7 @@ class BatchTranslateResponse(proto.Message): class GlossaryInputConfig(proto.Message): r"""Input configuration for glossaries. + Attributes: gcs_source (google.cloud.translate_v3.types.GcsSource): Required. Google Cloud Storage location of glossary data. @@ -983,6 +1003,7 @@ class GlossaryInputConfig(proto.Message): class Glossary(proto.Message): r"""Represents a glossary built from user provided data. + Attributes: name (str): Required. The resource name of the glossary. Glossary names @@ -1008,6 +1029,7 @@ class Glossary(proto.Message): class LanguageCodePair(proto.Message): r"""Used with unidirectional glossaries. + Attributes: source_language_code (str): Required. The BCP-47 language code of the input text, for @@ -1024,6 +1046,7 @@ class LanguageCodePair(proto.Message): class LanguageCodesSet(proto.Message): r"""Used with equivalent term set glossaries. + Attributes: language_codes (Sequence[str]): The BCP-47 language code(s) for terms defined in the @@ -1049,6 +1072,7 @@ class LanguageCodesSet(proto.Message): class CreateGlossaryRequest(proto.Message): r"""Request message for CreateGlossary. + Attributes: parent (str): Required. The project name. @@ -1062,6 +1086,7 @@ class CreateGlossaryRequest(proto.Message): class GetGlossaryRequest(proto.Message): r"""Request message for GetGlossary. + Attributes: name (str): Required. The name of the glossary to @@ -1073,6 +1098,7 @@ class GetGlossaryRequest(proto.Message): class DeleteGlossaryRequest(proto.Message): r"""Request message for DeleteGlossary. + Attributes: name (str): Required. The name of the glossary to delete. @@ -1083,6 +1109,7 @@ class DeleteGlossaryRequest(proto.Message): class ListGlossariesRequest(proto.Message): r"""Request message for ListGlossaries. + Attributes: parent (str): Required. The name of the project from which @@ -1133,6 +1160,7 @@ class ListGlossariesRequest(proto.Message): class ListGlossariesResponse(proto.Message): r"""Response message for ListGlossaries. + Attributes: glossaries (Sequence[google.cloud.translate_v3.types.Glossary]): The list of glossaries for a project. @@ -1241,6 +1269,7 @@ class DeleteGlossaryResponse(proto.Message): class BatchTranslateDocumentRequest(proto.Message): r"""The BatchTranslateDocument request. + Attributes: parent (str): Required. Location to make a regional call. @@ -1326,6 +1355,7 @@ class BatchTranslateDocumentRequest(proto.Message): class BatchDocumentInputConfig(proto.Message): r"""Input configuration for BatchTranslateDocument request. + Attributes: gcs_source (google.cloud.translate_v3.types.GcsSource): Google Cloud Storage location for the source input. This can @@ -1357,6 +1387,7 @@ class BatchDocumentInputConfig(proto.Message): class BatchDocumentOutputConfig(proto.Message): r"""Output configuration for BatchTranslateDocument request. + Attributes: gcs_destination (google.cloud.translate_v3.types.GcsDestination): Google Cloud Storage destination for output content. For @@ -1475,6 +1506,7 @@ class BatchTranslateDocumentResponse(proto.Message): class BatchTranslateDocumentMetadata(proto.Message): r"""State metadata for the batch translation operation. + Attributes: state (google.cloud.translate_v3.types.BatchTranslateDocumentMetadata.State): The state of the operation. diff --git a/google/cloud/translate_v3beta1/services/translation_service/async_client.py b/google/cloud/translate_v3beta1/services/translation_service/async_client.py index 27028461..46f7e2fd 100644 --- a/google/cloud/translate_v3beta1/services/translation_service/async_client.py +++ b/google/cloud/translate_v3beta1/services/translation_service/async_client.py @@ -1027,6 +1027,12 @@ async def delete_glossary( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/translate_v3beta1/services/translation_service/client.py b/google/cloud/translate_v3beta1/services/translation_service/client.py index 26dad37b..fbec4c57 100644 --- a/google/cloud/translate_v3beta1/services/translation_service/client.py +++ b/google/cloud/translate_v3beta1/services/translation_service/client.py @@ -349,10 +349,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def translate_text( @@ -1181,6 +1178,19 @@ def delete_glossary( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/translate_v3beta1/services/translation_service/transports/base.py b/google/cloud/translate_v3beta1/services/translation_service/transports/base.py index f79f5840..b52d6efb 100644 --- a/google/cloud/translate_v3beta1/services/translation_service/transports/base.py +++ b/google/cloud/translate_v3beta1/services/translation_service/transports/base.py @@ -241,6 +241,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/translate_v3beta1/services/translation_service/transports/grpc.py b/google/cloud/translate_v3beta1/services/translation_service/transports/grpc.py index c6373f87..7149d6c1 100644 --- a/google/cloud/translate_v3beta1/services/translation_service/transports/grpc.py +++ b/google/cloud/translate_v3beta1/services/translation_service/transports/grpc.py @@ -549,5 +549,8 @@ def delete_glossary( ) return self._stubs["delete_glossary"] + def close(self): + self.grpc_channel.close() + __all__ = ("TranslationServiceGrpcTransport",) diff --git a/google/cloud/translate_v3beta1/services/translation_service/transports/grpc_asyncio.py b/google/cloud/translate_v3beta1/services/translation_service/transports/grpc_asyncio.py index ce782d8e..dd50985a 100644 --- a/google/cloud/translate_v3beta1/services/translation_service/transports/grpc_asyncio.py +++ b/google/cloud/translate_v3beta1/services/translation_service/transports/grpc_asyncio.py @@ -557,5 +557,8 @@ def delete_glossary( ) return self._stubs["delete_glossary"] + def close(self): + return self.grpc_channel.close() + __all__ = ("TranslationServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/translate_v3beta1/types/translation_service.py b/google/cloud/translate_v3beta1/types/translation_service.py index f8e79577..4ef2cee5 100644 --- a/google/cloud/translate_v3beta1/types/translation_service.py +++ b/google/cloud/translate_v3beta1/types/translation_service.py @@ -81,6 +81,7 @@ class TranslateTextGlossaryConfig(proto.Message): class TranslateTextRequest(proto.Message): r"""The request message for synchronous translation. + Attributes: contents (Sequence[str]): Required. The content of the input in string @@ -170,6 +171,7 @@ class TranslateTextRequest(proto.Message): class TranslateTextResponse(proto.Message): r""" + Attributes: translations (Sequence[google.cloud.translate_v3beta1.types.Translation]): Text translation responses with no glossary applied. This @@ -191,6 +193,7 @@ class TranslateTextResponse(proto.Message): class Translation(proto.Message): r"""A single translation response. + Attributes: translated_text (str): Text translated into the target language. @@ -226,6 +229,7 @@ class Translation(proto.Message): class DetectLanguageRequest(proto.Message): r"""The request message for language detection. + Attributes: parent (str): Required. Project or location to make a call. Must refer to @@ -281,6 +285,7 @@ class DetectLanguageRequest(proto.Message): class DetectedLanguage(proto.Message): r"""The response message for language detection. + Attributes: language_code (str): The BCP-47 language code of source content in @@ -296,6 +301,7 @@ class DetectedLanguage(proto.Message): class DetectLanguageResponse(proto.Message): r"""The response message for language detection. + Attributes: languages (Sequence[google.cloud.translate_v3beta1.types.DetectedLanguage]): A list of detected languages sorted by @@ -310,6 +316,7 @@ class DetectLanguageResponse(proto.Message): class GetSupportedLanguagesRequest(proto.Message): r"""The request message for discovering supported languages. + Attributes: parent (str): Required. Project or location to make a call. Must refer to @@ -355,6 +362,7 @@ class GetSupportedLanguagesRequest(proto.Message): class SupportedLanguages(proto.Message): r"""The response message for discovering supported languages. + Attributes: languages (Sequence[google.cloud.translate_v3beta1.types.SupportedLanguage]): A list of supported language responses. This @@ -396,6 +404,7 @@ class SupportedLanguage(proto.Message): class GcsSource(proto.Message): r"""The Google Cloud Storage location for the input content. + Attributes: input_uri (str): Required. Source data URI. For example, @@ -407,6 +416,7 @@ class GcsSource(proto.Message): class InputConfig(proto.Message): r"""Input configuration for BatchTranslateText request. + Attributes: mime_type (str): Optional. Can be "text/plain" or "text/html". For ``.tsv``, @@ -443,6 +453,7 @@ class InputConfig(proto.Message): class GcsDestination(proto.Message): r"""The Google Cloud Storage location for the output content. + Attributes: output_uri_prefix (str): Required. There must be no files under 'output_uri_prefix'. @@ -456,6 +467,7 @@ class GcsDestination(proto.Message): class OutputConfig(proto.Message): r"""Output configuration for BatchTranslateText request. + Attributes: gcs_destination (google.cloud.translate_v3beta1.types.GcsDestination): Google Cloud Storage destination for output content. For @@ -543,6 +555,7 @@ class OutputConfig(proto.Message): class DocumentInputConfig(proto.Message): r"""A document translation request input config. + Attributes: content (bytes): Document's content represented as a stream of @@ -573,6 +586,7 @@ class DocumentInputConfig(proto.Message): class DocumentOutputConfig(proto.Message): r"""A document translation request output config. + Attributes: gcs_destination (google.cloud.translate_v3beta1.types.GcsDestination): Optional. Google Cloud Storage destination for the @@ -644,6 +658,7 @@ class DocumentOutputConfig(proto.Message): class TranslateDocumentRequest(proto.Message): r"""A document translation request. + Attributes: parent (str): Required. Location to make a regional call. @@ -734,6 +749,7 @@ class TranslateDocumentRequest(proto.Message): class DocumentTranslation(proto.Message): r"""A translated document message. + Attributes: byte_stream_outputs (Sequence[bytes]): The array of translated documents. It is @@ -759,6 +775,7 @@ class DocumentTranslation(proto.Message): class TranslateDocumentResponse(proto.Message): r"""A translated document response message. + Attributes: document_translation (google.cloud.translate_v3beta1.types.DocumentTranslation): Translated document. @@ -794,6 +811,7 @@ class TranslateDocumentResponse(proto.Message): class BatchTranslateTextRequest(proto.Message): r"""The batch translation request. + Attributes: parent (str): Required. Location to make a call. Must refer to a caller's @@ -870,6 +888,7 @@ class BatchTranslateTextRequest(proto.Message): class BatchTranslateMetadata(proto.Message): r"""State metadata for the batch translation operation. + Attributes: state (google.cloud.translate_v3beta1.types.BatchTranslateMetadata.State): The state of the operation. @@ -938,6 +957,7 @@ class BatchTranslateResponse(proto.Message): class GlossaryInputConfig(proto.Message): r"""Input configuration for glossaries. + Attributes: gcs_source (google.cloud.translate_v3beta1.types.GcsSource): Required. Google Cloud Storage location of glossary data. @@ -974,6 +994,7 @@ class GlossaryInputConfig(proto.Message): class Glossary(proto.Message): r"""Represents a glossary built from user provided data. + Attributes: name (str): Required. The resource name of the glossary. Glossary names @@ -999,6 +1020,7 @@ class Glossary(proto.Message): class LanguageCodePair(proto.Message): r"""Used with unidirectional glossaries. + Attributes: source_language_code (str): Required. The BCP-47 language code of the input text, for @@ -1015,6 +1037,7 @@ class LanguageCodePair(proto.Message): class LanguageCodesSet(proto.Message): r"""Used with equivalent term set glossaries. + Attributes: language_codes (Sequence[str]): The BCP-47 language code(s) for terms defined in the @@ -1040,6 +1063,7 @@ class LanguageCodesSet(proto.Message): class CreateGlossaryRequest(proto.Message): r"""Request message for CreateGlossary. + Attributes: parent (str): Required. The project name. @@ -1053,6 +1077,7 @@ class CreateGlossaryRequest(proto.Message): class GetGlossaryRequest(proto.Message): r"""Request message for GetGlossary. + Attributes: name (str): Required. The name of the glossary to @@ -1064,6 +1089,7 @@ class GetGlossaryRequest(proto.Message): class DeleteGlossaryRequest(proto.Message): r"""Request message for DeleteGlossary. + Attributes: name (str): Required. The name of the glossary to delete. @@ -1074,6 +1100,7 @@ class DeleteGlossaryRequest(proto.Message): class ListGlossariesRequest(proto.Message): r"""Request message for ListGlossaries. + Attributes: parent (str): Required. The name of the project from which @@ -1124,6 +1151,7 @@ class ListGlossariesRequest(proto.Message): class ListGlossariesResponse(proto.Message): r"""Response message for ListGlossaries. + Attributes: glossaries (Sequence[google.cloud.translate_v3beta1.types.Glossary]): The list of glossaries for a project. @@ -1232,6 +1260,7 @@ class DeleteGlossaryResponse(proto.Message): class BatchTranslateDocumentRequest(proto.Message): r"""The BatchTranslateDocument request. + Attributes: parent (str): Required. Location to make a regional call. @@ -1317,6 +1346,7 @@ class BatchTranslateDocumentRequest(proto.Message): class BatchDocumentInputConfig(proto.Message): r"""Input configuration for BatchTranslateDocument request. + Attributes: gcs_source (google.cloud.translate_v3beta1.types.GcsSource): Google Cloud Storage location for the source input. This can @@ -1348,6 +1378,7 @@ class BatchDocumentInputConfig(proto.Message): class BatchDocumentOutputConfig(proto.Message): r"""Output configuration for BatchTranslateDocument request. + Attributes: gcs_destination (google.cloud.translate_v3beta1.types.GcsDestination): Google Cloud Storage destination for output content. For @@ -1466,6 +1497,7 @@ class BatchTranslateDocumentResponse(proto.Message): class BatchTranslateDocumentMetadata(proto.Message): r"""State metadata for the batch translation operation. + Attributes: state (google.cloud.translate_v3beta1.types.BatchTranslateDocumentMetadata.State): The state of the operation. diff --git a/tests/unit/gapic/translate_v3/test_translation_service.py b/tests/unit/gapic/translate_v3/test_translation_service.py index 03d98cf5..9a733189 100644 --- a/tests/unit/gapic/translate_v3/test_translation_service.py +++ b/tests/unit/gapic/translate_v3/test_translation_service.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.translate_v3.services.translation_service import ( @@ -2799,6 +2800,9 @@ def test_translation_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -3335,3 +3339,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = TranslationServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = TranslationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = TranslationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/unit/gapic/translate_v3beta1/test_translation_service.py b/tests/unit/gapic/translate_v3beta1/test_translation_service.py index d7a486d8..eeed875e 100644 --- a/tests/unit/gapic/translate_v3beta1/test_translation_service.py +++ b/tests/unit/gapic/translate_v3beta1/test_translation_service.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.translate_v3beta1.services.translation_service import ( @@ -2691,6 +2692,9 @@ def test_translation_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -3227,3 +3231,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = TranslationServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = TranslationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = TranslationServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called()