Skip to content

Commit

Permalink
fix: add mypy checking + 'py.typed' files (#149)
Browse files Browse the repository at this point in the history
* ci: add mypy checking

* chore: add py.typed file.

* chore: use DEFAULT_PYTHON_VERSION to run mypy

* fix: add typing for 'Client.SCOPE'

* fix: cannot have 'py.typed' in the namespace package dir

* chore: make top-level modules in 'google/core' into packages

FBO typing, which interprets 'py.typed' file as always-inherited in subdirs.

* chore: back out converson of 'version.py' to a package

Our 'release-please' tooling won't find it there, and nobody is likely
to a) import the module,  and then b) choke on its lack of typing.

* chore: fix thinko
  • Loading branch information
tseaver committed Nov 9, 2021
1 parent 35d78d0 commit f20ef60
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .coveragerc
Expand Up @@ -3,9 +3,9 @@ branch = True

[report]
omit =
google/cloud/_testing.py
google/cloud/__init__.py
google/cloud/environment_vars.py
google/cloud/_testing/__init__.py
google/cloud/environment_vars/__init__.py
fail_under = 100
show_missing = True
exclude_lines =
Expand Down
2 changes: 1 addition & 1 deletion google/__init__.py
Expand Up @@ -21,4 +21,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/cloud/__init__.py
Expand Up @@ -21,4 +21,4 @@
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore
Expand Up @@ -25,6 +25,7 @@
import os
import re
from threading import local as Local
from typing import Union

import google.auth
import google.auth.transport.requests
Expand Down Expand Up @@ -62,6 +63,7 @@
)
# NOTE: Catching this ImportError is a workaround for GAE not supporting the
# "pwd" module which is imported lazily when "expanduser" is called.
_USER_ROOT: Union[str, None]
try:
_USER_ROOT = os.path.expanduser("~")
except ImportError: # pragma: NO COVER
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/_helpers/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
File renamed without changes.
2 changes: 2 additions & 0 deletions google/cloud/_http/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
File renamed without changes.
2 changes: 2 additions & 0 deletions google/cloud/_testing/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
4 changes: 3 additions & 1 deletion google/cloud/client.py → google/cloud/client/__init__.py
Expand Up @@ -18,6 +18,8 @@
import json
import os
from pickle import PicklingError
from typing import Tuple
from typing import Union

import google.api_core.client_options
import google.api_core.exceptions
Expand Down Expand Up @@ -142,7 +144,7 @@ class Client(_ClientFactoryMixin):
to acquire default credentials.
"""

SCOPE = None
SCOPE: Union[Tuple[str], None] = None
"""The scopes required for authenticating with a service.
Needs to be set by subclasses.
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/client/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
File renamed without changes.
2 changes: 2 additions & 0 deletions google/cloud/environment_vars/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
File renamed without changes.
2 changes: 2 additions & 0 deletions google/cloud/exceptions/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
File renamed without changes.
2 changes: 2 additions & 0 deletions google/cloud/obsolete/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
Expand Up @@ -14,13 +14,15 @@

"""Wrap long-running operations returned from Google Cloud APIs."""

from typing import Dict

from google.longrunning import operations_pb2
from google.protobuf import json_format


_GOOGLE_APIS_PREFIX = "type.googleapis.com"

_TYPE_URL_MAP = {}
_TYPE_URL_MAP: Dict[str, type] = {}


def _compute_type_url(klass, prefix=_GOOGLE_APIS_PREFIX):
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/operation/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# This package uses inline types.
7 changes: 7 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions noxfile.py
Expand Up @@ -38,6 +38,16 @@ def lint(session):
session.run("flake8", "google", "tests")


@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy(session):
"""Run type-checking."""
session.install(".", "mypy")
session.install(
"types-setuptools", "types-requests", "types-mock", "types-protobuf",
)
session.run("mypy", "google", "tests")


@nox.session(python=DEFAULT_PYTHON_VERSION)
def blacken(session):
"""Run black.
Expand Down

0 comments on commit f20ef60

Please sign in to comment.