Skip to content

Commit 8c53381

Browse files
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 2 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 8 additions & 3 deletions
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:

0 commit comments

Comments
 (0)