From 5f28e210306de47ff5a9fa3b4bf48ac6ceb13e85 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Wed, 2 Oct 2024 13:50:33 +1000 Subject: [PATCH 1/6] test: Switch to unittest.mock from mock Now that the minimum supported version of Python is 3.7, we can stop using the external mock requirement, and import it from unittest. I have also attempted to keep imports ordered. Fixes #377 --- noxfile.py | 2 -- tests/asyncio/future/test_async_future.py | 2 +- tests/asyncio/gapic/test_method_async.py | 2 +- tests/asyncio/operations_v1/test_operations_async_client.py | 3 ++- tests/asyncio/retry/test_retry_streaming_async.py | 4 ++-- tests/asyncio/retry/test_retry_unary_async.py | 2 +- tests/asyncio/test_grpc_helpers_async.py | 3 ++- tests/asyncio/test_operation_async.py | 3 ++- tests/asyncio/test_page_iterator_async.py | 2 +- tests/asyncio/test_rest_streaming_async.py | 6 +++--- tests/unit/future/test__helpers.py | 2 +- tests/unit/future/test_polling.py | 2 +- tests/unit/gapic/test_method.py | 2 +- tests/unit/operations_v1/test_operations_rest_client.py | 2 +- tests/unit/retry/test_retry_base.py | 2 +- tests/unit/retry/test_retry_streaming.py | 2 +- tests/unit/retry/test_retry_unary.py | 2 +- tests/unit/test_bidi.py | 2 +- tests/unit/test_exceptions.py | 2 +- tests/unit/test_extended_operation.py | 2 +- tests/unit/test_grpc_helpers.py | 3 ++- tests/unit/test_operation.py | 3 ++- tests/unit/test_page_iterator.py | 2 +- tests/unit/test_path_template.py | 2 +- tests/unit/test_timeout.py | 3 +-- 25 files changed, 32 insertions(+), 30 deletions(-) diff --git a/noxfile.py b/noxfile.py index 144e3e210..327097f2b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -124,7 +124,6 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal session.install( "dataclasses", - "mock", "pytest", "pytest-cov", "pytest-xdist", @@ -280,7 +279,6 @@ def mypy(session): "types-setuptools", "types-requests", "types-protobuf", - "types-mock", "types-dataclasses", ) session.run("mypy", "google", "tests") diff --git a/tests/asyncio/future/test_async_future.py b/tests/asyncio/future/test_async_future.py index 0cfe67734..659f41cf9 100644 --- a/tests/asyncio/future/test_async_future.py +++ b/tests/asyncio/future/test_async_future.py @@ -13,8 +13,8 @@ # limitations under the License. import asyncio +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/asyncio/gapic/test_method_async.py b/tests/asyncio/gapic/test_method_async.py index f64157b44..026993e28 100644 --- a/tests/asyncio/gapic/test_method_async.py +++ b/tests/asyncio/gapic/test_method_async.py @@ -13,8 +13,8 @@ # limitations under the License. import datetime +from unittest import mock -import mock import pytest try: diff --git a/tests/asyncio/operations_v1/test_operations_async_client.py b/tests/asyncio/operations_v1/test_operations_async_client.py index 19ac9b566..e5b20dcd0 100644 --- a/tests/asyncio/operations_v1/test_operations_async_client.py +++ b/tests/asyncio/operations_v1/test_operations_async_client.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +from unittest import mock + import pytest try: diff --git a/tests/asyncio/retry/test_retry_streaming_async.py b/tests/asyncio/retry/test_retry_streaming_async.py index 28ae6ff17..a54fc125b 100644 --- a/tests/asyncio/retry/test_retry_streaming_async.py +++ b/tests/asyncio/retry/test_retry_streaming_async.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import asyncio import datetime import re -import asyncio +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/asyncio/retry/test_retry_unary_async.py b/tests/asyncio/retry/test_retry_unary_async.py index fc2f572b4..032bab756 100644 --- a/tests/asyncio/retry/test_retry_unary_async.py +++ b/tests/asyncio/retry/test_retry_unary_async.py @@ -14,8 +14,8 @@ import datetime import re +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index 1a408ccde..a53de083d 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +from unittest import mock + import pytest # noqa: I202 try: diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index 127ba6343..f7ebad8a2 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -13,7 +13,8 @@ # limitations under the License. -import mock +from unittest import mock + import pytest try: diff --git a/tests/asyncio/test_page_iterator_async.py b/tests/asyncio/test_page_iterator_async.py index 75f9e1cf4..e661bd266 100644 --- a/tests/asyncio/test_page_iterator_async.py +++ b/tests/asyncio/test_page_iterator_async.py @@ -13,8 +13,8 @@ # limitations under the License. import inspect +from unittest import mock -import mock import pytest from google.api_core import page_iterator_async diff --git a/tests/asyncio/test_rest_streaming_async.py b/tests/asyncio/test_rest_streaming_async.py index da5b1c8d6..f726cbee7 100644 --- a/tests/asyncio/test_rest_streaming_async.py +++ b/tests/asyncio/test_rest_streaming_async.py @@ -15,14 +15,14 @@ # TODO: set random.seed explicitly in each test function. # See related issue: https://github.com/googleapis/python-api-core/issues/689. -import pytest # noqa: I202 -import mock - import datetime import logging import random import time from typing import List, AsyncIterator +from unittest import mock + +import pytest # noqa: I202 import proto diff --git a/tests/unit/future/test__helpers.py b/tests/unit/future/test__helpers.py index 98afc599f..a37efdd40 100644 --- a/tests/unit/future/test__helpers.py +++ b/tests/unit/future/test__helpers.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +from unittest import mock from google.api_core.future import _helpers diff --git a/tests/unit/future/test_polling.py b/tests/unit/future/test_polling.py index f5d9b4f10..2f66f2304 100644 --- a/tests/unit/future/test_polling.py +++ b/tests/unit/future/test_polling.py @@ -15,8 +15,8 @@ import concurrent.futures import threading import time +from unittest import mock -import mock import pytest from google.api_core import exceptions, retry diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index d966f478f..87aa63905 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -13,8 +13,8 @@ # limitations under the License. import datetime +from unittest import mock -import mock import pytest try: diff --git a/tests/unit/operations_v1/test_operations_rest_client.py b/tests/unit/operations_v1/test_operations_rest_client.py index 4ab4f1f75..26f34c401 100644 --- a/tests/unit/operations_v1/test_operations_rest_client.py +++ b/tests/unit/operations_v1/test_operations_rest_client.py @@ -14,8 +14,8 @@ # limitations under the License. # import os +from unittest import mock -import mock import pytest try: diff --git a/tests/unit/retry/test_retry_base.py b/tests/unit/retry/test_retry_base.py index a0c6776bf..212c4293b 100644 --- a/tests/unit/retry/test_retry_base.py +++ b/tests/unit/retry/test_retry_base.py @@ -14,8 +14,8 @@ import itertools import re +from unittest import mock -import mock import pytest import requests.exceptions diff --git a/tests/unit/retry/test_retry_streaming.py b/tests/unit/retry/test_retry_streaming.py index 01f35327b..82b60b058 100644 --- a/tests/unit/retry/test_retry_streaming.py +++ b/tests/unit/retry/test_retry_streaming.py @@ -13,8 +13,8 @@ # limitations under the License. import re +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/unit/retry/test_retry_unary.py b/tests/unit/retry/test_retry_unary.py index 7dcd8dd6a..b018fa962 100644 --- a/tests/unit/retry/test_retry_unary.py +++ b/tests/unit/retry/test_retry_unary.py @@ -14,8 +14,8 @@ import datetime import re +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/unit/test_bidi.py b/tests/unit/test_bidi.py index 84ac9dc52..08e80afd0 100644 --- a/tests/unit/test_bidi.py +++ b/tests/unit/test_bidi.py @@ -16,8 +16,8 @@ import logging import queue import threading +from unittest import mock -import mock import pytest try: diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 07a368175..e3f8f909a 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -14,8 +14,8 @@ import http.client import json +from unittest import mock -import mock import pytest import requests diff --git a/tests/unit/test_extended_operation.py b/tests/unit/test_extended_operation.py index 53af52047..ab5506624 100644 --- a/tests/unit/test_extended_operation.py +++ b/tests/unit/test_extended_operation.py @@ -15,8 +15,8 @@ import dataclasses import enum import typing +from unittest import mock -import mock import pytest from google.api_core import exceptions diff --git a/tests/unit/test_grpc_helpers.py b/tests/unit/test_grpc_helpers.py index 59442d437..8de9d8c0b 100644 --- a/tests/unit/test_grpc_helpers.py +++ b/tests/unit/test_grpc_helpers.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +from unittest import mock + import pytest try: diff --git a/tests/unit/test_operation.py b/tests/unit/test_operation.py index f029866c6..806807201 100644 --- a/tests/unit/test_operation.py +++ b/tests/unit/test_operation.py @@ -13,7 +13,8 @@ # limitations under the License. -import mock +from unittest import mock + import pytest try: diff --git a/tests/unit/test_page_iterator.py b/tests/unit/test_page_iterator.py index cf43aedfb..560722c5d 100644 --- a/tests/unit/test_page_iterator.py +++ b/tests/unit/test_page_iterator.py @@ -14,8 +14,8 @@ import math import types +from unittest import mock -import mock import pytest from google.api_core import page_iterator diff --git a/tests/unit/test_path_template.py b/tests/unit/test_path_template.py index 808b36f34..c34dd0f32 100644 --- a/tests/unit/test_path_template.py +++ b/tests/unit/test_path_template.py @@ -13,8 +13,8 @@ # limitations under the License. from __future__ import unicode_literals +from unittest import mock -import mock import pytest from google.api import auth_pb2 diff --git a/tests/unit/test_timeout.py b/tests/unit/test_timeout.py index 0bcf07f07..60a2e65d3 100644 --- a/tests/unit/test_timeout.py +++ b/tests/unit/test_timeout.py @@ -14,8 +14,7 @@ import datetime import itertools - -import mock +from unittest import mock from google.api_core import timeout as timeouts From 5583db468a07b831d4fb05a4abcf3e788154f2cb Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Thu, 3 Oct 2024 15:33:38 +1000 Subject: [PATCH 2/6] test: Fallback to external mock for AsyncMock AsyncMock is not included in unittest.mock under Python 3.7, so we must fallback to the external mock requirement for that Python version. Only install it for that version. Keep this as a separate commit so it can be reverted when 3.7 isn't supported anymore. --- noxfile.py | 2 ++ tests/asyncio/gapic/test_method_async.py | 6 +++++- tests/asyncio/retry/test_retry_unary_async.py | 14 +++++++++----- tests/asyncio/test_grpc_helpers_async.py | 18 +++++++++++------- tests/asyncio/test_operation_async.py | 8 ++++++-- tests/asyncio/test_page_iterator_async.py | 16 ++++++++++------ tests/asyncio/test_rest_streaming_async.py | 8 ++++++-- tests/unit/retry/test_retry_unary.py | 6 +++++- 8 files changed, 54 insertions(+), 24 deletions(-) diff --git a/noxfile.py b/noxfile.py index 327097f2b..3dbd27df0 100644 --- a/noxfile.py +++ b/noxfile.py @@ -124,6 +124,7 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal session.install( "dataclasses", + "mock; python_version=='3.7'", "pytest", "pytest-cov", "pytest-xdist", @@ -280,6 +281,7 @@ def mypy(session): "types-requests", "types-protobuf", "types-dataclasses", + "types-mock; python_version=='3.7'", ) session.run("mypy", "google", "tests") diff --git a/tests/asyncio/gapic/test_method_async.py b/tests/asyncio/gapic/test_method_async.py index 026993e28..4bb879593 100644 --- a/tests/asyncio/gapic/test_method_async.py +++ b/tests/asyncio/gapic/test_method_async.py @@ -15,6 +15,10 @@ import datetime from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest try: @@ -256,7 +260,7 @@ async def test_wrap_method_with_overriding_timeout_as_a_number(): @pytest.mark.asyncio async def test_wrap_method_without_wrap_errors(): - fake_call = mock.AsyncMock() + fake_call = AsyncMock() wrapped_method = gapic_v1.method_async.wrap_method(fake_call, kind="rest") with mock.patch("google.api_core.grpc_helpers_async.wrap_errors") as method: diff --git a/tests/asyncio/retry/test_retry_unary_async.py b/tests/asyncio/retry/test_retry_unary_async.py index 032bab756..4b3327d77 100644 --- a/tests/asyncio/retry/test_retry_unary_async.py +++ b/tests/asyncio/retry/test_retry_unary_async.py @@ -16,6 +16,10 @@ import re from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest from google.api_core import exceptions @@ -168,7 +172,7 @@ def if_exception_type(exc): @pytest.mark.asyncio async def test___call___and_execute_success(self, sleep): retry_ = retry_async.AsyncRetry() - target = mock.AsyncMock(spec=["__call__"], return_value=42) + target = AsyncMock(spec=["__call__"], return_value=42) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -190,7 +194,7 @@ async def test___call___and_execute_retry(self, sleep, uniform): predicate=retry_async.if_exception_type(ValueError) ) - target = mock.AsyncMock(spec=["__call__"], side_effect=[ValueError(), 42]) + target = AsyncMock(spec=["__call__"], side_effect=[ValueError(), 42]) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -220,7 +224,7 @@ async def test___call___and_execute_retry_hitting_timeout(self, sleep, uniform): monotonic_patcher = mock.patch("time.monotonic", return_value=0) - target = mock.AsyncMock(spec=["__call__"], side_effect=[ValueError()] * 10) + target = AsyncMock(spec=["__call__"], side_effect=[ValueError()] * 10) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -270,7 +274,7 @@ async def test___init___without_retry_executed(self, sleep): # check the proper creation of the class assert retry_._on_error is _some_function - target = mock.AsyncMock(spec=["__call__"], side_effect=[42]) + target = AsyncMock(spec=["__call__"], side_effect=[42]) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -295,7 +299,7 @@ async def test___init___when_retry_is_executed(self, sleep, uniform): # check the proper creation of the class assert retry_._on_error is _some_function - target = mock.AsyncMock( + target = AsyncMock( spec=["__call__"], side_effect=[ValueError(), ValueError(), 42] ) # __name__ is needed by functools.partial. diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index a53de083d..534f36d8a 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -14,6 +14,10 @@ from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest # noqa: I202 try: @@ -50,7 +54,7 @@ def trailing_metadata(self): @pytest.mark.asyncio async def test_wrap_unary_errors(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) - callable_ = mock.AsyncMock(spec=["__call__"], side_effect=grpc_error) + callable_ = AsyncMock(spec=["__call__"], side_effect=grpc_error) wrapped_callable = grpc_helpers_async._wrap_unary_errors(callable_) @@ -170,7 +174,7 @@ async def test_wrap_stream_errors_stream_stream(): async def test_wrap_stream_errors_raised(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.wait_for_connection = mock.AsyncMock(side_effect=[grpc_error]) + mock_call.wait_for_connection = AsyncMock(side_effect=[grpc_error]) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( @@ -187,7 +191,7 @@ async def test_wrap_stream_errors_read(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.read = mock.AsyncMock(side_effect=grpc_error) + mock_call.read = AsyncMock(side_effect=grpc_error) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( @@ -209,7 +213,7 @@ async def test_wrap_stream_errors_aiter(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = mock.AsyncMock( + mocked_aiter.__anext__ = AsyncMock( side_effect=[mock.sentinel.response, grpc_error] ) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) @@ -232,7 +236,7 @@ async def test_wrap_stream_errors_aiter_non_rpc_error(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = mock.AsyncMock( + mocked_aiter.__anext__ = AsyncMock( side_effect=[mock.sentinel.response, non_grpc_error] ) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) @@ -267,8 +271,8 @@ async def test_wrap_stream_errors_write(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.write = mock.AsyncMock(side_effect=[None, grpc_error]) - mock_call.done_writing = mock.AsyncMock(side_effect=[None, grpc_error]) + mock_call.write = AsyncMock(side_effect=[None, grpc_error]) + mock_call.done_writing = AsyncMock(side_effect=[None, grpc_error]) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index f7ebad8a2..42cebe448 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -15,6 +15,10 @@ from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest try: @@ -55,9 +59,9 @@ def make_operation_future(client_operations_responses=None): if client_operations_responses is None: client_operations_responses = [make_operation_proto()] - refresh = mock.AsyncMock(spec=["__call__"], side_effect=client_operations_responses) + refresh = AsyncMock(spec=["__call__"], side_effect=client_operations_responses) refresh.responses = client_operations_responses - cancel = mock.AsyncMock(spec=["__call__"]) + cancel = AsyncMock(spec=["__call__"]) operation_future = operation_async.AsyncOperation( client_operations_responses[0], refresh, diff --git a/tests/asyncio/test_page_iterator_async.py b/tests/asyncio/test_page_iterator_async.py index e661bd266..c6e764cd6 100644 --- a/tests/asyncio/test_page_iterator_async.py +++ b/tests/asyncio/test_page_iterator_async.py @@ -15,6 +15,10 @@ import inspect from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest from google.api_core import page_iterator_async @@ -58,7 +62,7 @@ async def test_anext(self): ) async_iterator = PageAsyncIteratorImpl(None, None) - async_iterator._next_page = mock.AsyncMock(side_effect=[page_1, page_2, None]) + async_iterator._next_page = AsyncMock(side_effect=[page_1, page_2, None]) # Consume items and check the state of the async_iterator. assert async_iterator.num_results == 0 @@ -98,7 +102,7 @@ async def test__page_aiter_increment(self): page = page_iterator_async.Page( iterator, ("item",), page_iterator_async._item_to_value_identity ) - iterator._next_page = mock.AsyncMock(side_effect=[page, None]) + iterator._next_page = AsyncMock(side_effect=[page, None]) assert iterator.num_results == 0 @@ -136,7 +140,7 @@ async def test__items_aiter(self): ) iterator = PageAsyncIteratorImpl(None, None) - iterator._next_page = mock.AsyncMock(side_effect=[page1, page2, None]) + iterator._next_page = AsyncMock(side_effect=[page1, page2, None]) items_aiter = iterator._items_aiter() @@ -159,7 +163,7 @@ async def test__items_aiter(self): @pytest.mark.asyncio async def test___aiter__(self): async_iterator = PageAsyncIteratorImpl(None, None) - async_iterator._next_page = mock.AsyncMock(side_effect=[(1, 2), (3,), None]) + async_iterator._next_page = AsyncMock(side_effect=[(1, 2), (3,), None]) assert not async_iterator._started @@ -248,7 +252,7 @@ async def test_iterate(self): response1 = mock.Mock(items=["a", "b"], next_page_token="1") response2 = mock.Mock(items=["c"], next_page_token="2") response3 = mock.Mock(items=["d"], next_page_token="") - method = mock.AsyncMock(side_effect=[response1, response2, response3]) + method = AsyncMock(side_effect=[response1, response2, response3]) iterator = page_iterator_async.AsyncGRPCIterator( mock.sentinel.client, method, request, "items" ) @@ -271,7 +275,7 @@ async def test_iterate_with_max_results(self): response1 = mock.Mock(items=["a", "b"], next_page_token="1") response2 = mock.Mock(items=["c"], next_page_token="2") response3 = mock.Mock(items=["d"], next_page_token="") - method = mock.AsyncMock(side_effect=[response1, response2, response3]) + method = AsyncMock(side_effect=[response1, response2, response3]) iterator = page_iterator_async.AsyncGRPCIterator( mock.sentinel.client, method, request, "items", max_results=3 ) diff --git a/tests/asyncio/test_rest_streaming_async.py b/tests/asyncio/test_rest_streaming_async.py index f726cbee7..79d292da8 100644 --- a/tests/asyncio/test_rest_streaming_async.py +++ b/tests/asyncio/test_rest_streaming_async.py @@ -22,6 +22,10 @@ from typing import List, AsyncIterator from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest # noqa: I202 import proto @@ -303,7 +307,7 @@ async def test_next_not_array(response_type): @pytest.mark.parametrize("response_type", [EchoResponse, httpbody_pb2.HttpBody]) async def test_cancel(response_type): with mock.patch.object( - ResponseMock, "close", new_callable=mock.AsyncMock + ResponseMock, "close", new_callable=AsyncMock ) as mock_method: resp = ResponseMock(responses=[], response_cls=response_type) itr = rest_streaming_async.AsyncResponseIterator(resp, response_type) @@ -315,7 +319,7 @@ async def test_cancel(response_type): @pytest.mark.parametrize("response_type", [EchoResponse, httpbody_pb2.HttpBody]) async def test_iterator_as_context_manager(response_type): with mock.patch.object( - ResponseMock, "close", new_callable=mock.AsyncMock + ResponseMock, "close", new_callable=AsyncMock ) as mock_method: resp = ResponseMock(responses=[], response_cls=response_type) async with rest_streaming_async.AsyncResponseIterator(resp, response_type): diff --git a/tests/unit/retry/test_retry_unary.py b/tests/unit/retry/test_retry_unary.py index b018fa962..c8d758336 100644 --- a/tests/unit/retry/test_retry_unary.py +++ b/tests/unit/retry/test_retry_unary.py @@ -16,6 +16,10 @@ import re from unittest import mock +try: + from unittest.mock import AsyncMock +except ImportError: + from mock import AsyncMock import pytest from google.api_core import exceptions @@ -102,7 +106,7 @@ def test_retry_target_non_retryable_error(utcnow, sleep): @pytest.mark.asyncio async def test_retry_target_warning_for_retry(utcnow, sleep): predicate = retry.if_exception_type(ValueError) - target = mock.AsyncMock(spec=["__call__"]) + target = AsyncMock(spec=["__call__"]) with pytest.warns(Warning) as exc_info: # Note: predicate is just a filler and doesn't affect the test From 216eea85db46794474132ae35125c232cbe83b6a Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 14:49:56 +0000 Subject: [PATCH 3/6] lint --- tests/asyncio/test_grpc_helpers_async.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index 534f36d8a..da1672a76 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -213,9 +213,7 @@ async def test_wrap_stream_errors_aiter(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = AsyncMock( - side_effect=[mock.sentinel.response, grpc_error] - ) + mocked_aiter.__anext__ = AsyncMock(side_effect=[mock.sentinel.response, grpc_error]) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) multicallable = mock.Mock(return_value=mock_call) From df2ce107f2ecb94e631a8ae4bda04e7fa305d112 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 15:21:30 +0000 Subject: [PATCH 4/6] clean up to satisfy mypy --- tests/asyncio/gapic/test_method_async.py | 11 ++++----- tests/asyncio/retry/test_retry_unary_async.py | 19 ++++++++------- tests/asyncio/test_grpc_helpers_async.py | 23 +++++++++---------- tests/asyncio/test_operation_async.py | 15 ++++++------ tests/asyncio/test_page_iterator_async.py | 21 ++++++++--------- tests/asyncio/test_rest_streaming_async.py | 14 +++++------ tests/unit/retry/test_retry_unary.py | 13 +++++------ 7 files changed, 55 insertions(+), 61 deletions(-) diff --git a/tests/asyncio/gapic/test_method_async.py b/tests/asyncio/gapic/test_method_async.py index 4bb879593..afad0bb85 100644 --- a/tests/asyncio/gapic/test_method_async.py +++ b/tests/asyncio/gapic/test_method_async.py @@ -13,12 +13,11 @@ # limitations under the License. import datetime -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest try: @@ -260,7 +259,7 @@ async def test_wrap_method_with_overriding_timeout_as_a_number(): @pytest.mark.asyncio async def test_wrap_method_without_wrap_errors(): - fake_call = AsyncMock() + fake_call = mock.AsyncMock() wrapped_method = gapic_v1.method_async.wrap_method(fake_call, kind="rest") with mock.patch("google.api_core.grpc_helpers_async.wrap_errors") as method: diff --git a/tests/asyncio/retry/test_retry_unary_async.py b/tests/asyncio/retry/test_retry_unary_async.py index 4b3327d77..86825f64f 100644 --- a/tests/asyncio/retry/test_retry_unary_async.py +++ b/tests/asyncio/retry/test_retry_unary_async.py @@ -14,12 +14,11 @@ import datetime import re -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest from google.api_core import exceptions @@ -172,7 +171,7 @@ def if_exception_type(exc): @pytest.mark.asyncio async def test___call___and_execute_success(self, sleep): retry_ = retry_async.AsyncRetry() - target = AsyncMock(spec=["__call__"], return_value=42) + target = mock.AsyncMock(spec=["__call__"], return_value=42) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -194,7 +193,7 @@ async def test___call___and_execute_retry(self, sleep, uniform): predicate=retry_async.if_exception_type(ValueError) ) - target = AsyncMock(spec=["__call__"], side_effect=[ValueError(), 42]) + target = mock.AsyncMock(spec=["__call__"], side_effect=[ValueError(), 42]) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -224,7 +223,7 @@ async def test___call___and_execute_retry_hitting_timeout(self, sleep, uniform): monotonic_patcher = mock.patch("time.monotonic", return_value=0) - target = AsyncMock(spec=["__call__"], side_effect=[ValueError()] * 10) + target = mock.AsyncMock(spec=["__call__"], side_effect=[ValueError()] * 10) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -274,7 +273,7 @@ async def test___init___without_retry_executed(self, sleep): # check the proper creation of the class assert retry_._on_error is _some_function - target = AsyncMock(spec=["__call__"], side_effect=[42]) + target = mock.AsyncMock(spec=["__call__"], side_effect=[42]) # __name__ is needed by functools.partial. target.__name__ = "target" @@ -299,7 +298,7 @@ async def test___init___when_retry_is_executed(self, sleep, uniform): # check the proper creation of the class assert retry_._on_error is _some_function - target = AsyncMock( + target = mock.AsyncMock( spec=["__call__"], side_effect=[ValueError(), ValueError(), 42] ) # __name__ is needed by functools.partial. diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index da1672a76..4a3ad70f1 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest # noqa: I202 try: @@ -54,7 +53,7 @@ def trailing_metadata(self): @pytest.mark.asyncio async def test_wrap_unary_errors(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) - callable_ = AsyncMock(spec=["__call__"], side_effect=grpc_error) + callable_ = mock.AsyncMock(spec=["__call__"], side_effect=grpc_error) wrapped_callable = grpc_helpers_async._wrap_unary_errors(callable_) @@ -174,7 +173,7 @@ async def test_wrap_stream_errors_stream_stream(): async def test_wrap_stream_errors_raised(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.wait_for_connection = AsyncMock(side_effect=[grpc_error]) + mock_call.wait_for_connection = mock.AsyncMock(side_effect=[grpc_error]) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( @@ -191,7 +190,7 @@ async def test_wrap_stream_errors_read(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.read = AsyncMock(side_effect=grpc_error) + mock_call.read = mock.AsyncMock(side_effect=grpc_error) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( @@ -213,7 +212,7 @@ async def test_wrap_stream_errors_aiter(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = AsyncMock(side_effect=[mock.sentinel.response, grpc_error]) + mocked_aiter.__anext__ = mock.AsyncMock(side_effect=[mock.sentinel.response, grpc_error]) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) multicallable = mock.Mock(return_value=mock_call) @@ -234,7 +233,7 @@ async def test_wrap_stream_errors_aiter_non_rpc_error(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = AsyncMock( + mocked_aiter.__anext__ = mock.AsyncMock( side_effect=[mock.sentinel.response, non_grpc_error] ) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) @@ -269,8 +268,8 @@ async def test_wrap_stream_errors_write(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) - mock_call.write = AsyncMock(side_effect=[None, grpc_error]) - mock_call.done_writing = AsyncMock(side_effect=[None, grpc_error]) + mock_call.write = mock.AsyncMock(side_effect=[None, grpc_error]) + mock_call.done_writing = mock.AsyncMock(side_effect=[None, grpc_error]) multicallable = mock.Mock(return_value=mock_call) wrapped_callable = grpc_helpers_async._wrap_stream_errors( diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index 42cebe448..865b1e953 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -13,13 +13,12 @@ # limitations under the License. -from unittest import mock - -try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock import pytest +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore try: import grpc # noqa: F401 @@ -59,9 +58,9 @@ def make_operation_future(client_operations_responses=None): if client_operations_responses is None: client_operations_responses = [make_operation_proto()] - refresh = AsyncMock(spec=["__call__"], side_effect=client_operations_responses) + refresh = mock.AsyncMock(spec=["__call__"], side_effect=client_operations_responses) refresh.responses = client_operations_responses - cancel = AsyncMock(spec=["__call__"]) + cancel = mock.AsyncMock(spec=["__call__"]) operation_future = operation_async.AsyncOperation( client_operations_responses[0], refresh, diff --git a/tests/asyncio/test_page_iterator_async.py b/tests/asyncio/test_page_iterator_async.py index c6e764cd6..3f32f2de1 100644 --- a/tests/asyncio/test_page_iterator_async.py +++ b/tests/asyncio/test_page_iterator_async.py @@ -13,12 +13,11 @@ # limitations under the License. import inspect -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest from google.api_core import page_iterator_async @@ -62,7 +61,7 @@ async def test_anext(self): ) async_iterator = PageAsyncIteratorImpl(None, None) - async_iterator._next_page = AsyncMock(side_effect=[page_1, page_2, None]) + async_iterator._next_page = mock.AsyncMock(side_effect=[page_1, page_2, None]) # Consume items and check the state of the async_iterator. assert async_iterator.num_results == 0 @@ -102,7 +101,7 @@ async def test__page_aiter_increment(self): page = page_iterator_async.Page( iterator, ("item",), page_iterator_async._item_to_value_identity ) - iterator._next_page = AsyncMock(side_effect=[page, None]) + iterator._next_page = mock.AsyncMock(side_effect=[page, None]) assert iterator.num_results == 0 @@ -140,7 +139,7 @@ async def test__items_aiter(self): ) iterator = PageAsyncIteratorImpl(None, None) - iterator._next_page = AsyncMock(side_effect=[page1, page2, None]) + iterator._next_page = mock.AsyncMock(side_effect=[page1, page2, None]) items_aiter = iterator._items_aiter() @@ -163,7 +162,7 @@ async def test__items_aiter(self): @pytest.mark.asyncio async def test___aiter__(self): async_iterator = PageAsyncIteratorImpl(None, None) - async_iterator._next_page = AsyncMock(side_effect=[(1, 2), (3,), None]) + async_iterator._next_page = mock.AsyncMock(side_effect=[(1, 2), (3,), None]) assert not async_iterator._started @@ -252,7 +251,7 @@ async def test_iterate(self): response1 = mock.Mock(items=["a", "b"], next_page_token="1") response2 = mock.Mock(items=["c"], next_page_token="2") response3 = mock.Mock(items=["d"], next_page_token="") - method = AsyncMock(side_effect=[response1, response2, response3]) + method = mock.AsyncMock(side_effect=[response1, response2, response3]) iterator = page_iterator_async.AsyncGRPCIterator( mock.sentinel.client, method, request, "items" ) @@ -275,7 +274,7 @@ async def test_iterate_with_max_results(self): response1 = mock.Mock(items=["a", "b"], next_page_token="1") response2 = mock.Mock(items=["c"], next_page_token="2") response3 = mock.Mock(items=["d"], next_page_token="") - method = AsyncMock(side_effect=[response1, response2, response3]) + method = mock.AsyncMock(side_effect=[response1, response2, response3]) iterator = page_iterator_async.AsyncGRPCIterator( mock.sentinel.client, method, request, "items", max_results=3 ) diff --git a/tests/asyncio/test_rest_streaming_async.py b/tests/asyncio/test_rest_streaming_async.py index 79d292da8..5c56eff0e 100644 --- a/tests/asyncio/test_rest_streaming_async.py +++ b/tests/asyncio/test_rest_streaming_async.py @@ -20,12 +20,12 @@ import random import time from typing import List, AsyncIterator -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore + import pytest # noqa: I202 import proto @@ -307,7 +307,7 @@ async def test_next_not_array(response_type): @pytest.mark.parametrize("response_type", [EchoResponse, httpbody_pb2.HttpBody]) async def test_cancel(response_type): with mock.patch.object( - ResponseMock, "close", new_callable=AsyncMock + ResponseMock, "close", new_callable=mock.AsyncMock ) as mock_method: resp = ResponseMock(responses=[], response_cls=response_type) itr = rest_streaming_async.AsyncResponseIterator(resp, response_type) @@ -319,7 +319,7 @@ async def test_cancel(response_type): @pytest.mark.parametrize("response_type", [EchoResponse, httpbody_pb2.HttpBody]) async def test_iterator_as_context_manager(response_type): with mock.patch.object( - ResponseMock, "close", new_callable=AsyncMock + ResponseMock, "close", new_callable=mock.AsyncMock ) as mock_method: resp = ResponseMock(responses=[], response_cls=response_type) async with rest_streaming_async.AsyncResponseIterator(resp, response_type): diff --git a/tests/unit/retry/test_retry_unary.py b/tests/unit/retry/test_retry_unary.py index c8d758336..f7e37ad1f 100644 --- a/tests/unit/retry/test_retry_unary.py +++ b/tests/unit/retry/test_retry_unary.py @@ -13,14 +13,13 @@ # limitations under the License. import datetime +import pytest import re -from unittest import mock - try: - from unittest.mock import AsyncMock -except ImportError: - from mock import AsyncMock -import pytest + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER +except ImportError: # pragma: NO COVER + import mock # type: ignore from google.api_core import exceptions from google.api_core import retry @@ -106,7 +105,7 @@ def test_retry_target_non_retryable_error(utcnow, sleep): @pytest.mark.asyncio async def test_retry_target_warning_for_retry(utcnow, sleep): predicate = retry.if_exception_type(ValueError) - target = AsyncMock(spec=["__call__"]) + target = mock.AsyncMock(spec=["__call__"]) with pytest.warns(Warning) as exc_info: # Note: predicate is just a filler and doesn't affect the test From e934f8700fe400cb5036debdf835285371e5427e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 15:28:10 +0000 Subject: [PATCH 5/6] lint --- tests/asyncio/gapic/test_method_async.py | 3 ++- tests/asyncio/retry/test_retry_unary_async.py | 3 ++- tests/asyncio/test_grpc_helpers_async.py | 6 ++++-- tests/asyncio/test_operation_async.py | 3 ++- tests/asyncio/test_page_iterator_async.py | 3 ++- tests/asyncio/test_rest_streaming_async.py | 3 ++- tests/unit/retry/test_retry_unary.py | 3 ++- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/asyncio/gapic/test_method_async.py b/tests/asyncio/gapic/test_method_async.py index afad0bb85..73f67b8db 100644 --- a/tests/asyncio/gapic/test_method_async.py +++ b/tests/asyncio/gapic/test_method_async.py @@ -13,9 +13,10 @@ # limitations under the License. import datetime + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore import pytest diff --git a/tests/asyncio/retry/test_retry_unary_async.py b/tests/asyncio/retry/test_retry_unary_async.py index 86825f64f..dc64299f9 100644 --- a/tests/asyncio/retry/test_retry_unary_async.py +++ b/tests/asyncio/retry/test_retry_unary_async.py @@ -14,9 +14,10 @@ import datetime import re + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore import pytest diff --git a/tests/asyncio/test_grpc_helpers_async.py b/tests/asyncio/test_grpc_helpers_async.py index 4a3ad70f1..d8f20ae44 100644 --- a/tests/asyncio/test_grpc_helpers_async.py +++ b/tests/asyncio/test_grpc_helpers_async.py @@ -14,7 +14,7 @@ try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore import pytest # noqa: I202 @@ -212,7 +212,9 @@ async def test_wrap_stream_errors_aiter(): mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) mocked_aiter = mock.Mock(spec=["__anext__"]) - mocked_aiter.__anext__ = mock.AsyncMock(side_effect=[mock.sentinel.response, grpc_error]) + mocked_aiter.__anext__ = mock.AsyncMock( + side_effect=[mock.sentinel.response, grpc_error] + ) mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) multicallable = mock.Mock(return_value=mock_call) diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index 865b1e953..939be0947 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -14,9 +14,10 @@ import pytest + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore diff --git a/tests/asyncio/test_page_iterator_async.py b/tests/asyncio/test_page_iterator_async.py index 3f32f2de1..d8bba9345 100644 --- a/tests/asyncio/test_page_iterator_async.py +++ b/tests/asyncio/test_page_iterator_async.py @@ -13,9 +13,10 @@ # limitations under the License. import inspect + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore import pytest diff --git a/tests/asyncio/test_rest_streaming_async.py b/tests/asyncio/test_rest_streaming_async.py index 5c56eff0e..c9caa2b15 100644 --- a/tests/asyncio/test_rest_streaming_async.py +++ b/tests/asyncio/test_rest_streaming_async.py @@ -20,9 +20,10 @@ import random import time from typing import List, AsyncIterator + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore diff --git a/tests/unit/retry/test_retry_unary.py b/tests/unit/retry/test_retry_unary.py index f7e37ad1f..6851fbe48 100644 --- a/tests/unit/retry/test_retry_unary.py +++ b/tests/unit/retry/test_retry_unary.py @@ -15,9 +15,10 @@ import datetime import pytest import re + try: from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 except ImportError: # pragma: NO COVER import mock # type: ignore From 8f99f518cef21d4b483cd482811dfdffb64f727b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 15:53:00 +0000 Subject: [PATCH 6/6] fix build --- tests/asyncio/retry/test_retry_streaming_async.py | 7 ++++++- tests/unit/operations_v1/test_operations_rest_client.py | 7 ++++++- tests/unit/retry/test_retry_streaming.py | 7 ++++++- tests/unit/test_bidi.py | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/asyncio/retry/test_retry_streaming_async.py b/tests/asyncio/retry/test_retry_streaming_async.py index a54fc125b..ffdf314a1 100644 --- a/tests/asyncio/retry/test_retry_streaming_async.py +++ b/tests/asyncio/retry/test_retry_streaming_async.py @@ -15,7 +15,12 @@ import asyncio import datetime import re -from unittest import mock + +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest diff --git a/tests/unit/operations_v1/test_operations_rest_client.py b/tests/unit/operations_v1/test_operations_rest_client.py index 93e9bf177..d1f6e0eba 100644 --- a/tests/unit/operations_v1/test_operations_rest_client.py +++ b/tests/unit/operations_v1/test_operations_rest_client.py @@ -14,7 +14,12 @@ # limitations under the License. # import os -from unittest import mock + +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest from typing import Any, List diff --git a/tests/unit/retry/test_retry_streaming.py b/tests/unit/retry/test_retry_streaming.py index 82b60b058..0bc85d926 100644 --- a/tests/unit/retry/test_retry_streaming.py +++ b/tests/unit/retry/test_retry_streaming.py @@ -13,7 +13,12 @@ # limitations under the License. import re -from unittest import mock + +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest diff --git a/tests/unit/test_bidi.py b/tests/unit/test_bidi.py index 08e80afd0..c196a6821 100644 --- a/tests/unit/test_bidi.py +++ b/tests/unit/test_bidi.py @@ -16,7 +16,12 @@ import logging import queue import threading -from unittest import mock + +try: + from unittest import mock + from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401 +except ImportError: # pragma: NO COVER + import mock # type: ignore import pytest