From d72b90f495bdaa465148a3f40295e762a6dbb982 Mon Sep 17 00:00:00 2001 From: Christopher Wilcox Date: Wed, 28 Nov 2018 14:28:56 -0800 Subject: [PATCH] Run Black on Generated libraries (#6666) * blacken appveyor * Blacken all gen'd libraries not under PR --- google-cloud-speech/google/__init__.py | 2 + google-cloud-speech/google/cloud/__init__.py | 2 + google-cloud-speech/google/cloud/speech.py | 6 +- .../google/cloud/speech_v1/__init__.py | 6 +- .../google/cloud/speech_v1/gapic/enums.py | 2 + .../cloud/speech_v1/gapic/speech_client.py | 164 +++++++++--------- .../speech_v1/gapic/speech_client_config.py | 14 +- .../gapic/transports/speech_grpc_transport.py | 40 ++--- .../google/cloud/speech_v1/helpers.py | 13 +- .../google/cloud/speech_v1/types.py | 6 +- .../google/cloud/speech_v1p1beta1/__init__.py | 6 +- .../cloud/speech_v1p1beta1/gapic/enums.py | 6 + .../speech_v1p1beta1/gapic/speech_client.py | 164 +++++++++--------- .../gapic/speech_client_config.py | 14 +- .../gapic/transports/speech_grpc_transport.py | 40 ++--- .../google/cloud/speech_v1p1beta1/types.py | 6 +- .../system/gapic/v1/test_system_speech_v1.py | 12 +- .../v1p1beta1/test_system_speech_v1p1beta1.py | 12 +- .../unit/gapic/v1/test_speech_client_v1.py | 93 +++++----- .../v1p1beta1/test_speech_client_v1p1beta1.py | 93 +++++----- .../tests/unit/test_helpers.py | 12 +- 21 files changed, 354 insertions(+), 359 deletions(-) diff --git a/google-cloud-speech/google/__init__.py b/google-cloud-speech/google/__init__.py index 7a9e5a0ef198..dd3a9f485275 100644 --- a/google-cloud-speech/google/__init__.py +++ b/google-cloud-speech/google/__init__.py @@ -14,7 +14,9 @@ try: import pkg_resources + pkg_resources.declare_namespace(__name__) except ImportError: import pkgutil + __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google-cloud-speech/google/cloud/__init__.py b/google-cloud-speech/google/cloud/__init__.py index 7a9e5a0ef198..dd3a9f485275 100644 --- a/google-cloud-speech/google/cloud/__init__.py +++ b/google-cloud-speech/google/cloud/__init__.py @@ -14,7 +14,9 @@ try: import pkg_resources + pkg_resources.declare_namespace(__name__) except ImportError: import pkgutil + __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google-cloud-speech/google/cloud/speech.py b/google-cloud-speech/google/cloud/speech.py index fd511f736ef8..268ab600c713 100644 --- a/google-cloud-speech/google/cloud/speech.py +++ b/google-cloud-speech/google/cloud/speech.py @@ -20,8 +20,4 @@ from google.cloud.speech_v1 import enums from google.cloud.speech_v1 import types -__all__ = ( - 'enums', - 'types', - 'SpeechClient', -) +__all__ = ("enums", "types", "SpeechClient") diff --git a/google-cloud-speech/google/cloud/speech_v1/__init__.py b/google-cloud-speech/google/cloud/speech_v1/__init__.py index 272b623c510d..81314685022a 100644 --- a/google-cloud-speech/google/cloud/speech_v1/__init__.py +++ b/google-cloud-speech/google/cloud/speech_v1/__init__.py @@ -27,8 +27,4 @@ class SpeechClient(SpeechHelpers, speech_client.SpeechClient): types = types -__all__ = ( - 'enums', - 'SpeechClient', - 'types', -) +__all__ = ("enums", "SpeechClient", "types") diff --git a/google-cloud-speech/google/cloud/speech_v1/gapic/enums.py b/google-cloud-speech/google/cloud/speech_v1/gapic/enums.py index b15888d23394..42ed23f6b1f5 100644 --- a/google-cloud-speech/google/cloud/speech_v1/gapic/enums.py +++ b/google-cloud-speech/google/cloud/speech_v1/gapic/enums.py @@ -71,6 +71,7 @@ class AudioEncoding(enum.IntEnum): RTP header is replaced with a single byte containing the block length. Only Speex wideband is supported. ``sample_rate_hertz`` must be 16000. """ + ENCODING_UNSPECIFIED = 0 LINEAR16 = 1 FLAC = 2 @@ -97,5 +98,6 @@ class SpeechEventType(enum.IntEnum): sent if ``single_utterance`` was set to ``true``, and is not used otherwise. """ + SPEECH_EVENT_UNSPECIFIED = 0 END_OF_SINGLE_UTTERANCE = 1 diff --git a/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client.py b/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client.py index f68206d2c243..4a584e3920c7 100644 --- a/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client.py +++ b/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client.py @@ -35,19 +35,18 @@ from google.cloud.speech_v1.proto import cloud_speech_pb2_grpc from google.longrunning import operations_pb2 -_GAPIC_LIBRARY_VERSION = pkg_resources.get_distribution( - 'google-cloud-speech', ).version +_GAPIC_LIBRARY_VERSION = pkg_resources.get_distribution("google-cloud-speech").version class SpeechClient(object): """Service that implements Google Cloud Speech API.""" - SERVICE_ADDRESS = 'speech.googleapis.com:443' + SERVICE_ADDRESS = "speech.googleapis.com:443" """The default address of the service.""" # The name of the interface for this client. This is the key used to # find the method configuration in the client_config dictionary. - _INTERFACE_NAME = 'google.cloud.speech.v1.Speech' + _INTERFACE_NAME = "google.cloud.speech.v1.Speech" @classmethod def from_service_account_file(cls, filename, *args, **kwargs): @@ -63,19 +62,20 @@ def from_service_account_file(cls, filename, *args, **kwargs): Returns: SpeechClient: The constructed client. """ - credentials = service_account.Credentials.from_service_account_file( - filename) - kwargs['credentials'] = credentials + credentials = service_account.Credentials.from_service_account_file(filename) + kwargs["credentials"] = credentials return cls(*args, **kwargs) from_service_account_json = from_service_account_file - def __init__(self, - transport=None, - channel=None, - credentials=None, - client_config=None, - client_info=None): + def __init__( + self, + transport=None, + channel=None, + credentials=None, + client_config=None, + client_info=None, + ): """Constructor. Args: @@ -109,18 +109,19 @@ def __init__(self, # Raise deprecation warnings for things we want to go away. if client_config is not None: warnings.warn( - 'The `client_config` argument is deprecated.', + "The `client_config` argument is deprecated.", PendingDeprecationWarning, - stacklevel=2) + stacklevel=2, + ) else: client_config = speech_client_config.config if channel: warnings.warn( - 'The `channel` argument is deprecated; use ' - '`transport` instead.', + "The `channel` argument is deprecated; use " "`transport` instead.", PendingDeprecationWarning, - stacklevel=2) + stacklevel=2, + ) # Instantiate the transport. # The transport is responsible for handling serialization and @@ -134,19 +135,19 @@ def __init__(self, else: if credentials: raise ValueError( - 'Received both a transport instance and ' - 'credentials; these are mutually exclusive.') + "Received both a transport instance and " + "credentials; these are mutually exclusive." + ) self.transport = transport else: self.transport = speech_grpc_transport.SpeechGrpcTransport( - address=self.SERVICE_ADDRESS, - channel=channel, - credentials=credentials, + address=self.SERVICE_ADDRESS, channel=channel, credentials=credentials ) if client_info is None: client_info = google.api_core.gapic_v1.client_info.ClientInfo( - gapic_version=_GAPIC_LIBRARY_VERSION, ) + gapic_version=_GAPIC_LIBRARY_VERSION + ) else: client_info.gapic_version = _GAPIC_LIBRARY_VERSION self._client_info = client_info @@ -156,7 +157,8 @@ def __init__(self, # (Ordinarily, these are the defaults specified in the `*_config.py` # file next to this one.) self._method_configs = google.api_core.gapic_v1.config.parse_method_configs( - client_config['interfaces'][self._INTERFACE_NAME], ) + client_config["interfaces"][self._INTERFACE_NAME] + ) # Save a dictionary of cached API call functions. # These are the actual callables which invoke the proper @@ -165,12 +167,14 @@ def __init__(self, self._inner_api_calls = {} # Service calls - def recognize(self, - config, - audio, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + def recognize( + self, + config, + audio, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs synchronous speech recognition: receive results after all audio has been sent and processed. @@ -220,28 +224,29 @@ def recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'recognize' not in self._inner_api_calls: + if "recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.recognize, - default_retry=self._method_configs['Recognize'].retry, - default_timeout=self._method_configs['Recognize'].timeout, - client_info=self._client_info, - ) + "recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.recognize, + default_retry=self._method_configs["Recognize"].retry, + default_timeout=self._method_configs["Recognize"].timeout, + client_info=self._client_info, + ) - request = cloud_speech_pb2.RecognizeRequest( - config=config, - audio=audio, + request = cloud_speech_pb2.RecognizeRequest(config=config, audio=audio) + return self._inner_api_calls["recognize"]( + request, retry=retry, timeout=timeout, metadata=metadata ) - return self._inner_api_calls['recognize']( - request, retry=retry, timeout=timeout, metadata=metadata) - - def long_running_recognize(self, - config, - audio, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + + def long_running_recognize( + self, + config, + audio, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an @@ -302,23 +307,22 @@ def long_running_recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'long_running_recognize' not in self._inner_api_calls: + if "long_running_recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'long_running_recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.long_running_recognize, - default_retry=self._method_configs['LongRunningRecognize']. - retry, - default_timeout=self. - _method_configs['LongRunningRecognize'].timeout, - client_info=self._client_info, - ) + "long_running_recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.long_running_recognize, + default_retry=self._method_configs["LongRunningRecognize"].retry, + default_timeout=self._method_configs["LongRunningRecognize"].timeout, + client_info=self._client_info, + ) request = cloud_speech_pb2.LongRunningRecognizeRequest( - config=config, - audio=audio, + config=config, audio=audio + ) + operation = self._inner_api_calls["long_running_recognize"]( + request, retry=retry, timeout=timeout, metadata=metadata ) - operation = self._inner_api_calls['long_running_recognize']( - request, retry=retry, timeout=timeout, metadata=metadata) return google.api_core.operation.from_gapic( operation, self.transport._operations_client, @@ -326,11 +330,13 @@ def long_running_recognize(self, metadata_type=cloud_speech_pb2.LongRunningRecognizeMetadata, ) - def streaming_recognize(self, - requests, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + def streaming_recognize( + self, + requests, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST). @@ -372,16 +378,16 @@ def streaming_recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'streaming_recognize' not in self._inner_api_calls: + if "streaming_recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'streaming_recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.streaming_recognize, - default_retry=self._method_configs['StreamingRecognize']. - retry, - default_timeout=self._method_configs['StreamingRecognize']. - timeout, - client_info=self._client_info, - ) + "streaming_recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.streaming_recognize, + default_retry=self._method_configs["StreamingRecognize"].retry, + default_timeout=self._method_configs["StreamingRecognize"].timeout, + client_info=self._client_info, + ) - return self._inner_api_calls['streaming_recognize']( - requests, retry=retry, timeout=timeout, metadata=metadata) + return self._inner_api_calls["streaming_recognize"]( + requests, retry=retry, timeout=timeout, metadata=metadata + ) diff --git a/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client_config.py b/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client_config.py index d116c8d0bb78..b54aed26bed8 100644 --- a/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client_config.py +++ b/google-cloud-speech/google/cloud/speech_v1/gapic/speech_client_config.py @@ -3,7 +3,7 @@ "google.cloud.speech.v1.Speech": { "retry_codes": { "idempotent": ["DEADLINE_EXCEEDED", "UNAVAILABLE"], - "non_idempotent": [] + "non_idempotent": [], }, "retry_params": { "default": { @@ -13,26 +13,26 @@ "initial_rpc_timeout_millis": 1000000, "rpc_timeout_multiplier": 1.0, "max_rpc_timeout_millis": 1000000, - "total_timeout_millis": 5000000 + "total_timeout_millis": 5000000, } }, "methods": { "Recognize": { "timeout_millis": 200000, "retry_codes_name": "idempotent", - "retry_params_name": "default" + "retry_params_name": "default", }, "LongRunningRecognize": { "timeout_millis": 200000, "retry_codes_name": "non_idempotent", - "retry_params_name": "default" + "retry_params_name": "default", }, "StreamingRecognize": { "timeout_millis": 200000, "retry_codes_name": "idempotent", - "retry_params_name": "default" - } - } + "retry_params_name": "default", + }, + }, } } } diff --git a/google-cloud-speech/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py b/google-cloud-speech/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py index 53bccbf3fd1d..97d5d0e31d42 100644 --- a/google-cloud-speech/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py +++ b/google-cloud-speech/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py @@ -28,14 +28,14 @@ class SpeechGrpcTransport(object): which can be used to take advantage of advanced features of gRPC. """ + # The scopes needed to make gRPC calls to all of the methods defined # in this service. - _OAUTH_SCOPES = ('https://www.googleapis.com/auth/cloud-platform', ) + _OAUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",) - def __init__(self, - channel=None, - credentials=None, - address='speech.googleapis.com:443'): + def __init__( + self, channel=None, credentials=None, address="speech.googleapis.com:443" + ): """Instantiate the transport class. Args: @@ -53,34 +53,28 @@ def __init__(self, # exception (channels come with credentials baked in already). if channel is not None and credentials is not None: raise ValueError( - 'The `channel` and `credentials` arguments are mutually ' - 'exclusive.', ) + "The `channel` and `credentials` arguments are mutually " "exclusive." + ) # Create the channel. if channel is None: - channel = self.create_channel( - address=address, - credentials=credentials, - ) + channel = self.create_channel(address=address, credentials=credentials) self._channel = channel # gRPC uses objects called "stubs" that are bound to the # channel and provide a basic method for each RPC. - self._stubs = { - 'speech_stub': cloud_speech_pb2_grpc.SpeechStub(channel), - } + self._stubs = {"speech_stub": cloud_speech_pb2_grpc.SpeechStub(channel)} # Because this API includes a method that returns a # long-running operation (proto: google.longrunning.Operation), # instantiate an LRO client. self._operations_client = google.api_core.operations_v1.OperationsClient( - channel) + channel + ) @classmethod - def create_channel(cls, - address='speech.googleapis.com:443', - credentials=None): + def create_channel(cls, address="speech.googleapis.com:443", credentials=None): """Create and return a gRPC channel object. Args: @@ -95,9 +89,7 @@ def create_channel(cls, grpc.Channel: A gRPC channel object. """ return google.api_core.grpc_helpers.create_channel( - address, - credentials=credentials, - scopes=cls._OAUTH_SCOPES, + address, credentials=credentials, scopes=cls._OAUTH_SCOPES ) @property @@ -121,7 +113,7 @@ def recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].Recognize + return self._stubs["speech_stub"].Recognize @property def long_running_recognize(self): @@ -137,7 +129,7 @@ def long_running_recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].LongRunningRecognize + return self._stubs["speech_stub"].LongRunningRecognize @property def streaming_recognize(self): @@ -151,4 +143,4 @@ def streaming_recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].StreamingRecognize + return self._stubs["speech_stub"].StreamingRecognize diff --git a/google-cloud-speech/google/cloud/speech_v1/helpers.py b/google-cloud-speech/google/cloud/speech_v1/helpers.py index cee59ba052c5..26f17dbe52d3 100644 --- a/google-cloud-speech/google/cloud/speech_v1/helpers.py +++ b/google-cloud-speech/google/cloud/speech_v1/helpers.py @@ -24,10 +24,14 @@ class SpeechHelpers(object): in a multiple-inheritance construction alongside the applicable GAPIC. See the :class:`~google.cloud.speech_v1.SpeechClient`. """ + def streaming_recognize( - self, config, requests, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT): + self, + config, + requests, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + ): """Perform bi-directional speech recognition. This method allows you to receive results while sending audio; @@ -75,7 +79,8 @@ def streaming_recognize( """ return super(SpeechHelpers, self).streaming_recognize( self._streaming_request_iterable(config, requests), - retry=retry, timeout=timeout + retry=retry, + timeout=timeout, ) def _streaming_request_iterable(self, config, requests): diff --git a/google-cloud-speech/google/cloud/speech_v1/types.py b/google-cloud-speech/google/cloud/speech_v1/types.py index c6ea1c9bc04e..eb1831e87294 100644 --- a/google-cloud-speech/google/cloud/speech_v1/types.py +++ b/google-cloud-speech/google/cloud/speech_v1/types.py @@ -40,9 +40,7 @@ status_pb2, ] -_local_modules = [ - cloud_speech_pb2, -] +_local_modules = [cloud_speech_pb2] names = [] @@ -52,7 +50,7 @@ names.append(name) for module in _local_modules: for name, message in get_messages(module).items(): - message.__module__ = 'google.cloud.speech_v1.types' + message.__module__ = "google.cloud.speech_v1.types" setattr(sys.modules[__name__], name, message) names.append(name) diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/__init__.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/__init__.py index feed0a0cf56b..b68a395372f9 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/__init__.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/__init__.py @@ -27,8 +27,4 @@ class SpeechClient(SpeechHelpers, speech_client.SpeechClient): types = types -__all__ = ( - 'enums', - 'types', - 'SpeechClient', -) +__all__ = ("enums", "types", "SpeechClient") diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/enums.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/enums.py index ab530c1cc862..49fd29a64842 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/enums.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/enums.py @@ -71,6 +71,7 @@ class AudioEncoding(enum.IntEnum): RTP header is replaced with a single byte containing the block length. Only Speex wideband is supported. ``sample_rate_hertz`` must be 16000. """ + ENCODING_UNSPECIFIED = 0 LINEAR16 = 1 FLAC = 2 @@ -105,6 +106,7 @@ class InteractionType(enum.IntEnum): DICTATION (int): Transcribe speech to text to create a written document, such as a text-message, email or report. """ + INTERACTION_TYPE_UNSPECIFIED = 0 DISCUSSION = 1 PRESENTATION = 2 @@ -127,6 +129,7 @@ class MicrophoneDistance(enum.IntEnum): MIDFIELD (int): The speaker if within 3 meters of the microphone. FARFIELD (int): The speaker is more than 3 meters away from the microphone. """ + MICROPHONE_DISTANCE_UNSPECIFIED = 0 NEARFIELD = 1 MIDFIELD = 2 @@ -141,6 +144,7 @@ class OriginalMediaType(enum.IntEnum): AUDIO (int): The speech data is an audio recording. VIDEO (int): The speech data originally recorded on a video. """ + ORIGINAL_MEDIA_TYPE_UNSPECIFIED = 0 AUDIO = 1 VIDEO = 2 @@ -158,6 +162,7 @@ class RecordingDeviceType(enum.IntEnum): OTHER_OUTDOOR_DEVICE (int): Speech was recorded outdoors. OTHER_INDOOR_DEVICE (int): Speech was recorded indoors. """ + RECORDING_DEVICE_TYPE_UNSPECIFIED = 0 SMARTPHONE = 1 PC = 2 @@ -183,5 +188,6 @@ class SpeechEventType(enum.IntEnum): sent if ``single_utterance`` was set to ``true``, and is not used otherwise. """ + SPEECH_EVENT_UNSPECIFIED = 0 END_OF_SINGLE_UTTERANCE = 1 diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client.py index 037bcd40b9ac..987b98930a28 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client.py @@ -35,19 +35,18 @@ from google.cloud.speech_v1p1beta1.proto import cloud_speech_pb2_grpc from google.longrunning import operations_pb2 -_GAPIC_LIBRARY_VERSION = pkg_resources.get_distribution( - 'google-cloud-speech', ).version +_GAPIC_LIBRARY_VERSION = pkg_resources.get_distribution("google-cloud-speech").version class SpeechClient(object): """Service that implements Google Cloud Speech API.""" - SERVICE_ADDRESS = 'speech.googleapis.com:443' + SERVICE_ADDRESS = "speech.googleapis.com:443" """The default address of the service.""" # The name of the interface for this client. This is the key used to # find the method configuration in the client_config dictionary. - _INTERFACE_NAME = 'google.cloud.speech.v1p1beta1.Speech' + _INTERFACE_NAME = "google.cloud.speech.v1p1beta1.Speech" @classmethod def from_service_account_file(cls, filename, *args, **kwargs): @@ -63,19 +62,20 @@ def from_service_account_file(cls, filename, *args, **kwargs): Returns: SpeechClient: The constructed client. """ - credentials = service_account.Credentials.from_service_account_file( - filename) - kwargs['credentials'] = credentials + credentials = service_account.Credentials.from_service_account_file(filename) + kwargs["credentials"] = credentials return cls(*args, **kwargs) from_service_account_json = from_service_account_file - def __init__(self, - transport=None, - channel=None, - credentials=None, - client_config=None, - client_info=None): + def __init__( + self, + transport=None, + channel=None, + credentials=None, + client_config=None, + client_info=None, + ): """Constructor. Args: @@ -109,18 +109,19 @@ def __init__(self, # Raise deprecation warnings for things we want to go away. if client_config is not None: warnings.warn( - 'The `client_config` argument is deprecated.', + "The `client_config` argument is deprecated.", PendingDeprecationWarning, - stacklevel=2) + stacklevel=2, + ) else: client_config = speech_client_config.config if channel: warnings.warn( - 'The `channel` argument is deprecated; use ' - '`transport` instead.', + "The `channel` argument is deprecated; use " "`transport` instead.", PendingDeprecationWarning, - stacklevel=2) + stacklevel=2, + ) # Instantiate the transport. # The transport is responsible for handling serialization and @@ -134,19 +135,19 @@ def __init__(self, else: if credentials: raise ValueError( - 'Received both a transport instance and ' - 'credentials; these are mutually exclusive.') + "Received both a transport instance and " + "credentials; these are mutually exclusive." + ) self.transport = transport else: self.transport = speech_grpc_transport.SpeechGrpcTransport( - address=self.SERVICE_ADDRESS, - channel=channel, - credentials=credentials, + address=self.SERVICE_ADDRESS, channel=channel, credentials=credentials ) if client_info is None: client_info = google.api_core.gapic_v1.client_info.ClientInfo( - gapic_version=_GAPIC_LIBRARY_VERSION, ) + gapic_version=_GAPIC_LIBRARY_VERSION + ) else: client_info.gapic_version = _GAPIC_LIBRARY_VERSION self._client_info = client_info @@ -156,7 +157,8 @@ def __init__(self, # (Ordinarily, these are the defaults specified in the `*_config.py` # file next to this one.) self._method_configs = google.api_core.gapic_v1.config.parse_method_configs( - client_config['interfaces'][self._INTERFACE_NAME], ) + client_config["interfaces"][self._INTERFACE_NAME] + ) # Save a dictionary of cached API call functions. # These are the actual callables which invoke the proper @@ -165,12 +167,14 @@ def __init__(self, self._inner_api_calls = {} # Service calls - def recognize(self, - config, - audio, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + def recognize( + self, + config, + audio, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs synchronous speech recognition: receive results after all audio has been sent and processed. @@ -220,28 +224,29 @@ def recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'recognize' not in self._inner_api_calls: + if "recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.recognize, - default_retry=self._method_configs['Recognize'].retry, - default_timeout=self._method_configs['Recognize'].timeout, - client_info=self._client_info, - ) + "recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.recognize, + default_retry=self._method_configs["Recognize"].retry, + default_timeout=self._method_configs["Recognize"].timeout, + client_info=self._client_info, + ) - request = cloud_speech_pb2.RecognizeRequest( - config=config, - audio=audio, + request = cloud_speech_pb2.RecognizeRequest(config=config, audio=audio) + return self._inner_api_calls["recognize"]( + request, retry=retry, timeout=timeout, metadata=metadata ) - return self._inner_api_calls['recognize']( - request, retry=retry, timeout=timeout, metadata=metadata) - - def long_running_recognize(self, - config, - audio, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + + def long_running_recognize( + self, + config, + audio, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an @@ -302,23 +307,22 @@ def long_running_recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'long_running_recognize' not in self._inner_api_calls: + if "long_running_recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'long_running_recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.long_running_recognize, - default_retry=self._method_configs['LongRunningRecognize']. - retry, - default_timeout=self. - _method_configs['LongRunningRecognize'].timeout, - client_info=self._client_info, - ) + "long_running_recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.long_running_recognize, + default_retry=self._method_configs["LongRunningRecognize"].retry, + default_timeout=self._method_configs["LongRunningRecognize"].timeout, + client_info=self._client_info, + ) request = cloud_speech_pb2.LongRunningRecognizeRequest( - config=config, - audio=audio, + config=config, audio=audio + ) + operation = self._inner_api_calls["long_running_recognize"]( + request, retry=retry, timeout=timeout, metadata=metadata ) - operation = self._inner_api_calls['long_running_recognize']( - request, retry=retry, timeout=timeout, metadata=metadata) return google.api_core.operation.from_gapic( operation, self.transport._operations_client, @@ -326,11 +330,13 @@ def long_running_recognize(self, metadata_type=cloud_speech_pb2.LongRunningRecognizeMetadata, ) - def streaming_recognize(self, - requests, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None): + def streaming_recognize( + self, + requests, + retry=google.api_core.gapic_v1.method.DEFAULT, + timeout=google.api_core.gapic_v1.method.DEFAULT, + metadata=None, + ): """ Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST). @@ -372,16 +378,16 @@ def streaming_recognize(self, ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. - if 'streaming_recognize' not in self._inner_api_calls: + if "streaming_recognize" not in self._inner_api_calls: self._inner_api_calls[ - 'streaming_recognize'] = google.api_core.gapic_v1.method.wrap_method( - self.transport.streaming_recognize, - default_retry=self._method_configs['StreamingRecognize']. - retry, - default_timeout=self._method_configs['StreamingRecognize']. - timeout, - client_info=self._client_info, - ) + "streaming_recognize" + ] = google.api_core.gapic_v1.method.wrap_method( + self.transport.streaming_recognize, + default_retry=self._method_configs["StreamingRecognize"].retry, + default_timeout=self._method_configs["StreamingRecognize"].timeout, + client_info=self._client_info, + ) - return self._inner_api_calls['streaming_recognize']( - requests, retry=retry, timeout=timeout, metadata=metadata) + return self._inner_api_calls["streaming_recognize"]( + requests, retry=retry, timeout=timeout, metadata=metadata + ) diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client_config.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client_config.py index fd93fde49f27..8360e0273019 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client_config.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/speech_client_config.py @@ -3,7 +3,7 @@ "google.cloud.speech.v1p1beta1.Speech": { "retry_codes": { "idempotent": ["DEADLINE_EXCEEDED", "UNAVAILABLE"], - "non_idempotent": [] + "non_idempotent": [], }, "retry_params": { "default": { @@ -13,26 +13,26 @@ "initial_rpc_timeout_millis": 1000000, "rpc_timeout_multiplier": 1.0, "max_rpc_timeout_millis": 1000000, - "total_timeout_millis": 5000000 + "total_timeout_millis": 5000000, } }, "methods": { "Recognize": { "timeout_millis": 200000, "retry_codes_name": "idempotent", - "retry_params_name": "default" + "retry_params_name": "default", }, "LongRunningRecognize": { "timeout_millis": 200000, "retry_codes_name": "non_idempotent", - "retry_params_name": "default" + "retry_params_name": "default", }, "StreamingRecognize": { "timeout_millis": 905000, "retry_codes_name": "idempotent", - "retry_params_name": "default" - } - } + "retry_params_name": "default", + }, + }, } } } diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/transports/speech_grpc_transport.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/transports/speech_grpc_transport.py index 8c9496d5f84b..2c51663f0ca1 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/transports/speech_grpc_transport.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/gapic/transports/speech_grpc_transport.py @@ -28,14 +28,14 @@ class SpeechGrpcTransport(object): which can be used to take advantage of advanced features of gRPC. """ + # The scopes needed to make gRPC calls to all of the methods defined # in this service. - _OAUTH_SCOPES = ('https://www.googleapis.com/auth/cloud-platform', ) + _OAUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",) - def __init__(self, - channel=None, - credentials=None, - address='speech.googleapis.com:443'): + def __init__( + self, channel=None, credentials=None, address="speech.googleapis.com:443" + ): """Instantiate the transport class. Args: @@ -53,34 +53,28 @@ def __init__(self, # exception (channels come with credentials baked in already). if channel is not None and credentials is not None: raise ValueError( - 'The `channel` and `credentials` arguments are mutually ' - 'exclusive.', ) + "The `channel` and `credentials` arguments are mutually " "exclusive." + ) # Create the channel. if channel is None: - channel = self.create_channel( - address=address, - credentials=credentials, - ) + channel = self.create_channel(address=address, credentials=credentials) self._channel = channel # gRPC uses objects called "stubs" that are bound to the # channel and provide a basic method for each RPC. - self._stubs = { - 'speech_stub': cloud_speech_pb2_grpc.SpeechStub(channel), - } + self._stubs = {"speech_stub": cloud_speech_pb2_grpc.SpeechStub(channel)} # Because this API includes a method that returns a # long-running operation (proto: google.longrunning.Operation), # instantiate an LRO client. self._operations_client = google.api_core.operations_v1.OperationsClient( - channel) + channel + ) @classmethod - def create_channel(cls, - address='speech.googleapis.com:443', - credentials=None): + def create_channel(cls, address="speech.googleapis.com:443", credentials=None): """Create and return a gRPC channel object. Args: @@ -95,9 +89,7 @@ def create_channel(cls, grpc.Channel: A gRPC channel object. """ return google.api_core.grpc_helpers.create_channel( - address, - credentials=credentials, - scopes=cls._OAUTH_SCOPES, + address, credentials=credentials, scopes=cls._OAUTH_SCOPES ) @property @@ -121,7 +113,7 @@ def recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].Recognize + return self._stubs["speech_stub"].Recognize @property def long_running_recognize(self): @@ -137,7 +129,7 @@ def long_running_recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].LongRunningRecognize + return self._stubs["speech_stub"].LongRunningRecognize @property def streaming_recognize(self): @@ -151,4 +143,4 @@ def streaming_recognize(self): deserialized request object and returns a deserialized response object. """ - return self._stubs['speech_stub'].StreamingRecognize + return self._stubs["speech_stub"].StreamingRecognize diff --git a/google-cloud-speech/google/cloud/speech_v1p1beta1/types.py b/google-cloud-speech/google/cloud/speech_v1p1beta1/types.py index 083452329a34..07a7faafe162 100644 --- a/google-cloud-speech/google/cloud/speech_v1p1beta1/types.py +++ b/google-cloud-speech/google/cloud/speech_v1p1beta1/types.py @@ -40,9 +40,7 @@ status_pb2, ] -_local_modules = [ - cloud_speech_pb2, -] +_local_modules = [cloud_speech_pb2] names = [] @@ -52,7 +50,7 @@ names.append(name) for module in _local_modules: for name, message in get_messages(module).items(): - message.__module__ = 'google.cloud.speech_v1p1beta1.types' + message.__module__ = "google.cloud.speech_v1p1beta1.types" setattr(sys.modules[__name__], name, message) names.append(name) diff --git a/google-cloud-speech/tests/system/gapic/v1/test_system_speech_v1.py b/google-cloud-speech/tests/system/gapic/v1/test_system_speech_v1.py index 36728e6cb1ad..080e431ad81d 100644 --- a/google-cloud-speech/tests/system/gapic/v1/test_system_speech_v1.py +++ b/google-cloud-speech/tests/system/gapic/v1/test_system_speech_v1.py @@ -23,14 +23,14 @@ class TestSystemSpeech(object): def test_recognize(self): client = speech_v1.SpeechClient() - language_code = 'en-US' + language_code = "en-US" sample_rate_hertz = 44100 encoding = enums.RecognitionConfig.AudioEncoding.FLAC config = { - 'language_code': language_code, - 'sample_rate_hertz': sample_rate_hertz, - 'encoding': encoding + "language_code": language_code, + "sample_rate_hertz": sample_rate_hertz, + "encoding": encoding, } - uri = 'gs://gapic-toolkit/hello.flac' - audio = {'uri': uri} + uri = "gs://gapic-toolkit/hello.flac" + audio = {"uri": uri} response = client.recognize(config, audio) diff --git a/google-cloud-speech/tests/system/gapic/v1p1beta1/test_system_speech_v1p1beta1.py b/google-cloud-speech/tests/system/gapic/v1p1beta1/test_system_speech_v1p1beta1.py index fb2b47b7fce7..36636c3e2479 100644 --- a/google-cloud-speech/tests/system/gapic/v1p1beta1/test_system_speech_v1p1beta1.py +++ b/google-cloud-speech/tests/system/gapic/v1p1beta1/test_system_speech_v1p1beta1.py @@ -23,14 +23,14 @@ class TestSystemSpeech(object): def test_recognize(self): client = speech_v1p1beta1.SpeechClient() - language_code = 'en-US' + language_code = "en-US" sample_rate_hertz = 44100 encoding = enums.RecognitionConfig.AudioEncoding.FLAC config = { - 'language_code': language_code, - 'sample_rate_hertz': sample_rate_hertz, - 'encoding': encoding + "language_code": language_code, + "sample_rate_hertz": sample_rate_hertz, + "encoding": encoding, } - uri = 'gs://gapic-toolkit/hello.flac' - audio = {'uri': uri} + uri = "gs://gapic-toolkit/hello.flac" + audio = {"uri": uri} response = client.recognize(config, audio) diff --git a/google-cloud-speech/tests/unit/gapic/v1/test_speech_client_v1.py b/google-cloud-speech/tests/unit/gapic/v1/test_speech_client_v1.py index fa4be8099c32..0733e21827fc 100644 --- a/google-cloud-speech/tests/unit/gapic/v1/test_speech_client_v1.py +++ b/google-cloud-speech/tests/unit/gapic/v1/test_speech_client_v1.py @@ -54,16 +54,12 @@ def __init__(self, responses=[]): self.responses = responses self.requests = [] - def unary_unary(self, - method, - request_serializer=None, - response_deserializer=None): + def unary_unary(self, method, request_serializer=None, response_deserializer=None): return MultiCallableStub(method, self) - def stream_stream(self, - method, - request_serializer=None, - response_deserializer=None): + def stream_stream( + self, method, request_serializer=None, response_deserializer=None + ): return MultiCallableStub(method, self) @@ -75,12 +71,11 @@ class TestSpeechClient(object): def test_recognize(self): # Setup Expected Response expected_response = {} - expected_response = cloud_speech_pb2.RecognizeResponse( - **expected_response) + expected_response = cloud_speech_pb2.RecognizeResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() @@ -88,28 +83,27 @@ def test_recognize(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.recognize(config, audio) assert expected_response == response assert len(channel.requests) == 1 - expected_request = cloud_speech_pb2.RecognizeRequest( - config=config, audio=audio) + expected_request = cloud_speech_pb2.RecognizeRequest(config=config, audio=audio) actual_request = channel.requests[0][1] assert expected_request == actual_request def test_recognize_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() @@ -117,14 +111,14 @@ def test_recognize_exception(self): # Setup request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} with pytest.raises(CustomException): client.recognize(config, audio) @@ -133,14 +127,16 @@ def test_long_running_recognize(self): # Setup Expected Response expected_response = {} expected_response = cloud_speech_pb2.LongRunningRecognizeResponse( - **expected_response) + **expected_response + ) operation = operations_pb2.Operation( - name='operations/test_long_running_recognize', done=True) + name="operations/test_long_running_recognize", done=True + ) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() @@ -148,14 +144,14 @@ def test_long_running_recognize(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.long_running_recognize(config, audio) result = response.result() @@ -163,7 +159,8 @@ def test_long_running_recognize(self): assert len(channel.requests) == 1 expected_request = cloud_speech_pb2.LongRunningRecognizeRequest( - config=config, audio=audio) + config=config, audio=audio + ) actual_request = channel.requests[0][1] assert expected_request == actual_request @@ -171,12 +168,13 @@ def test_long_running_recognize_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( - name='operations/test_long_running_recognize_exception', done=True) + name="operations/test_long_running_recognize_exception", done=True + ) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() @@ -184,14 +182,14 @@ def test_long_running_recognize_exception(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.long_running_recognize(config, audio) exception = response.exception() @@ -201,11 +199,12 @@ def test_streaming_recognize(self): # Setup Expected Response expected_response = {} expected_response = cloud_speech_pb2.StreamingRecognizeResponse( - **expected_response) + **expected_response + ) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() @@ -229,7 +228,7 @@ def test_streaming_recognize(self): def test_streaming_recognize_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1.SpeechClient() diff --git a/google-cloud-speech/tests/unit/gapic/v1p1beta1/test_speech_client_v1p1beta1.py b/google-cloud-speech/tests/unit/gapic/v1p1beta1/test_speech_client_v1p1beta1.py index f27c47874f00..fc5e1c2de451 100644 --- a/google-cloud-speech/tests/unit/gapic/v1p1beta1/test_speech_client_v1p1beta1.py +++ b/google-cloud-speech/tests/unit/gapic/v1p1beta1/test_speech_client_v1p1beta1.py @@ -54,16 +54,12 @@ def __init__(self, responses=[]): self.responses = responses self.requests = [] - def unary_unary(self, - method, - request_serializer=None, - response_deserializer=None): + def unary_unary(self, method, request_serializer=None, response_deserializer=None): return MultiCallableStub(method, self) - def stream_stream(self, - method, - request_serializer=None, - response_deserializer=None): + def stream_stream( + self, method, request_serializer=None, response_deserializer=None + ): return MultiCallableStub(method, self) @@ -75,12 +71,11 @@ class TestSpeechClient(object): def test_recognize(self): # Setup Expected Response expected_response = {} - expected_response = cloud_speech_pb2.RecognizeResponse( - **expected_response) + expected_response = cloud_speech_pb2.RecognizeResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() @@ -88,28 +83,27 @@ def test_recognize(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.recognize(config, audio) assert expected_response == response assert len(channel.requests) == 1 - expected_request = cloud_speech_pb2.RecognizeRequest( - config=config, audio=audio) + expected_request = cloud_speech_pb2.RecognizeRequest(config=config, audio=audio) actual_request = channel.requests[0][1] assert expected_request == actual_request def test_recognize_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() @@ -117,14 +111,14 @@ def test_recognize_exception(self): # Setup request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} with pytest.raises(CustomException): client.recognize(config, audio) @@ -133,14 +127,16 @@ def test_long_running_recognize(self): # Setup Expected Response expected_response = {} expected_response = cloud_speech_pb2.LongRunningRecognizeResponse( - **expected_response) + **expected_response + ) operation = operations_pb2.Operation( - name='operations/test_long_running_recognize', done=True) + name="operations/test_long_running_recognize", done=True + ) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() @@ -148,14 +144,14 @@ def test_long_running_recognize(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.long_running_recognize(config, audio) result = response.result() @@ -163,7 +159,8 @@ def test_long_running_recognize(self): assert len(channel.requests) == 1 expected_request = cloud_speech_pb2.LongRunningRecognizeRequest( - config=config, audio=audio) + config=config, audio=audio + ) actual_request = channel.requests[0][1] assert expected_request == actual_request @@ -171,12 +168,13 @@ def test_long_running_recognize_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( - name='operations/test_long_running_recognize_exception', done=True) + name="operations/test_long_running_recognize_exception", done=True + ) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() @@ -184,14 +182,14 @@ def test_long_running_recognize_exception(self): # Setup Request encoding = enums.RecognitionConfig.AudioEncoding.FLAC sample_rate_hertz = 44100 - language_code = 'en-US' + language_code = "en-US" config = { - 'encoding': encoding, - 'sample_rate_hertz': sample_rate_hertz, - 'language_code': language_code + "encoding": encoding, + "sample_rate_hertz": sample_rate_hertz, + "language_code": language_code, } - uri = 'gs://bucket_name/file_name.flac' - audio = {'uri': uri} + uri = "gs://bucket_name/file_name.flac" + audio = {"uri": uri} response = client.long_running_recognize(config, audio) exception = response.exception() @@ -201,11 +199,12 @@ def test_streaming_recognize(self): # Setup Expected Response expected_response = {} expected_response = cloud_speech_pb2.StreamingRecognizeResponse( - **expected_response) + **expected_response + ) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() @@ -229,7 +228,7 @@ def test_streaming_recognize(self): def test_streaming_recognize_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - patch = mock.patch('google.api_core.grpc_helpers.create_channel') + patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = speech_v1p1beta1.SpeechClient() diff --git a/google-cloud-speech/tests/unit/test_helpers.py b/google-cloud-speech/tests/unit/test_helpers.py index a8199ec8ce5f..366b1821dd4c 100644 --- a/google-cloud-speech/tests/unit/test_helpers.py +++ b/google-cloud-speech/tests/unit/test_helpers.py @@ -31,11 +31,11 @@ def test_streaming_recognize(): client = make_speech_client() config = types.StreamingRecognitionConfig() - requests = [types.StreamingRecognizeRequest(audio_content=b'...')] + requests = [types.StreamingRecognizeRequest(audio_content=b"...")] super_patch = mock.patch( - 'google.cloud.speech_v1.speech_client.SpeechClient.' - 'streaming_recognize', - autospec=True) + "google.cloud.speech_v1.speech_client.SpeechClient." "streaming_recognize", + autospec=True, + ) with super_patch as streaming_recognize: client.streaming_recognize(config, requests) @@ -49,5 +49,5 @@ def test_streaming_recognize(): types.StreamingRecognizeRequest(streaming_config=config), requests[0], ] - assert 'retry' in kwargs - assert 'timeout' in kwargs + assert "retry" in kwargs + assert "timeout" in kwargs