Skip to content

Commit

Permalink
fix: Switch to unittest.mock from mock (#713)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
s-t-e-v-e-n-k and parthea authored Oct 9, 2024
1 parent b2baf47 commit 8c53381
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 31 deletions.
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal

session.install(
"dataclasses",
"mock",
"mock; python_version=='3.7'",
"pytest",
"pytest-cov",
"pytest-xdist",
Expand Down Expand Up @@ -280,8 +280,8 @@ def mypy(session):
"types-setuptools",
"types-requests",
"types-protobuf",
"types-mock",
"types-dataclasses",
"types-mock; python_version=='3.7'",
)
session.run("mypy", "google", "tests")

Expand Down
2 changes: 1 addition & 1 deletion tests/asyncio/future/test_async_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

import asyncio
from unittest import mock

import mock
import pytest

from google.api_core import exceptions
Expand Down
6 changes: 5 additions & 1 deletion tests/asyncio/gapic/test_method_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import datetime

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

try:
Expand Down
3 changes: 2 additions & 1 deletion tests/asyncio/operations_v1/test_operations_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions tests/asyncio/retry/test_retry_streaming_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import datetime
import re
import asyncio

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 google.api_core import exceptions
Expand Down
6 changes: 5 additions & 1 deletion tests/asyncio/retry/test_retry_unary_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import datetime
import re

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 google.api_core import exceptions
Expand Down
6 changes: 5 additions & 1 deletion tests/asyncio/test_grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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 # noqa: I202

try:
Expand Down
7 changes: 6 additions & 1 deletion tests/asyncio/test_operation_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
# limitations under the License.


import mock
import pytest

try:
from unittest import mock
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
except ImportError: # pragma: NO COVER
import mock # type: ignore

try:
import grpc # noqa: F401
except ImportError: # pragma: NO COVER
Expand Down
6 changes: 5 additions & 1 deletion tests/asyncio/test_page_iterator_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import inspect

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 google.api_core import page_iterator_async
Expand Down
11 changes: 8 additions & 3 deletions tests/asyncio/test_rest_streaming_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
# 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

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 # noqa: I202

import proto

try:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/future/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/future/test_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/gapic/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

import datetime
from unittest import mock

import mock
import pytest

try:
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/operations_v1/test_operations_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
#
import os

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

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/retry/test_retry_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import itertools
import re
from unittest import mock

import mock
import pytest
import requests.exceptions

Expand Down
7 changes: 6 additions & 1 deletion tests/unit/retry/test_retry_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

import re

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 google.api_core import exceptions
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/retry/test_retry_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
# limitations under the License.

import datetime
import pytest
import re

import mock
import pytest
try:
from unittest import mock
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
except ImportError: # pragma: NO COVER
import mock # type: ignore

from google.api_core import exceptions
from google.api_core import retry
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/test_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import queue
import threading

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

try:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import http.client
import json
from unittest import mock

import mock
import pytest
import requests

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_extended_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import dataclasses
import enum
import typing
from unittest import mock

import mock
import pytest

from google.api_core import exceptions
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.


import mock
from unittest import mock

import pytest

try:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_page_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import math
import types
from unittest import mock

import mock
import pytest

from google.api_core import page_iterator
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_path_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import datetime
import itertools

import mock
from unittest import mock

from google.api_core import timeout as timeouts

Expand Down

0 comments on commit 8c53381

Please sign in to comment.