diff --git a/mypy.ini b/mypy.ini index 7c6adc95eb99d..1b3f206141ebd 100644 --- a/mypy.ini +++ b/mypy.ini @@ -6,7 +6,7 @@ warn_no_return = false no_implicit_optional = true warn_redundant_casts = true warn_unused_ignores = true -plugins = beam_plugin +#plugins = beam_plugin [mypy-apache_beam.runners.dataflow.internal.clients.dataflow.dataflow_v1b3_client] ignore_errors = true diff --git a/sdks/python/setup.py b/sdks/python/setup.py index 3106a21aef01e..b74e8f7c899b7 100644 --- a/sdks/python/setup.py +++ b/sdks/python/setup.py @@ -25,12 +25,16 @@ import sys import warnings from distutils.version import StrictVersion +from distutils.errors import DistutilsError # Pylint and isort disagree here. # pylint: disable=ungrouped-imports import setuptools from pkg_resources import DistributionNotFound from pkg_resources import get_distribution +from pkg_resources import normalize_path +from pkg_resources import to_filename +from setuptools import Command from setuptools.command.build_py import build_py from setuptools.command.develop import develop from setuptools.command.egg_info import egg_info @@ -38,6 +42,54 @@ from setuptools.command.test import test +class mypy(Command): + user_options = [] + + def initialize_options(self): + """Abstract method that is required to be overwritten""" + + def finalize_options(self): + """Abstract method that is required to be overwritten""" + + def get_project_path(self, include_dists=[]): + # with_2to3 = six.PY3 and getattr(self.distribution, 'use_2to3', False) + # + # if with_2to3: + # # If we run 2to3 we can not do this inplace: + # + # # Ensure metadata is up-to-date + # self.reinitialize_command('build_py', inplace=0) + # self.run_command('build_py') + # bpy_cmd = self.get_finalized_command("build_py") + # build_path = normalize_path(bpy_cmd.build_lib) + # + # # Build extensions + # self.reinitialize_command('egg_info', egg_base=build_path) + # self.run_command('egg_info') + # + # self.reinitialize_command('build_ext', inplace=0) + # self.run_command('build_ext') + # else: + # Without 2to3 inplace works fine: + self.run_command('egg_info') + + # Build extensions in-place + self.reinitialize_command('build_ext', inplace=1) + self.run_command('build_ext') + + ei_cmd = self.get_finalized_command("egg_info") + + project_path = normalize_path(ei_cmd.egg_base) + return os.path.join(project_path, to_filename(ei_cmd.egg_name)) + + def run(self): + import subprocess + args = ['mypy', self.get_project_path()] + result = subprocess.call(args) + if result != 0: + raise DistutilsError() + + def get_version(): global_names = {} exec( # pylint: disable=exec-used @@ -236,5 +288,6 @@ def run(self): 'egg_info': generate_protos_first(egg_info), 'sdist': generate_protos_first(sdist), 'test': generate_protos_first(test), + 'mypy': generate_protos_first(mypy), }, ) diff --git a/sdks/python/tox.ini b/sdks/python/tox.ini index 2a221b0c5efee..246c8ee1e3d6b 100644 --- a/sdks/python/tox.ini +++ b/sdks/python/tox.ini @@ -17,7 +17,7 @@ [tox] # new environments will be excluded by default unless explicitly added to envlist. -envlist = py27,py35,py36,py37,py27-{gcp,cython,lint,lint3},py35-{gcp,cython,lint},py36-{gcp,cython},py37-{gcp,cython},docs +envlist = py27,py35,py36,py37,py27-{gcp,cython,lint,lint3},py35-{gcp,cython,lint},py36-{gcp,cython},py37-{gcp,cython,mypy},docs toxworkdir = {toxinidir}/target/{env:ENV_NAME:.tox} [pycodestyle] @@ -172,7 +172,6 @@ commands = pylint --version time {toxinidir}/scripts/run_pylint_2to3.sh - [testenv:py35-lint] deps = pycodestyle==2.3.1 @@ -184,6 +183,13 @@ commands = pylint --version time {toxinidir}/scripts/run_mini_py3lint.sh +[testenv:py37-mypy] +deps = + mypy==0.711 +commands = + mypy --version + python setup.py mypy + [testenv:docs] extras = test,gcp,docs deps =