From 5e894faccf0eefae6f00a1841d41bb51d95ab1d1 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 13 Oct 2021 16:19:11 -0400 Subject: [PATCH 1/3] ci: add mypy checking --- google/__init__.py | 2 +- google/api_core/client_info.py | 3 +++ google/api_core/exceptions.py | 8 +++++--- google/api_core/py.typed | 2 ++ mypy.ini | 7 +++++++ noxfile.py | 9 +++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 google/api_core/py.typed create mode 100644 mypy.ini diff --git a/google/__init__.py b/google/__init__.py index 0d0a4c3a..70a7bd99 100644 --- a/google/__init__.py +++ b/google/__init__.py @@ -21,4 +21,4 @@ except ImportError: import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) + __path__ = pkgutil.extend_path(__path__, __name__) # type: ignore diff --git a/google/api_core/client_info.py b/google/api_core/client_info.py index d7f4367a..e093ffda 100644 --- a/google/api_core/client_info.py +++ b/google/api_core/client_info.py @@ -19,6 +19,7 @@ """ import platform +from typing import Union import pkg_resources @@ -27,6 +28,8 @@ _PY_VERSION = platform.python_version() _API_CORE_VERSION = api_core_version.__version__ +_GRPC_VERSION: Union[str, None] + try: _GRPC_VERSION = pkg_resources.get_distribution("grpcio").version except pkg_resources.DistributionNotFound: # pragma: NO COVER diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index b0909f1e..c7319601 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -20,6 +20,8 @@ from __future__ import absolute_import from __future__ import unicode_literals +from typing import Union +from typing import Dict import http.client @@ -31,8 +33,8 @@ # Lookup tables for mapping exceptions from HTTP and gRPC transports. # Populated by _GoogleAPICallErrorMeta -_HTTP_CODE_TO_EXCEPTION = {} -_GRPC_CODE_TO_EXCEPTION = {} +_HTTP_CODE_TO_EXCEPTION: Dict[int, Exception] = {} +_GRPC_CODE_TO_EXCEPTION: Dict[int, Exception] = {} # Additional lookup table to map integer status codes to grpc status code # grpc does not currently support initializing enums from ints @@ -100,7 +102,7 @@ class GoogleAPICallError(GoogleAPIError, metaclass=_GoogleAPICallErrorMeta): gRPC call metadata. """ - code = None + code: Union[int, None] = None """Optional[int]: The HTTP status code associated with this error. This may be ``None`` if the exception does not have a direct mapping diff --git a/google/api_core/py.typed b/google/api_core/py.typed new file mode 100644 index 00000000..1d5517b1 --- /dev/null +++ b/google/api_core/py.typed @@ -0,0 +1,2 @@ +# Marker file for PEP 561. +# The google-api-core package uses inline types. diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..5663b40d --- /dev/null +++ b/mypy.ini @@ -0,0 +1,7 @@ +[mypy] +python_version = 3.6 +namespace_packages = True +ignore_missing_imports = True + +[mypy-google.protobuf] +ignore_missing_imports = True diff --git a/noxfile.py b/noxfile.py index 617dc580..2ffd15b5 100644 --- a/noxfile.py +++ b/noxfile.py @@ -35,6 +35,7 @@ "unit_grpc_gcp", "cover", "pytype", + "mypy", "lint", "lint_setup_py", "blacken", @@ -155,6 +156,14 @@ def pytype(session): session.run("pytype") +@nox.session(python="3.6") +def mypy(session): + """Run type-checking.""" + session.install(".[grpc, grpcgcp]", "mypy") + session.install("types-setuptools", "types-requests", "types-mock") + session.run("mypy", "google", "tests") + + @nox.session(python="3.6") def cover(session): """Run the final coverage report. From 1c9b709b774e2234ca4eaf5383621ae23ea47221 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 13 Oct 2021 16:36:19 -0400 Subject: [PATCH 2/3] ci: appease flake8 --- google/api_core/exceptions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index c7319601..2cfc2e4a 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -20,14 +20,13 @@ from __future__ import absolute_import from __future__ import unicode_literals -from typing import Union -from typing import Dict import http.client +from typing import Dict +from typing import Union try: import grpc - except ImportError: # pragma: NO COVER grpc = None From b1a6640e3439a60c8f4ec531021a0c3fc56d2692 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 13 Oct 2021 16:47:49 -0400 Subject: [PATCH 3/3] chore: use DEFAULT_PYTHON_VERSION to run mypy --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index 2ffd15b5..926f9f5d 100644 --- a/noxfile.py +++ b/noxfile.py @@ -156,7 +156,7 @@ def pytype(session): session.run("pytype") -@nox.session(python="3.6") +@nox.session(python=DEFAULT_PYTHON_VERSION) def mypy(session): """Run type-checking.""" session.install(".[grpc, grpcgcp]", "mypy")