mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-30 02:46:52 +00:00
Merge branch 'feature/register-optional-checks' into 'master'
Allow for optional checks See merge request !14
This commit is contained in:
commit
c761d22876
2 changed files with 37 additions and 4 deletions
|
|
@ -19,6 +19,7 @@ def _register_extensions():
|
|||
extensions.add(('pep8', pep8.__version__))
|
||||
parser_hooks = []
|
||||
options_hooks = []
|
||||
ignored_hooks = []
|
||||
try:
|
||||
from pkg_resources import iter_entry_points
|
||||
except ImportError:
|
||||
|
|
@ -32,7 +33,9 @@ def _register_extensions():
|
|||
parser_hooks.append(checker.add_options)
|
||||
if hasattr(checker, 'parse_options'):
|
||||
options_hooks.append(checker.parse_options)
|
||||
return extensions, parser_hooks, options_hooks
|
||||
if getattr(checker, 'off_by_default', False) is True:
|
||||
ignored_hooks.append(entry.name)
|
||||
return extensions, parser_hooks, options_hooks, ignored_hooks
|
||||
|
||||
|
||||
def _install_hook_cb(option, option_str, value, parser):
|
||||
|
|
@ -52,7 +55,7 @@ def get_parser():
|
|||
"""This returns an instance of optparse.OptionParser with all the
|
||||
extensions registered and options set. This wraps ``pep8.get_parser``.
|
||||
"""
|
||||
(extensions, parser_hooks, options_hooks) = _register_extensions()
|
||||
(extensions, parser_hooks, options_hooks, ignored) = _register_extensions()
|
||||
details = ', '.join(['%s: %s' % ext for ext in extensions])
|
||||
python_version = get_python_version()
|
||||
parser = pep8.get_parser('flake8', '%s (%s) %s' % (
|
||||
|
|
@ -79,6 +82,7 @@ def get_parser():
|
|||
help='Install the appropriate hook for this '
|
||||
'repository.', action='callback',
|
||||
callback=_install_hook_cb)
|
||||
parser.ignored_extensions = ignored
|
||||
return parser, options_hooks
|
||||
|
||||
|
||||
|
|
@ -96,6 +100,16 @@ class StyleGuide(pep8.StyleGuide):
|
|||
return fchecker.check_all(expected=expected, line_offset=line_offset)
|
||||
|
||||
|
||||
def _disable_extensions(parser, options):
|
||||
ignored_extensions = set(getattr(parser, 'ignored_extensions', []))
|
||||
# Remove any of the selected extensions from the extensions ignored by
|
||||
# default.
|
||||
ignored_extensions -= set(options.select)
|
||||
# Whatever is left afterwards should be unioned with options.ignore and
|
||||
# options.ignore should be updated with that.
|
||||
options.ignore = tuple(ignored_extensions.union(options.ignore))
|
||||
|
||||
|
||||
def get_style_guide(**kwargs):
|
||||
"""Parse the options and configure the checker. This returns a sub-class
|
||||
of ``pep8.StyleGuide``."""
|
||||
|
|
@ -103,6 +117,8 @@ def get_style_guide(**kwargs):
|
|||
styleguide = StyleGuide(**kwargs)
|
||||
options = styleguide.options
|
||||
|
||||
_disable_extensions(kwargs['parser'], options)
|
||||
|
||||
if options.exclude and not isinstance(options.exclude, list):
|
||||
options.exclude = pep8.normalize_paths(options.exclude)
|
||||
elif not options.exclude:
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class TestEngine(unittest.TestCase):
|
|||
|
||||
def test_get_style_guide(self):
|
||||
with mock.patch('flake8.engine._register_extensions') as reg_ext:
|
||||
reg_ext.return_value = ([], [], [])
|
||||
reg_ext.return_value = ([], [], [], [])
|
||||
g = engine.get_style_guide()
|
||||
self.assertTrue(isinstance(g, engine.StyleGuide))
|
||||
reg_ext.assert_called_once_with()
|
||||
|
|
@ -37,6 +37,7 @@ class TestEngine(unittest.TestCase):
|
|||
m = mock.Mock()
|
||||
with mock.patch('flake8.engine.StyleGuide') as StyleGuide:
|
||||
with mock.patch('flake8.engine.get_parser') as get_parser:
|
||||
m.ignored_extensions = []
|
||||
StyleGuide.return_value.options.jobs = '42'
|
||||
get_parser.return_value = (m, [])
|
||||
engine.get_style_guide(foo='bar')
|
||||
|
|
@ -58,7 +59,8 @@ class TestEngine(unittest.TestCase):
|
|||
gpv = self.start_patch('flake8.engine.get_python_version')
|
||||
pgp = self.start_patch('pep8.get_parser')
|
||||
m = mock.Mock()
|
||||
re.return_value = ([('pyflakes', '0.7'), ('mccabe', '0.2')], [], [])
|
||||
re.return_value = ([('pyflakes', '0.7'), ('mccabe', '0.2')], [], [],
|
||||
[])
|
||||
gpv.return_value = 'Python Version'
|
||||
pgp.return_value = m
|
||||
# actual call we're testing
|
||||
|
|
@ -94,5 +96,20 @@ class TestEngine(unittest.TestCase):
|
|||
guide = engine.get_style_guide()
|
||||
assert isinstance(guide, reporter.BaseQReport) is False
|
||||
|
||||
def test_disables_extensions_that_are_not_selected(self):
|
||||
with mock.patch('flake8.engine._register_extensions') as re:
|
||||
re.return_value = ([('fake_ext', '0.1a1')], [], [], ['X'])
|
||||
sg = engine.get_style_guide()
|
||||
assert 'X' in sg.options.ignore
|
||||
|
||||
def test_enables_off_by_default_extensions(self):
|
||||
with mock.patch('flake8.engine._register_extensions') as re:
|
||||
re.return_value = ([('fake_ext', '0.1a1')], [], [], ['X'])
|
||||
parser, options = engine.get_parser()
|
||||
parser.parse_args(['--select=X'])
|
||||
sg = engine.StyleGuide(parser=parser)
|
||||
assert 'X' not in sg.options.ignore
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue