Skip to content

Commit a10b15e

Browse files
Solumintheacodes
authored andcommitted
Enable static type checking with pytype (#298)
* 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.
1 parent f102825 commit a10b15e

File tree

7 files changed

+32
-3
lines changed

7 files changed

+32
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ tests/data/user-key.json
3535
# Generated files
3636
pylintrc
3737
pylintrc.test
38+
pytype_output/

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ matrix:
2222
env: TOXENV=py36-system SYSTEM_TEST=1 SKIP_APP_ENGINE_SYSTEM_TEST=1
2323
- python: 2.7
2424
env: TOXENV=py27-system SYSTEM_TEST=1 SKIP_APP_ENGINE_SYSTEM_TEST=1
25+
- python: 3.6
26+
env: TOXENV=pytype
2527
cache:
2628
directories:
2729
- ${HOME}/.cache

google/auth/_oauth2client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from google.auth import _helpers
2727
import google.auth.app_engine
28+
import google.auth.compute_engine
2829
import google.oauth2.credentials
2930
import google.oauth2.service_account
3031

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

3940
try:
40-
import oauth2client.contrib.appengine
41+
import oauth2client.contrib.appengine # pytype: disable=import-error
4142
_HAS_APPENGINE = True
4243
except ImportError:
4344
_HAS_APPENGINE = False

google/auth/app_engine.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
from google.auth import credentials
2929
from google.auth import crypt
3030

31+
# pytype: disable=import-error
3132
try:
3233
from google.appengine.api import app_identity
3334
except ImportError:
3435
app_identity = None
36+
# pytype: enable=import-error
3537

3638

3739
class Signer(crypt.Signer):

google/auth/jwt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def before_request(self, request, method, url, headers):
738738
parts = urllib.parse.urlsplit(url)
739739
# Strip query string and fragment
740740
audience = urllib.parse.urlunsplit(
741-
(parts.scheme, parts.netloc, parts.path, None, None))
741+
(parts.scheme, parts.netloc, parts.path, "", ""))
742742
token = self._get_jwt_for_audience(audience)
743743
self.apply(headers, token=token)
744744

setup.cfg

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
[bdist_wheel]
22
universal = 1
3+
4+
[pytype]
5+
# Where to start analysis.
6+
inputs = .
7+
# Some files aren't worth analyzing, namely tests. Hidden directories (e.g.
8+
# .tox) are automatically filtered out.
9+
exclude = tests system_tests
10+
# All pytype output goes here.
11+
output = pytype_output
12+
# Python version (major.minor) of the target code.
13+
python_version = 3.6
14+
# Paths to source code directories, separated by ':'.
15+
pythonpath = .
16+
# Errors to disable.
17+
disable = pyi-error

tox.ini

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = lint,py27,py34,py35,py36,pypy,cover
2+
envlist = lint,py27,py34,py35,py36,pypy,cover,pytype
33

44
[testenv]
55
deps =
@@ -82,3 +82,11 @@ deps =
8282
flake8
8383
flake8-import-order
8484
docutils
85+
86+
[testenv:pytype]
87+
basepython = python3.6
88+
commands =
89+
pytype
90+
deps =
91+
{[testenv]deps}
92+
pytype

0 commit comments

Comments
 (0)