Skip to content

Commit

Permalink
fix: improve type hints, mypy check (#242)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Tres Seaver <tseaver@palladion.com>
  • Loading branch information
3 people committed Oct 14, 2021
1 parent dbe20e3 commit 6398bbc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
4 changes: 2 additions & 2 deletions google/cloud/datastore/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
17 changes: 9 additions & 8 deletions google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[mypy]
python_version = 3.6
namespace_packages = True
ignore_missing_imports = True

[mypy-google.protobuf]
ignore_missing_imports = True
10 changes: 10 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
nox.options.sessions = [
"unit",
"system",
"mypy",
"cover",
"lint",
"lint_setup_py",
Expand Down Expand Up @@ -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)."""
Expand Down
33 changes: 33 additions & 0 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6398bbc

Please sign in to comment.