Skip to content

Commit

Permalink
feat: add 'py.typed' declaration file (#287)
Browse files Browse the repository at this point in the history
Check typing using new 'mypy' nox session.
  • Loading branch information
tseaver committed Nov 18, 2021
1 parent d082772 commit cee4164
Show file tree
Hide file tree
Showing 33 changed files with 85 additions and 67 deletions.
2 changes: 1 addition & 1 deletion google/__init__.py
Expand Up @@ -19,4 +19,4 @@
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore
2 changes: 1 addition & 1 deletion google/_async_resumable_media/_upload.py
Expand Up @@ -612,7 +612,7 @@ def _make_invalid(self):
"""
self._invalid = True

async def _process_response(self, response, bytes_sent):
async def _process_resumable_response(self, response, bytes_sent):
"""Process the response from an HTTP request.
This is everything that must be done after a request that doesn't
Expand Down
4 changes: 2 additions & 2 deletions google/_async_resumable_media/requests/_request_helpers.py
Expand Up @@ -23,8 +23,8 @@
from google._async_resumable_media import _helpers
from google.resumable_media import common

import google.auth.transport._aiohttp_requests as aiohttp_requests
import aiohttp
from google.auth.transport import _aiohttp_requests as aiohttp_requests # type: ignore
import aiohttp # type: ignore

_DEFAULT_RETRY_STRATEGY = common.RetryStrategy()
_SINGLE_GET_CHUNK_SIZE = 8192
Expand Down
2 changes: 1 addition & 1 deletion google/_async_resumable_media/requests/download.py
Expand Up @@ -14,7 +14,7 @@

"""Support for downloading media from Google APIs."""

import urllib3.response
import urllib3.response # type: ignore

from google._async_resumable_media import _download
from google._async_resumable_media import _helpers
Expand Down
2 changes: 1 addition & 1 deletion google/_async_resumable_media/requests/upload.py
Expand Up @@ -481,7 +481,7 @@ async def transmit_next_chunk(
retry_strategy=self._retry_strategy,
timeout=timeout,
)
await self._process_response(response, len(payload))
await self._process_resumable_response(response, len(payload))
return response

async def recover(self, transport):
Expand Down
4 changes: 2 additions & 2 deletions google/resumable_media/_helpers.py
Expand Up @@ -140,12 +140,12 @@ def _get_crc32c_object():
to use CRCMod. CRCMod might be using a 'slow' varietal. If so, warn...
"""
try:
import google_crc32c
import google_crc32c # type: ignore

crc_obj = google_crc32c.Checksum()
except ImportError:
try:
import crcmod
import crcmod # type: ignore

crc_obj = crcmod.predefined.Crc("crc-32c")
_is_fast_crcmod()
Expand Down
2 changes: 1 addition & 1 deletion google/resumable_media/_upload.py
Expand Up @@ -647,7 +647,7 @@ def _make_invalid(self):
"""
self._invalid = True

def _process_response(self, response, bytes_sent):
def _process_resumable_response(self, response, bytes_sent):
"""Process the response from an HTTP request.
This is everything that must be done after a request that doesn't
Expand Down
2 changes: 1 addition & 1 deletion google/resumable_media/common.py
Expand Up @@ -26,7 +26,7 @@
UPLOAD_CHUNK_SIZE = 262144 # 256 * 1024
"""int: Chunks in a resumable upload must come in multiples of 256 KB."""

PERMANENT_REDIRECT = http.client.PERMANENT_REDIRECT
PERMANENT_REDIRECT = http.client.PERMANENT_REDIRECT # type: ignore
"""int: Permanent redirect status code.
.. note::
Expand Down
2 changes: 2 additions & 0 deletions google/resumable_media/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# The google-resumable_media package uses inline types.
2 changes: 1 addition & 1 deletion google/resumable_media/requests/_request_helpers.py
Expand Up @@ -18,7 +18,7 @@
"""

import requests.exceptions
import urllib3.exceptions
import urllib3.exceptions # type: ignore

import time

Expand Down
2 changes: 1 addition & 1 deletion google/resumable_media/requests/download.py
Expand Up @@ -14,7 +14,7 @@

"""Support for downloading media from Google APIs."""

import urllib3.response
import urllib3.response # type: ignore

from google.resumable_media import _download
from google.resumable_media import common
Expand Down
2 changes: 1 addition & 1 deletion google/resumable_media/requests/upload.py
Expand Up @@ -508,7 +508,7 @@ def retriable_request():
method, url, data=payload, headers=headers, timeout=timeout
)

self._process_response(result, len(payload))
self._process_resumable_response(result, len(payload))

return result

Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
@@ -0,0 +1,3 @@
[mypy]
python_version = 3.6
namespace_packages = True
13 changes: 13 additions & 0 deletions noxfile.py
Expand Up @@ -188,6 +188,19 @@ def blacken(session):
)


@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy(session):
"""Verify type hints are mypy compatible."""
session.install("-e", ".")
session.install(
"mypy",
"types-setuptools",
"types-requests",
"types-mock",
)
session.run("mypy", "google/", "tests/", "tests_async/")


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system(session):
"""Run the system test suite."""
Expand Down
6 changes: 3 additions & 3 deletions tests/system/requests/conftest.py
Expand Up @@ -13,9 +13,9 @@
# limitations under the License.
"""py.test fixtures to be shared across multiple system test modules."""

import google.auth
import google.auth.transport.requests as tr_requests
import pytest
import google.auth # type: ignore
import google.auth.transport.requests as tr_requests # type: ignore
import pytest # type: ignore

from tests.system import utils

Expand Down
6 changes: 3 additions & 3 deletions tests/system/requests/test_download.py
Expand Up @@ -19,9 +19,9 @@
import io
import os

import google.auth
import google.auth.transport.requests as tr_requests
import pytest
import google.auth # type: ignore
import google.auth.transport.requests as tr_requests # type: ignore
import pytest # type: ignore

from google.resumable_media import common
import google.resumable_media.requests as resumable_requests
Expand Down
2 changes: 1 addition & 1 deletion tests/system/requests/test_upload.py
Expand Up @@ -19,7 +19,7 @@
import os
import urllib.parse

import pytest
import pytest # type: ignore
import mock

from google.resumable_media import common
Expand Down
2 changes: 1 addition & 1 deletion tests/system/utils.py
Expand Up @@ -16,7 +16,7 @@
import hashlib
import time

from test_utils.retry import RetryResult
from test_utils.retry import RetryResult # type: ignore


BUCKET_NAME = "grpm-systest-{}".format(int(1000 * time.time()))
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/requests/test__helpers.py
Expand Up @@ -15,10 +15,10 @@
import http.client

import mock
import pytest
import pytest # type: ignore

import requests.exceptions
import urllib3.exceptions
import urllib3.exceptions # type: ignore

from google.resumable_media import common
from google.resumable_media.requests import _request_helpers
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/requests/test_download.py
Expand Up @@ -16,7 +16,7 @@
import io

import mock
import pytest
import pytest # type: ignore

from google.resumable_media import common
from google.resumable_media import _helpers
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test__download.py
Expand Up @@ -16,7 +16,7 @@
import io

import mock
import pytest
import pytest # type: ignore

from google.resumable_media import _download
from google.resumable_media import common
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test__helpers.py
Expand Up @@ -18,7 +18,7 @@
import http.client

import mock
import pytest
import pytest # type: ignore

from google.resumable_media import _helpers
from google.resumable_media import common
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test__upload.py
Expand Up @@ -17,7 +17,7 @@
import sys

import mock
import pytest
import pytest # type: ignore

from google.resumable_media import _helpers
from google.resumable_media import _upload
Expand Down Expand Up @@ -705,15 +705,15 @@ def test__make_invalid(self):
upload._make_invalid()
assert upload.invalid

def test__process_response_bad_status(self):
def test__process_resumable_response_bad_status(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
_fix_up_virtual(upload)

# Make sure the upload is valid before the failure.
assert not upload.invalid
response = _make_response(status_code=http.client.NOT_FOUND)
with pytest.raises(common.InvalidResponse) as exc_info:
upload._process_response(response, None)
upload._process_resumable_response(response, None)

error = exc_info.value
assert error.response is response
Expand All @@ -724,7 +724,7 @@ def test__process_response_bad_status(self):
# Make sure the upload is invalid after the failure.
assert upload.invalid

def test__process_response_success(self):
def test__process_resumable_response_success(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
_fix_up_virtual(upload)

Expand All @@ -743,21 +743,21 @@ def test__process_response_success(self):
status_code=http.client.OK,
spec=["content", "status_code"],
)
ret_val = upload._process_response(response, bytes_sent)
ret_val = upload._process_resumable_response(response, bytes_sent)
assert ret_val is None
# Check status after.
assert upload._bytes_uploaded == total_bytes
assert upload._finished

def test__process_response_partial_no_range(self):
def test__process_resumable_response_partial_no_range(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
_fix_up_virtual(upload)

response = _make_response(status_code=http.client.PERMANENT_REDIRECT)
# Make sure the upload is valid before the failure.
assert not upload.invalid
with pytest.raises(common.InvalidResponse) as exc_info:
upload._process_response(response, None)
upload._process_resumable_response(response, None)
# Make sure the upload is invalid after the failure.
assert upload.invalid

Expand All @@ -767,7 +767,7 @@ def test__process_response_partial_no_range(self):
assert len(error.args) == 2
assert error.args[1] == "range"

def test__process_response_partial_bad_range(self):
def test__process_resumable_response_partial_bad_range(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
_fix_up_virtual(upload)

Expand All @@ -778,7 +778,7 @@ def test__process_response_partial_bad_range(self):
status_code=http.client.PERMANENT_REDIRECT, headers=headers
)
with pytest.raises(common.InvalidResponse) as exc_info:
upload._process_response(response, 81)
upload._process_resumable_response(response, 81)

# Check the error response.
error = exc_info.value
Expand All @@ -788,7 +788,7 @@ def test__process_response_partial_bad_range(self):
# Make sure the upload is invalid after the failure.
assert upload.invalid

def test__process_response_partial(self):
def test__process_resumable_response_partial(self):
upload = _upload.ResumableUpload(RESUMABLE_URL, ONE_MB)
_fix_up_virtual(upload)

Expand All @@ -798,7 +798,7 @@ def test__process_response_partial(self):
response = _make_response(
status_code=http.client.PERMANENT_REDIRECT, headers=headers
)
ret_val = upload._process_response(response, 172)
ret_val = upload._process_resumable_response(response, 172)
assert ret_val is None
# Check status after.
assert upload._bytes_uploaded == 172
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_common.py
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import mock
import pytest
import pytest # type: ignore

from google.resumable_media import common

Expand Down
6 changes: 3 additions & 3 deletions tests_async/system/requests/conftest.py
Expand Up @@ -15,9 +15,9 @@

from tests.system import utils

from google.auth._default_async import default_async
import google.auth.transport._aiohttp_requests as tr_requests
import pytest
from google.auth._default_async import default_async # type: ignore
import google.auth.transport._aiohttp_requests as tr_requests # type: ignore
import pytest # type: ignore


async def ensure_bucket(transport):
Expand Down
8 changes: 4 additions & 4 deletions tests_async/system/requests/test_download.py
Expand Up @@ -20,10 +20,10 @@
import os

import asyncio
from google.auth._default_async import default_async
import google.auth.transport._aiohttp_requests as tr_requests
import multidict
import pytest
from google.auth._default_async import default_async # type: ignore
import google.auth.transport._aiohttp_requests as tr_requests # type: ignore
import multidict # type: ignore
import pytest # type: ignore

import google._async_resumable_media.requests as resumable_requests
from google.resumable_media import _helpers
Expand Down
2 changes: 1 addition & 1 deletion tests_async/system/requests/test_upload.py
Expand Up @@ -21,7 +21,7 @@

import asyncio
import mock
import pytest
import pytest # type: ignore

from google.resumable_media import common
from google import _async_resumable_media
Expand Down
4 changes: 2 additions & 2 deletions tests_async/unit/requests/test__helpers.py
Expand Up @@ -15,9 +15,9 @@
import http.client
import io

import aiohttp
import aiohttp # type: ignore
import mock
import pytest
import pytest # type: ignore

from google._async_resumable_media.requests import _request_helpers as _helpers

Expand Down
4 changes: 2 additions & 2 deletions tests_async/unit/requests/test_download.py
Expand Up @@ -15,9 +15,9 @@
import http.client
import io

import aiohttp
import aiohttp # type: ignore
import mock
import pytest
import pytest # type: ignore


from google.resumable_media import common
Expand Down
6 changes: 3 additions & 3 deletions tests_async/unit/requests/test_upload.py
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import aiohttp
import http.client
import io
import json
import pytest

import mock
import aiohttp # type: ignore
import mock # type: ignore
import pytest # type: ignore

import google._async_resumable_media.requests.upload as upload_mod
from tests.unit.requests import test_upload as sync_test
Expand Down

0 comments on commit cee4164

Please sign in to comment.