Skip to content

Commit

Permalink
Enable static type checking with pytype (#298)
Browse files Browse the repository at this point in the history
* Ignore pytype import errors in google/auth.

These errors are raised because the packages are not required, i.e. not
listed in setup.py. We can't guarantee they'll be installed or that
their pyi files exist in github.com/python/typeshed, so silence
potential errors instead.

This does impact the accuracy of type checking.

* Ignore pytype import errors in transport/.

These imports are not listed in setup.py, because they are optional --
it is assumed the user has them installed if needed. Disabling
import-error for these imports prevents useless errors from pytype.

* Ignore various type errors raised by pytype.

- `__init__.py`: pytype is not aware of `__path__`.
- jwt.py: the pyi file for urllib.unparse is not aware of None. Empty
strings are clearer.

* Add pytype disable comments for scripts/

oauth2client isn't listed as a requirement, so users may not have it
installed.

* Fix lint errors from pytype directives.

* Enable pytype -V3.6.

A few notes:
- Previous commits fixed type errors detected by pytype.
- setup.cfg disables pytype's `pyi-error`. This is necessary due to
incomplete type stubs in https://github.com/python/typeshed.
- This only enables pytype's Python3.6 checks. Python2.7 is supported by
pytype but incomplete type stubs cause spurious type errors.

* Remove pytype directives.

Updates to pytype made these directives unnecessary.

* Move pytype install command.

* Add pytype to tox.

* Remove pytype directives for tox-installed imports.

These imports are handled by setup.py and tox.ini, so they'll be
available when pytype is run under tox.

* Fix lint error.
  • Loading branch information
Solumin authored and theacodes committed Oct 5, 2018
1 parent f102825 commit a10b15e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ tests/data/user-key.json
# Generated files
pylintrc
pylintrc.test
pytype_output/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ matrix:
env: TOXENV=py36-system SYSTEM_TEST=1 SKIP_APP_ENGINE_SYSTEM_TEST=1
- python: 2.7
env: TOXENV=py27-system SYSTEM_TEST=1 SKIP_APP_ENGINE_SYSTEM_TEST=1
- python: 3.6
env: TOXENV=pytype
cache:
directories:
- ${HOME}/.cache
Expand Down
3 changes: 2 additions & 1 deletion google/auth/_oauth2client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from google.auth import _helpers
import google.auth.app_engine
import google.auth.compute_engine
import google.oauth2.credentials
import google.oauth2.service_account

Expand All @@ -37,7 +38,7 @@
ImportError('oauth2client is not installed.'), caught_exc)

try:
import oauth2client.contrib.appengine
import oauth2client.contrib.appengine # pytype: disable=import-error
_HAS_APPENGINE = True
except ImportError:
_HAS_APPENGINE = False
Expand Down
2 changes: 2 additions & 0 deletions google/auth/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
from google.auth import credentials
from google.auth import crypt

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


class Signer(crypt.Signer):
Expand Down
2 changes: 1 addition & 1 deletion google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def before_request(self, request, method, url, headers):
parts = urllib.parse.urlsplit(url)
# Strip query string and fragment
audience = urllib.parse.urlunsplit(
(parts.scheme, parts.netloc, parts.path, None, None))
(parts.scheme, parts.netloc, parts.path, "", ""))
token = self._get_jwt_for_audience(audience)
self.apply(headers, token=token)

Expand Down
15 changes: 15 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
[bdist_wheel]
universal = 1

[pytype]
# Where to start analysis.
inputs = .
# Some files aren't worth analyzing, namely tests. Hidden directories (e.g.
# .tox) are automatically filtered out.
exclude = tests system_tests
# All pytype output goes here.
output = pytype_output
# Python version (major.minor) of the target code.
python_version = 3.6
# Paths to source code directories, separated by ':'.
pythonpath = .
# Errors to disable.
disable = pyi-error
10 changes: 9 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = lint,py27,py34,py35,py36,pypy,cover
envlist = lint,py27,py34,py35,py36,pypy,cover,pytype

[testenv]
deps =
Expand Down Expand Up @@ -82,3 +82,11 @@ deps =
flake8
flake8-import-order
docutils

[testenv:pytype]
basepython = python3.6
commands =
pytype
deps =
{[testenv]deps}
pytype

0 comments on commit a10b15e

Please sign in to comment.