From 6398bbcaf8a9d845a4b3d06cfc673a633851f48b Mon Sep 17 00:00:00 2001 From: Christopher Wilcox Date: Thu, 14 Oct 2021 14:40:02 -0700 Subject: [PATCH] fix: improve type hints, mypy check (#242) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: ensure mypy passes * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: flake8 doesn't like lambdas * test: add mypy test scenarioa * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: scope mypy to google.cloud.datastore for now * chore: remove api_core ignores, quiet a mypy error by import rename * fix: drop unneded extra function Also, have shim 'make_datastore_api' raise an exception, as it only gets called if gRPC is disabled, or not installed. Co-authored-by: Owl Bot Co-authored-by: Tres Seaver --- google/cloud/datastore/_http.py | 4 ++-- google/cloud/datastore/client.py | 17 ++++++++-------- mypy.ini | 7 +++++++ noxfile.py | 10 ++++++++++ owlbot.py | 33 ++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 mypy.ini diff --git a/google/cloud/datastore/_http.py b/google/cloud/datastore/_http.py index 9e13567b..9ea5aac8 100644 --- a/google/cloud/datastore/_http.py +++ b/google/cloud/datastore/_http.py @@ -14,10 +14,10 @@ """Connections to Google Cloud Datastore API servers.""" -from google.rpc import status_pb2 +from google.rpc import status_pb2 # type: ignore from google.cloud import _http as connection_module -from google.cloud import exceptions +from google.cloud import exceptions # type: ignore from google.cloud.datastore_v1.types import datastore as _datastore_pb2 diff --git a/google/cloud/datastore/client.py b/google/cloud/datastore/client.py index b5de0fab..4793e059 100644 --- a/google/cloud/datastore/client.py +++ b/google/cloud/datastore/client.py @@ -17,10 +17,10 @@ import warnings import google.api_core.client_options -from google.auth.credentials import AnonymousCredentials -from google.cloud._helpers import _LocalStack -from google.cloud._helpers import _determine_default_project as _base_default_project -from google.cloud.client import ClientWithProject +from google.auth.credentials import AnonymousCredentials # type: ignore +from google.cloud._helpers import _LocalStack # type: ignore +from google.cloud._helpers import _determine_default_project as _base_default_project # type: ignore +from google.cloud.client import ClientWithProject # type: ignore from google.cloud.datastore.version import __version__ from google.cloud.datastore import helpers from google.cloud.datastore._http import HTTPDatastoreAPI @@ -32,13 +32,14 @@ try: from google.cloud.datastore._gapic import make_datastore_api - except ImportError: # pragma: NO COVER - from google.api_core import client_info + from google.api_core import client_info as api_core_client_info + + def make_datastore_api(client): + raise RuntimeError("No gRPC available") - make_datastore_api = None _HAVE_GRPC = False - _CLIENT_INFO = client_info.ClientInfo(client_library_version=__version__) + _CLIENT_INFO = api_core_client_info.ClientInfo(client_library_version=__version__) else: from google.api_core.gapic_v1 import client_info 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 1ca31940..4eeac549 100644 --- a/noxfile.py +++ b/noxfile.py @@ -37,6 +37,7 @@ nox.options.sessions = [ "unit", "system", + "mypy", "cover", "lint", "lint_setup_py", @@ -72,6 +73,15 @@ def blacken(session): ) +@nox.session(python=DEFAULT_PYTHON_VERSION) +def mypy(session): + """Verify type hints are mypy compatible.""" + session.install("-e", ".") + session.install("mypy") + # TODO: also verify types on tests, all of google package + session.run("mypy", "-p", "google.cloud.datastore", "--no-incremental") + + @nox.session(python=DEFAULT_PYTHON_VERSION) def lint_setup_py(session): """Verify that setup.py is valid (including RST check).""" diff --git a/owlbot.py b/owlbot.py index abbbb99a..0ad059b7 100644 --- a/owlbot.py +++ b/owlbot.py @@ -200,4 +200,37 @@ def docfx(session): """, ) +# add type checker nox session +s.replace("noxfile.py", + """nox.options.sessions = \[ + "unit", + "system",""", + """nox.options.sessions = [ + "unit", + "system", + "mypy",""", +) + + +s.replace( + "noxfile.py", + """\ +@nox.session\(python=DEFAULT_PYTHON_VERSION\) +def lint_setup_py\(session\): +""", + '''\ +@nox.session(python=DEFAULT_PYTHON_VERSION) +def mypy(session): + """Verify type hints are mypy compatible.""" + session.install("-e", ".") + session.install("mypy") + # TODO: also verify types on tests, all of google package + session.run("mypy", "-p", "google.cloud.datastore", "--no-incremental") + + +@nox.session(python=DEFAULT_PYTHON_VERSION) +def lint_setup_py(session): +''', +) + s.shell.run(["nox", "-s", "blacken"], hide_output=False)