Skip to content

Commit

Permalink
Fix handling of ignore= / flycheck-pycheckers-ignore-codes
Browse files Browse the repository at this point in the history
This allows us to differentiate between "ignore no codes (enable all
warnings/errors)" vs. "don't specify anything for the `ignore` option, use
config files instead."

Fixes #12
  • Loading branch information
msherry committed Feb 23, 2018
1 parent 41e6769 commit 0ff6d59
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
57 changes: 33 additions & 24 deletions bin/pycheckers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# pylint: disable=unused-import, ungrouped-imports
from argparse import Namespace
from typing import (
Any, Dict, List, IO, Optional, Set, Tuple)
Any, Dict, List, IO, Iterable, Optional, Set, Tuple)
except ImportError:
pass

Expand Down Expand Up @@ -98,8 +98,8 @@ class LintRunner(object):

def __init__(self, ignore_codes, enable_codes, options):
# type: (Tuple[str], Tuple[str], Namespace) -> None
self.ignore_codes = set(ignore_codes)
self.enable_codes = set(enable_codes)
self.ignore_codes = set(ignore_codes) if ignore_codes is not None else None
self.enable_codes = set(enable_codes) if enable_codes is not None else None
self.options = options

@property
Expand All @@ -114,7 +114,7 @@ def name(self):
return self.command

def get_run_flags(self, _filename):
# type: (str) -> Tuple[str, ...]
# type: (str) -> Iterable[str]
return ()

def get_env_vars(self):
Expand All @@ -128,9 +128,7 @@ def fixup_data(self, _line, data):
def process_output(self, line):
# type: (str) -> Optional[Dict[str, str]]
m = self.output_matcher.match(line)
if m:
return m.groupdict()
return None
return m.groupdict() if m else None

def process_returncode(self, _returncode):
# type: (int) -> bool
Expand Down Expand Up @@ -296,13 +294,18 @@ def fixup_data(cls, _line, data):
return data

def get_run_flags(self, _filename):
# type: (str) -> Tuple[str, ...]
return (
'--ignore=' + ','.join(self.ignore_codes),
# type: (str) -> Iterable[str]
args = []
if self.ignore_codes is not None:
# We're explicitly ignoring something, even if that something is
# nothing (i.e. `--ignore=`, meaning ignore nothing)
args.append('--ignore=' + ','.join(self.ignore_codes))
args += [
# TODO: --select, but additive
# '-select=' + ','.join(self.enable_codes),
'--max-line-length', str(self.options.max_line_length),
)
]
return args


class Pep8Runner(LintRunner):
Expand Down Expand Up @@ -332,14 +335,17 @@ def fixup_data(cls, _line, data):
return data

def get_run_flags(self, _filename):
# type: (str) -> Tuple[str, ...]
return (
# type: (str) -> Iterable[str]
args = []
if self.ignore_codes is not None:
args.append('--ignore=' + ','.join(self.ignore_codes))
args += [
'--repeat',
'--ignore=' + ','.join(self.ignore_codes),
# TODO: make this additive, not a replacement
# '--select=' + ','.join(self.enable_codes),
'--max-line-length', str(self.options.max_line_length),
)
]
return args


class PylintRunner(LintRunner):
Expand Down Expand Up @@ -375,18 +381,21 @@ def fixup_data(cls, _line, data):
return data

def get_run_flags(self, _filename):
# type: (str) -> Tuple[str, ...]
return (
# type: (str) -> Iterable[str]
args = []
if self.ignore_codes is not None:
args.append('--disable=' + ','.join(self.ignore_codes))
args += [
'--msg-template', ('{path}:{line}:{column}: '
'[{msg_id}({symbol})] {msg}'),
'--reports', 'n',
'--disable=' + ','.join(self.ignore_codes),
# This is additive, not replacing
'--enable=' + ','.join(self.enable_codes),
'--dummy-variables-rgx=' + '_.*',
'--max-line-length', str(self.options.max_line_length),
'--rcfile', self.options.pylint_rcfile,
)
]
return args

def get_env_vars(self):
# type: () -> Dict[str, str]
Expand Down Expand Up @@ -440,7 +449,7 @@ def _get_cache_dir(self, filename):
return cache_dir

def get_run_flags(self, filename):
# type: (str) -> Tuple[str, ...]
# type: (str) -> Iterable[str]
"""Determine which mypy (2 or 3) to run, find the cache dir and config file"""
flags = [
'--cache-dir={}'.format(self._get_cache_dir(filename)),
Expand All @@ -457,7 +466,7 @@ def get_run_flags(self, filename):
if self.name == 'mypy':
# mypy2 mode
flags += ['--py2']
return tuple(flags)
return flags

def fixup_data(self, _line, data):
# type: (str, Dict[str, str]) -> Dict[str, str]
Expand Down Expand Up @@ -673,7 +682,6 @@ def parse_args():
default=default_checkers,
help="Comma-separated list of checkers")
parser.add_argument("-i", "--ignore-codes", dest="ignore_codes",
default='',
help="Comma-separated list of error codes to ignore")
parser.add_argument("-e", "--enable-codes", dest="enable_codes",
default='',
Expand Down Expand Up @@ -723,8 +731,9 @@ def main():
options = update_options_locally(options)

checkers = options.checkers
ignore_codes = tuple(c for c in options.ignore_codes.split(",") if c)
enable_codes = tuple(c for c in options.enable_codes.split(",") if c)
ignore_codes = (tuple(c.strip() for c in options.ignore_codes.split(",") if c)
if options.ignore_codes is not None else None)
enable_codes = tuple(c.strip() for c in options.enable_codes.split(",") if c)
set_path_for_virtualenv(source_file, options.venv_root)

checker_names = [checker.strip() for checker in checkers.split(',')]
Expand Down
12 changes: 10 additions & 2 deletions flycheck-pycheckers.el
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,16 @@
python-pycheckers
"A list of error codes to ignore.
A nil value means that this option will not be used at all, and
instead, ignored error codes will come from any config files, if
found. An *empty* value means that no codes will be ignored --
i.e., config file options will not be used, and all errors will
be reported.
Can be further customized via the \".pycheckers\" config file."
:type '(repeat :tag "Codes" (string :tag "Error/Warning code")))
:type '(radio :tag "Ignored errors"
(repeat :tag "Codes (overrides config files)" (string :tag "Error/Warning code"))
(const :tag "Use errors from found config files" unset)))

(define-obsolete-variable-alias 'flycheck-pycheckers-enabled-codes 'flycheck-pycheckers-enable-codes)
(flycheck-def-option-var flycheck-pycheckers-enable-codes
Expand Down Expand Up @@ -237,7 +245,7 @@ per-directory."

:command `(,flycheck-pycheckers-command
(eval flycheck-pycheckers-args)
"-i" (eval (mapconcat 'identity flycheck-pycheckers-ignore-codes ","))
(eval (if (not (eq 'unset flycheck-pycheckers-ignore-codes)) (concat "-i " (mapconcat 'identity flycheck-pycheckers-ignore-codes ","))))
"-e" (eval (mapconcat 'identity flycheck-pycheckers-enable-codes ","))
"--checkers" (eval (mapconcat #'symbol-name flycheck-pycheckers-checkers ","))
"--max-line-length" (eval (number-to-string flycheck-pycheckers-max-line-length))
Expand Down

0 comments on commit 0ff6d59

Please sign in to comment.