Skip to content

Commit

Permalink
feat: add 'py.typed' declaration (#919)
Browse files Browse the repository at this point in the history
Test types under CI using new 'mypy' nox session.

Co-authored-by: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com>
  • Loading branch information
tseaver and arithmetic1728 authored Jan 6, 2022
1 parent dc6a4c1 commit c993504
Show file tree
Hide file tree
Showing 68 changed files with 151 additions and 133 deletions.
2 changes: 1 addition & 1 deletion google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore
10 changes: 3 additions & 7 deletions google/auth/_jwt_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
change in minor releases.
"""

import google.auth
from google.auth import _credentials_async
from google.auth import jwt


Expand Down Expand Up @@ -91,9 +91,7 @@ def decode(token, certs=None, verify=True, audience=None):


class Credentials(
jwt.Credentials,
google.auth._credentials_async.Signing,
google.auth._credentials_async.Credentials,
jwt.Credentials, _credentials_async.Signing, _credentials_async.Credentials
):
"""Credentials that use a JWT as the bearer token.
Expand Down Expand Up @@ -146,9 +144,7 @@ class Credentials(


class OnDemandCredentials(
jwt.OnDemandCredentials,
google.auth._credentials_async.Signing,
google.auth._credentials_async.Credentials,
jwt.OnDemandCredentials, _credentials_async.Signing, _credentials_async.Credentials
):
"""On-demand JWT credentials.
Expand Down
8 changes: 4 additions & 4 deletions google/auth/_oauth2client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import google.oauth2.service_account

try:
import oauth2client.client
import oauth2client.contrib.gce
import oauth2client.service_account
import oauth2client.client # type: ignore
import oauth2client.contrib.gce # type: ignore
import oauth2client.service_account # type: ignore
except ImportError as caught_exc:
six.raise_from(ImportError("oauth2client is not installed."), caught_exc)

try:
import oauth2client.contrib.appengine # pytype: disable=import-error
import oauth2client.contrib.appengine # type: ignore

_HAS_APPENGINE = True
except ImportError:
Expand Down
8 changes: 4 additions & 4 deletions google/auth/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

# pytype: disable=import-error
try:
from google.appengine.api import app_identity
from google.appengine.api import app_identity # type: ignore
except ImportError:
app_identity = None
app_identity = None # type: ignore
# pytype: enable=import-error


Expand Down Expand Up @@ -168,12 +168,12 @@ def with_quota_project(self, quota_project_id):
def sign_bytes(self, message):
return self._signer.sign(message)

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer_email(self):
return self.service_account_email

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer(self):
return self._signer
7 changes: 1 addition & 6 deletions google/auth/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@
import posixpath
import re

try:
from urllib.parse import urljoin
# Python 2.7 compatibility
except ImportError: # pragma: NO COVER
from urlparse import urljoin

from six.moves import http_client
from six.moves import urllib
from six.moves.urllib.parse import urljoin

from google.auth import _helpers
from google.auth import environment_vars
Expand Down
2 changes: 1 addition & 1 deletion google/auth/compute_engine/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def refresh(self, request):
self.token = access_token
self.expiry = expiry

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer(self):
return self._signer
Expand Down
2 changes: 1 addition & 1 deletion google/auth/crypt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
try:
from google.auth.crypt import es256
except ImportError: # pragma: NO COVER
es256 = None
es256 = None # type: ignore

if es256 is not None: # pragma: NO COVER
__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion google/auth/crypt/_cryptography_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self, private_key, key_id=None):
self._key = private_key
self._key_id = key_id

@property
@property # type: ignore
@_helpers.copy_docstring(base.Signer)
def key_id(self):
return self._key_id
Expand Down
12 changes: 6 additions & 6 deletions google/auth/crypt/_python_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

from __future__ import absolute_import

from pyasn1.codec.der import decoder
from pyasn1_modules import pem
from pyasn1_modules.rfc2459 import Certificate
from pyasn1_modules.rfc5208 import PrivateKeyInfo
import rsa
from pyasn1.codec.der import decoder # type: ignore
from pyasn1_modules import pem # type: ignore
from pyasn1_modules.rfc2459 import Certificate # type: ignore
from pyasn1_modules.rfc5208 import PrivateKeyInfo # type: ignore
import rsa # type: ignore
import six

from google.auth import _helpers
Expand Down Expand Up @@ -125,7 +125,7 @@ def __init__(self, private_key, key_id=None):
self._key = private_key
self._key_id = key_id

@property
@property # type: ignore
@_helpers.copy_docstring(base.Signer)
def key_id(self):
return self._key_id
Expand Down
4 changes: 2 additions & 2 deletions google/auth/crypt/es256.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""ECDSA (ES256) verifier and signer that use the ``cryptography`` library.
"""

from cryptography import utils
from cryptography import utils # type: ignore
import cryptography.exceptions
from cryptography.hazmat import backends
from cryptography.hazmat.primitives import hashes
Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(self, private_key, key_id=None):
self._key = private_key
self._key_id = key_id

@property
@property # type: ignore
@_helpers.copy_docstring(base.Signer)
def key_id(self):
return self._key_id
Expand Down
4 changes: 2 additions & 2 deletions google/auth/crypt/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# unavailable.
from google.auth.crypt import _python_rsa

RSASigner = _python_rsa.RSASigner
RSAVerifier = _python_rsa.RSAVerifier
RSASigner = _python_rsa.RSASigner # type: ignore
RSAVerifier = _python_rsa.RSAVerifier # type: ignore
14 changes: 7 additions & 7 deletions google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from collections.abc import Mapping
# Python 2.7 compatibility
except ImportError: # pragma: NO COVER
from collections import Mapping
from collections import Mapping # type: ignore
import copy
import datetime
import json
Expand All @@ -62,15 +62,15 @@
try:
from google.auth.crypt import es256
except ImportError: # pragma: NO COVER
es256 = None
es256 = None # type: ignore

_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
_DEFAULT_MAX_CACHE_SIZE = 10
_ALGORITHM_TO_VERIFIER_CLASS = {"RS256": crypt.RSAVerifier}
_CRYPTOGRAPHY_BASED_ALGORITHMS = frozenset(["ES256"])

if es256 is not None: # pragma: NO COVER
_ALGORITHM_TO_VERIFIER_CLASS["ES256"] = es256.ES256Verifier
_ALGORITHM_TO_VERIFIER_CLASS["ES256"] = es256.ES256Verifier # type: ignore


def encode(signer, payload, header=None, key_id=None):
Expand Down Expand Up @@ -557,12 +557,12 @@ def refresh(self, request):
def sign_bytes(self, message):
return self._signer.sign(message)

@property
@property # type: ignore
@_helpers.copy_docstring(google.auth.credentials.Signing)
def signer_email(self):
return self._issuer

@property
@property # type: ignore
@_helpers.copy_docstring(google.auth.credentials.Signing)
def signer(self):
return self._signer
Expand Down Expand Up @@ -846,12 +846,12 @@ def before_request(self, request, method, url, headers):
def sign_bytes(self, message):
return self._signer.sign(message)

@property
@property # type: ignore
@_helpers.copy_docstring(google.auth.credentials.Signing)
def signer_email(self):
return self._issuer

@property
@property # type: ignore
@_helpers.copy_docstring(google.auth.credentials.Signing)
def signer(self):
return self._signer
2 changes: 2 additions & 0 deletions google/auth/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# The google-auth package uses inline types.
4 changes: 2 additions & 2 deletions google/auth/transport/_aiohttp_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import asyncio
import functools

import aiohttp
import aiohttp # type: ignore
import six
import urllib3
import urllib3 # type: ignore

from google.auth import exceptions
from google.auth import transport
Expand Down
2 changes: 1 addition & 1 deletion google/auth/transport/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from google.oauth2 import service_account

try:
import grpc
import grpc # type: ignore
except ImportError as caught_exc: # pragma: NO COVER
six.raise_from(
ImportError(
Expand Down
2 changes: 1 addition & 1 deletion google/auth/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class _MutualTlsAdapter(requests.adapters.HTTPAdapter):
def __init__(self, cert, key):
import certifi
from OpenSSL import crypto
import urllib3.contrib.pyopenssl
import urllib3.contrib.pyopenssl # type: ignore

urllib3.contrib.pyopenssl.inject_into_urllib3()

Expand Down
13 changes: 6 additions & 7 deletions google/auth/transport/urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@
try:
import certifi
except ImportError: # pragma: NO COVER
certifi = None
certifi = None # type: ignore

import six

try:
import urllib3
import urllib3 # type: ignore
import urllib3.exceptions # type: ignore
except ImportError as caught_exc: # pragma: NO COVER
import six

six.raise_from(
ImportError(
"The urllib3 library is not installed, please install the "
"urllib3 package to use the urllib3 transport."
),
caught_exc,
)
import six
import urllib3.exceptions # pylint: disable=ungrouped-imports

from google.auth import environment_vars
from google.auth import exceptions
Expand Down Expand Up @@ -169,7 +168,7 @@ def _make_mutual_tls_http(cert, key):
"""
import certifi
from OpenSSL import crypto
import urllib3.contrib.pyopenssl
import urllib3.contrib.pyopenssl # type: ignore

urllib3.contrib.pyopenssl.inject_into_urllib3()
ctx = urllib3.util.ssl_.create_urllib3_context()
Expand Down
6 changes: 3 additions & 3 deletions google/oauth2/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def is_locally_eligible(self):
@_helpers.copy_docstring(ReauthChallenge)
def obtain_challenge_input(self, metadata):
try:
import pyu2f.convenience.authenticator
import pyu2f.errors
import pyu2f.model
import pyu2f.convenience.authenticator # type: ignore
import pyu2f.errors # type: ignore
import pyu2f.model # type: ignore
except ImportError:
raise exceptions.ReauthFailError(
"pyu2f dependency is required to use Security key reauth feature. "
Expand Down
2 changes: 2 additions & 0 deletions google/oauth2/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# The google-oauth2 package uses inline types.
8 changes: 4 additions & 4 deletions google/oauth2/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ def _create_self_signed_jwt(self, audience):
def sign_bytes(self, message):
return self._signer.sign(message)

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer(self):
return self._signer

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer_email(self):
return self._service_account_email
Expand Down Expand Up @@ -676,12 +676,12 @@ def service_account_email(self):
def sign_bytes(self, message):
return self._signer.sign(message)

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer(self):
return self._signer

@property
@property # type: ignore
@_helpers.copy_docstring(credentials.Signing)
def signer_email(self):
return self._service_account_email
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
python_version = 3.6
namespace_packages = True
18 changes: 18 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ def blacken(session):
session.run("black", *BLACK_PATHS)


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


@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
def unit(session):
constraints_path = str(
Expand Down
2 changes: 1 addition & 1 deletion tests/compute_engine/test__metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os

import mock
import pytest
import pytest # type: ignore
from six.moves import http_client
from six.moves import reload_module

Expand Down
4 changes: 2 additions & 2 deletions tests/compute_engine/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import datetime

import mock
import pytest
import responses
import pytest # type: ignore
import responses # type: ignore

from google.auth import _helpers
from google.auth import exceptions
Expand Down
Loading

0 comments on commit c993504

Please # to comment.