Skip to content

Commit 8c53381

Browse files
s-t-e-v-e-n-kparthea
andauthoredOct 9, 2024··
fix: Switch to unittest.mock from mock (#713)
* 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 * 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. * lint * clean up to satisfy mypy * lint * fix build --------- Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent b2baf47 commit 8c53381

25 files changed

+83
-31
lines changed
 

‎noxfile.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal
124124

125125
session.install(
126126
"dataclasses",
127-
"mock",
127+
"mock; python_version=='3.7'",
128128
"pytest",
129129
"pytest-cov",
130130
"pytest-xdist",
@@ -280,8 +280,8 @@ def mypy(session):
280280
"types-setuptools",
281281
"types-requests",
282282
"types-protobuf",
283-
"types-mock",
284283
"types-dataclasses",
284+
"types-mock; python_version=='3.7'",
285285
)
286286
session.run("mypy", "google", "tests")
287287

‎tests/asyncio/future/test_async_future.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
from unittest import mock
1617

17-
import mock
1818
import pytest
1919

2020
from google.api_core import exceptions

‎tests/asyncio/gapic/test_method_async.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
import datetime
1616

17-
import mock
17+
try:
18+
from unittest import mock
19+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
20+
except ImportError: # pragma: NO COVER
21+
import mock # type: ignore
1822
import pytest
1923

2024
try:

‎tests/asyncio/operations_v1/test_operations_async_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
15+
from unittest import mock
16+
1617
import pytest
1718

1819
try:

‎tests/asyncio/retry/test_retry_streaming_async.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import asyncio
1516
import datetime
1617
import re
17-
import asyncio
1818

19-
import mock
19+
try:
20+
from unittest import mock
21+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
22+
except ImportError: # pragma: NO COVER
23+
import mock # type: ignore
24+
2025
import pytest
2126

2227
from google.api_core import exceptions

‎tests/asyncio/retry/test_retry_unary_async.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import datetime
1616
import re
1717

18-
import mock
18+
try:
19+
from unittest import mock
20+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
21+
except ImportError: # pragma: NO COVER
22+
import mock # type: ignore
1923
import pytest
2024

2125
from google.api_core import exceptions

‎tests/asyncio/test_grpc_helpers_async.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
15+
try:
16+
from unittest import mock
17+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
18+
except ImportError: # pragma: NO COVER
19+
import mock # type: ignore
1620
import pytest # noqa: I202
1721

1822
try:

‎tests/asyncio/test_operation_async.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
# limitations under the License.
1414

1515

16-
import mock
1716
import pytest
1817

18+
try:
19+
from unittest import mock
20+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
21+
except ImportError: # pragma: NO COVER
22+
import mock # type: ignore
23+
1924
try:
2025
import grpc # noqa: F401
2126
except ImportError: # pragma: NO COVER

‎tests/asyncio/test_page_iterator_async.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
import inspect
1616

17-
import mock
17+
try:
18+
from unittest import mock
19+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
20+
except ImportError: # pragma: NO COVER
21+
import mock # type: ignore
1822
import pytest
1923

2024
from google.api_core import page_iterator_async

‎tests/asyncio/test_rest_streaming_async.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
# TODO: set random.seed explicitly in each test function.
1616
# See related issue: https://github.com/googleapis/python-api-core/issues/689.
1717

18-
import pytest # noqa: I202
19-
import mock
20-
2118
import datetime
2219
import logging
2320
import random
2421
import time
2522
from typing import List, AsyncIterator
2623

24+
try:
25+
from unittest import mock
26+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
27+
except ImportError: # pragma: NO COVER
28+
import mock # type: ignore
29+
30+
import pytest # noqa: I202
31+
2732
import proto
2833

2934
try:

‎tests/unit/future/test__helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
15+
from unittest import mock
1616

1717
from google.api_core.future import _helpers
1818

‎tests/unit/future/test_polling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import concurrent.futures
1616
import threading
1717
import time
18+
from unittest import mock
1819

19-
import mock
2020
import pytest
2121

2222
from google.api_core import exceptions, retry

‎tests/unit/gapic/test_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414

1515
import datetime
16+
from unittest import mock
1617

17-
import mock
1818
import pytest
1919

2020
try:

‎tests/unit/operations_v1/test_operations_rest_client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
#
1616
import os
1717

18-
import mock
18+
try:
19+
from unittest import mock
20+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
21+
except ImportError: # pragma: NO COVER
22+
import mock # type: ignore
23+
1924
import pytest
2025
from typing import Any, List
2126

‎tests/unit/retry/test_retry_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import itertools
1616
import re
17+
from unittest import mock
1718

18-
import mock
1919
import pytest
2020
import requests.exceptions
2121

‎tests/unit/retry/test_retry_streaming.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
import re
1616

17-
import mock
17+
try:
18+
from unittest import mock
19+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
20+
except ImportError: # pragma: NO COVER
21+
import mock # type: ignore
22+
1823
import pytest
1924

2025
from google.api_core import exceptions

‎tests/unit/retry/test_retry_unary.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
# limitations under the License.
1414

1515
import datetime
16+
import pytest
1617
import re
1718

18-
import mock
19-
import pytest
19+
try:
20+
from unittest import mock
21+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
22+
except ImportError: # pragma: NO COVER
23+
import mock # type: ignore
2024

2125
from google.api_core import exceptions
2226
from google.api_core import retry

‎tests/unit/test_bidi.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import queue
1818
import threading
1919

20-
import mock
20+
try:
21+
from unittest import mock
22+
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
23+
except ImportError: # pragma: NO COVER
24+
import mock # type: ignore
25+
2126
import pytest
2227

2328
try:

‎tests/unit/test_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import http.client
1616
import json
17+
from unittest import mock
1718

18-
import mock
1919
import pytest
2020
import requests
2121

‎tests/unit/test_extended_operation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import dataclasses
1616
import enum
1717
import typing
18+
from unittest import mock
1819

19-
import mock
2020
import pytest
2121

2222
from google.api_core import exceptions

‎tests/unit/test_grpc_helpers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
15+
from unittest import mock
16+
1617
import pytest
1718

1819
try:

‎tests/unit/test_operation.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# limitations under the License.
1414

1515

16-
import mock
16+
from unittest import mock
17+
1718
import pytest
1819

1920
try:

‎tests/unit/test_page_iterator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import math
1616
import types
17+
from unittest import mock
1718

18-
import mock
1919
import pytest
2020

2121
from google.api_core import page_iterator

‎tests/unit/test_path_template.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414

1515
from __future__ import unicode_literals
16+
from unittest import mock
1617

17-
import mock
1818
import pytest
1919

2020
from google.api import auth_pb2

‎tests/unit/test_timeout.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
import datetime
1616
import itertools
17-
18-
import mock
17+
from unittest import mock
1918

2019
from google.api_core import timeout as timeouts
2120

0 commit comments

Comments
 (0)
Please sign in to comment.