Skip to content

Commit

Permalink
Add a mypy lint job
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Oct 27, 2019
1 parent fd87c7f commit c6ec72d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ sdks/java/maven-archetypes/examples-java8/src/main/resources/archetype-resources
**/dist/**/*
**/distribute-*/**/*
**/env/**/*
**/.mypy_cache
**/.dmypy.json
sdks/python/**/*.c
sdks/python/**/*.so
sdks/python/**/*.egg
Expand Down
2 changes: 2 additions & 0 deletions sdks/python/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ disable =
unnecessary-lambda,
unnecessary-pass,
unneeded-not,
unsubscriptable-object,
unused-argument,
unused-wildcard-import,
useless-object-inheritance,
Expand Down Expand Up @@ -178,6 +179,7 @@ indent-after-paren=4
ignore-long-lines=(?x)
(^\s*(import|from)\s
|^\s*(\#\ )?<?(https?|ftp):\/\/[^\s\/$.?#].[^\s]*>?$
|# type:
)

[VARIABLES]
Expand Down
60 changes: 60 additions & 0 deletions sdks/python/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

[mypy]
python_version = 3.6
ignore_missing_imports = true
follow_imports = true
warn_no_return = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
show_error_codes = true
files = apache_beam
color_output = true
# uncomment this to see how close we are to being complete
# check_untyped_defs = true

[mypy-apache_beam.coders.proto2_coder_test_messages_pb2]
ignore_errors = true

[mypy-apache_beam.examples.*]
ignore_errors = true

[mypy-apache_beam.io.gcp.gcsfilesystem_test]
# error: Cannot infer type of lambda [misc]
ignore_errors = true

[mypy-apache_beam.io.gcp.internal.clients.storage.storage_v1_client]
ignore_errors = true

[mypy-apache_beam.io.gcp.internal.clients.bigquery.bigquery_v2_client]
ignore_errors = true

[mypy-apache_beam.portability.api.*]
ignore_errors = true

[mypy-apache_beam.runners.dataflow.internal.clients.dataflow.dataflow_v1b3_client]
ignore_errors = true

[mypy-apache_beam.typehints.typed_pipeline_test_py3]
# error: Signature of "process" incompatible with supertype "DoFn" [override]
ignore_errors = true

[mypy-apache_beam.typehints.typehints_test_py3]
# error: Signature of "process" incompatible with supertype "DoFn" [override]
ignore_errors = true
38 changes: 37 additions & 1 deletion sdks/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,53 @@
import sys
import warnings
from distutils import log
from distutils.errors import DistutilsError
from distutils.version import StrictVersion

# 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
# TODO: (BEAM-8411): re-enable lint check.
from setuptools.command.develop import develop # pylint: disable-all
from setuptools.command.egg_info import egg_info
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):
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
Expand Down Expand Up @@ -130,7 +163,9 @@ def get_version():
'pytz>=2018.3',
# [BEAM-5628] Beam VCF IO is not supported in Python 3.
'pyvcf>=0.6.8,<0.7.0; python_version < "3.0"',
'typing>=3.6.0,<3.7.0; python_version < "3.5.0"',
# fixes and additions have been made since typing 3.5
'typing>=3.7.0,<3.8.0; python_version < "3.8.0"',
'typing-extensions>=3.7.0,<3.8.0; python_version < "3.8.0"',
]

# [BEAM-8181] pyarrow cannot be installed on 32-bit Windows platforms.
Expand Down Expand Up @@ -249,5 +284,6 @@ def run(self):
'develop': generate_protos_first(develop),
'egg_info': generate_protos_first(egg_info),
'test': generate_protos_first(test),
'mypy': generate_protos_first(mypy),
},
)
3 changes: 3 additions & 0 deletions sdks/python/test-suites/tox/py37/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ check.dependsOn lint
toxTask "lintPy37", "py37-lint"
lint.dependsOn lintPy37

toxTask "mypyPy37", "py37-mypy"
lint.dependsOn mypyPy37

toxTask "testPython37", "py37"
test.dependsOn testPython37

Expand Down
9 changes: 8 additions & 1 deletion sdks/python/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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},py36-{gcp,cython},py37-{gcp,cython,lint},docs
envlist = py27,py35,py36,py37,py27-{gcp,cython,lint,lint3},py35-{gcp,cython},py36-{gcp,cython},py37-{gcp,cython,lint,mypy},docs
toxworkdir = {toxinidir}/target/{env:ENV_NAME:.tox}

[pycodestyle]
Expand Down Expand Up @@ -188,6 +188,13 @@ commands =
pylint --version
time {toxinidir}/scripts/run_pylint.sh

[testenv:py37-mypy]
deps =
mypy==0.730
commands =
mypy --version
python setup.py mypy

[testenv:docs]
extras = test,gcp,docs
deps =
Expand Down

0 comments on commit c6ec72d

Please sign in to comment.