summaryrefslogtreecommitdiff
path: root/buildscripts/linter
diff options
context:
space:
mode:
authorJonathan Abrahams <jonathan@mongodb.com>2018-03-27 14:30:46 -0400
committerJonathan Abrahams <jonathan@mongodb.com>2018-04-05 14:41:58 -0400
commitc50c68fef179d9306f1a3432f48985bf20555e38 (patch)
treea1c208329a090c54a8a1f02558b2be87b830a8ab /buildscripts/linter
parenta5dacf7092f51055dd774a1911a48815bb9a1e0e (diff)
downloadmongo-c50c68fef179d9306f1a3432f48985bf20555e38.tar.gz
SERVER-23312 Python linting - Lint using pylint, pydocstyle & mypy
Diffstat (limited to 'buildscripts/linter')
-rw-r--r--buildscripts/linter/base.py6
-rw-r--r--buildscripts/linter/git.py6
-rw-r--r--buildscripts/linter/mypy.py17
-rw-r--r--buildscripts/linter/pydocstyle.py2
-rw-r--r--buildscripts/linter/pylint.py12
-rw-r--r--buildscripts/linter/runner.py6
6 files changed, 20 insertions, 29 deletions
diff --git a/buildscripts/linter/base.py b/buildscripts/linter/base.py
index ae78d520664..f22f59e4f01 100644
--- a/buildscripts/linter/base.py
+++ b/buildscripts/linter/base.py
@@ -11,16 +11,18 @@ class LinterBase(object):
__metaclass__ = ABCMeta
- def __init__(self, cmd_name, required_version):
- # type: (str, str) -> None
+ def __init__(self, cmd_name, required_version, cmd_location=None):
+ # type: (str, str, Optional[str]) -> None
"""
Create a linter.
cmd_name - short friendly name
required_version - the required version string to check against
+ cmd_location - location of executable
"""
self.cmd_name = cmd_name
self.required_version = required_version
+ self.cmd_location = cmd_location
@abstractmethod
def get_lint_cmd_args(self, file_name):
diff --git a/buildscripts/linter/git.py b/buildscripts/linter/git.py
index edde6d0a494..b4a68986040 100644
--- a/buildscripts/linter/git.py
+++ b/buildscripts/linter/git.py
@@ -69,7 +69,7 @@ class Repo(_git.Repository):
Returns the full path to the file for clang-format to consume.
"""
- if candidates is not None and len(candidates) > 0:
+ if candidates is not None and len(candidates) > 0: # pylint: disable=len-as-condition
candidates = [self._get_local_dir(f) for f in candidates]
valid_files = list(
set(candidates).intersection(self.get_candidate_files(filter_function)))
@@ -150,7 +150,7 @@ def get_files_to_check(files, filter_function):
candidates_nested = [expand_file_string(f) for f in files]
candidates = list(itertools.chain.from_iterable(candidates_nested))
- if len(files) > 0 and len(candidates) == 0:
+ if files and not candidates:
raise ValueError("Globs '%s' did not find any files with glob." % (files))
repos = get_repos()
@@ -159,7 +159,7 @@ def get_files_to_check(files, filter_function):
itertools.chain.from_iterable(
[r.get_candidates(candidates, filter_function) for r in repos]))
- if len(files) > 0 and len(valid_files) == 0:
+ if files and not valid_files:
raise ValueError("Globs '%s' did not find any files with glob in git." % (files))
return valid_files
diff --git a/buildscripts/linter/mypy.py b/buildscripts/linter/mypy.py
index 9526b388c75..c720ae8f870 100644
--- a/buildscripts/linter/mypy.py
+++ b/buildscripts/linter/mypy.py
@@ -2,6 +2,7 @@
from __future__ import absolute_import
from __future__ import print_function
+import os
from typing import List
from . import base
@@ -13,7 +14,9 @@ class MypyLinter(base.LinterBase):
def __init__(self):
# type: () -> None
"""Create a mypy linter."""
- super(MypyLinter, self).__init__("mypy", "mypy 0.501")
+ # User can override the location of mypy from an environment variable.
+
+ super(MypyLinter, self).__init__("mypy", "mypy 0.580", os.getenv("MYPY"))
def get_lint_version_cmd_args(self):
# type: () -> List[str]
@@ -23,17 +26,7 @@ class MypyLinter(base.LinterBase):
def get_lint_cmd_args(self, file_name):
# type: (str) -> List[str]
"""Get the command to run a linter."""
- # -py2 - Check Python 2 code for type annotations in comments
- # --disallow-untyped-defs - Error if any code is missing type annotations
- # --ignore-missing-imports - Do not error if imports are not found. This can be a problem
- # with standalone scripts and relative imports. This will limit effectiveness but avoids
- # mypy complaining about running code.
- # --follow-imports=silent - Do not error on imported files since all imported files may not
- # be mypy clean
- return [
- "--py2", "--disallow-untyped-defs", "--ignore-missing-imports",
- "--follow-imports=silent", file_name
- ]
+ return [file_name]
def ignore_interpreter(self):
# type: () -> bool
diff --git a/buildscripts/linter/pydocstyle.py b/buildscripts/linter/pydocstyle.py
index 9420d42354c..b259becfd1c 100644
--- a/buildscripts/linter/pydocstyle.py
+++ b/buildscripts/linter/pydocstyle.py
@@ -13,7 +13,7 @@ class PyDocstyleLinter(base.LinterBase):
def __init__(self):
# type: () -> None
"""Create a pydocstyle linter."""
- super(PyDocstyleLinter, self).__init__("pydocstyle", "1.1.1")
+ super(PyDocstyleLinter, self).__init__("pydocstyle", "2.1.1")
def get_lint_version_cmd_args(self):
# type: () -> List[str]
diff --git a/buildscripts/linter/pylint.py b/buildscripts/linter/pylint.py
index 056465501d8..12dbcfdf6d3 100644
--- a/buildscripts/linter/pylint.py
+++ b/buildscripts/linter/pylint.py
@@ -15,9 +15,7 @@ class PyLintLinter(base.LinterBase):
def __init__(self):
# type: () -> None
"""Create a pylint linter."""
- self._rc_file = os.path.join(
- os.path.normpath(git.get_base_dir()), "buildscripts", ".pylintrc")
- super(PyLintLinter, self).__init__("pylint", "pylint 1.6.5")
+ super(PyLintLinter, self).__init__("pylint", "pylint 1.8.3")
def get_lint_version_cmd_args(self):
# type: () -> List[str]
@@ -27,10 +25,4 @@ class PyLintLinter(base.LinterBase):
def get_lint_cmd_args(self, file_name):
# type: (str) -> List[str]
"""Get the command to run a linter."""
- # pylintrc only searches parent directories if it is a part of a module, and since our code
- # is split across different modules, and individual script files, we need to specify the
- # path to the rcfile.
- # See https://pylint.readthedocs.io/en/latest/user_guide/run.html
- return [
- "--rcfile=%s" % (self._rc_file), "--output-format", "msvs", "--reports=n", file_name
- ]
+ return ["--output-format=msvs", "--reports=n", file_name]
diff --git a/buildscripts/linter/runner.py b/buildscripts/linter/runner.py
index f3ff287ee95..67c69d25d02 100644
--- a/buildscripts/linter/runner.py
+++ b/buildscripts/linter/runner.py
@@ -88,7 +88,11 @@ def _find_linter(linter, config_dict):
if linter.ignore_interpreter():
# Some linters use a different interpreter then the current interpreter.
- cmd_str = os.path.join('/opt/mongodbtoolchain/v2/bin', linter.cmd_name)
+ # If the linter cmd_location is specified then use that location.
+ if linter.cmd_location:
+ cmd_str = linter.cmd_location
+ else:
+ cmd_str = os.path.join('/opt/mongodbtoolchain/v2/bin', linter.cmd_name)
cmd = [cmd_str]
else:
cmd = [sys.executable, cmd_str]