mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 15:44:17 +00:00
Add ability to turn checks off by default
This commit is contained in:
parent
07677578b4
commit
b301532636
2 changed files with 35 additions and 4 deletions
|
|
@ -19,6 +19,7 @@ def _register_extensions():
|
||||||
extensions.add(('pep8', pep8.__version__))
|
extensions.add(('pep8', pep8.__version__))
|
||||||
parser_hooks = []
|
parser_hooks = []
|
||||||
options_hooks = []
|
options_hooks = []
|
||||||
|
ignored_hooks = []
|
||||||
try:
|
try:
|
||||||
from pkg_resources import iter_entry_points
|
from pkg_resources import iter_entry_points
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -32,7 +33,9 @@ def _register_extensions():
|
||||||
parser_hooks.append(checker.add_options)
|
parser_hooks.append(checker.add_options)
|
||||||
if hasattr(checker, 'parse_options'):
|
if hasattr(checker, 'parse_options'):
|
||||||
options_hooks.append(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):
|
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
|
"""This returns an instance of optparse.OptionParser with all the
|
||||||
extensions registered and options set. This wraps ``pep8.get_parser``.
|
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])
|
details = ', '.join(['%s: %s' % ext for ext in extensions])
|
||||||
python_version = get_python_version()
|
python_version = get_python_version()
|
||||||
parser = pep8.get_parser('flake8', '%s (%s) %s' % (
|
parser = pep8.get_parser('flake8', '%s (%s) %s' % (
|
||||||
|
|
@ -79,6 +82,7 @@ def get_parser():
|
||||||
help='Install the appropriate hook for this '
|
help='Install the appropriate hook for this '
|
||||||
'repository.', action='callback',
|
'repository.', action='callback',
|
||||||
callback=_install_hook_cb)
|
callback=_install_hook_cb)
|
||||||
|
parser.ignored_extensions = ignored
|
||||||
return parser, options_hooks
|
return parser, options_hooks
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -96,6 +100,14 @@ class StyleGuide(pep8.StyleGuide):
|
||||||
return fchecker.check_all(expected=expected, line_offset=line_offset)
|
return fchecker.check_all(expected=expected, line_offset=line_offset)
|
||||||
|
|
||||||
|
|
||||||
|
def _disable_extensions(parser, options):
|
||||||
|
select = set(options.select)
|
||||||
|
ignore = set(options.ignore)
|
||||||
|
ignore.update(getattr(parser, 'ignored_extensions', []))
|
||||||
|
ignore -= select
|
||||||
|
options.ignore = tuple(ignore)
|
||||||
|
|
||||||
|
|
||||||
def get_style_guide(**kwargs):
|
def get_style_guide(**kwargs):
|
||||||
"""Parse the options and configure the checker. This returns a sub-class
|
"""Parse the options and configure the checker. This returns a sub-class
|
||||||
of ``pep8.StyleGuide``."""
|
of ``pep8.StyleGuide``."""
|
||||||
|
|
@ -103,6 +115,8 @@ def get_style_guide(**kwargs):
|
||||||
styleguide = StyleGuide(**kwargs)
|
styleguide = StyleGuide(**kwargs)
|
||||||
options = styleguide.options
|
options = styleguide.options
|
||||||
|
|
||||||
|
_disable_extensions(kwargs['parser'], options)
|
||||||
|
|
||||||
if options.exclude and not isinstance(options.exclude, list):
|
if options.exclude and not isinstance(options.exclude, list):
|
||||||
options.exclude = pep8.normalize_paths(options.exclude)
|
options.exclude = pep8.normalize_paths(options.exclude)
|
||||||
elif not options.exclude:
|
elif not options.exclude:
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class TestEngine(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_style_guide(self):
|
def test_get_style_guide(self):
|
||||||
with mock.patch('flake8.engine._register_extensions') as reg_ext:
|
with mock.patch('flake8.engine._register_extensions') as reg_ext:
|
||||||
reg_ext.return_value = ([], [], [])
|
reg_ext.return_value = ([], [], [], [])
|
||||||
g = engine.get_style_guide()
|
g = engine.get_style_guide()
|
||||||
self.assertTrue(isinstance(g, engine.StyleGuide))
|
self.assertTrue(isinstance(g, engine.StyleGuide))
|
||||||
reg_ext.assert_called_once_with()
|
reg_ext.assert_called_once_with()
|
||||||
|
|
@ -37,6 +37,7 @@ class TestEngine(unittest.TestCase):
|
||||||
m = mock.Mock()
|
m = mock.Mock()
|
||||||
with mock.patch('flake8.engine.StyleGuide') as StyleGuide:
|
with mock.patch('flake8.engine.StyleGuide') as StyleGuide:
|
||||||
with mock.patch('flake8.engine.get_parser') as get_parser:
|
with mock.patch('flake8.engine.get_parser') as get_parser:
|
||||||
|
m.ignored_extensions = []
|
||||||
StyleGuide.return_value.options.jobs = '42'
|
StyleGuide.return_value.options.jobs = '42'
|
||||||
get_parser.return_value = (m, [])
|
get_parser.return_value = (m, [])
|
||||||
engine.get_style_guide(foo='bar')
|
engine.get_style_guide(foo='bar')
|
||||||
|
|
@ -58,7 +59,8 @@ class TestEngine(unittest.TestCase):
|
||||||
gpv = self.start_patch('flake8.engine.get_python_version')
|
gpv = self.start_patch('flake8.engine.get_python_version')
|
||||||
pgp = self.start_patch('pep8.get_parser')
|
pgp = self.start_patch('pep8.get_parser')
|
||||||
m = mock.Mock()
|
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'
|
gpv.return_value = 'Python Version'
|
||||||
pgp.return_value = m
|
pgp.return_value = m
|
||||||
# actual call we're testing
|
# actual call we're testing
|
||||||
|
|
@ -94,5 +96,20 @@ class TestEngine(unittest.TestCase):
|
||||||
guide = engine.get_style_guide()
|
guide = engine.get_style_guide()
|
||||||
assert isinstance(guide, reporter.BaseQReport) is False
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue