mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-09 06:14:17 +00:00
Add --enable-extensions flag to Flake8
This new flag is added so that off-by-default extensions can be enabled without using --select which (currently) causes several problems with pep8's rule engine. This also adds support to the --enable-extensions flag to be specified as a multi-line config option in an appropriate config file. Closes GitLab #67
This commit is contained in:
parent
5b7dc3927a
commit
96cb23e2e7
2 changed files with 37 additions and 6 deletions
|
|
@ -78,6 +78,11 @@ def get_parser():
|
||||||
help='Redirect report to a file.',
|
help='Redirect report to a file.',
|
||||||
type='string', nargs=1, action='callback',
|
type='string', nargs=1, action='callback',
|
||||||
callback=callbacks.redirect_stdout)
|
callback=callbacks.redirect_stdout)
|
||||||
|
parser.add_option('--enable-extensions', default='',
|
||||||
|
dest='enabled_extensions',
|
||||||
|
help='Enable plugins and extensions that are disabled '
|
||||||
|
'by default',
|
||||||
|
type='string')
|
||||||
parser.ignored_extensions = ignored
|
parser.ignored_extensions = ignored
|
||||||
return parser, options_hooks
|
return parser, options_hooks
|
||||||
|
|
||||||
|
|
@ -166,17 +171,31 @@ class StyleGuide(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_multi_options(options, split_token=','):
|
||||||
|
r"""Split and strip and discard empties.
|
||||||
|
|
||||||
|
Turns the following:
|
||||||
|
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
|
||||||
|
into ["A", "B"].
|
||||||
|
|
||||||
|
Credit: Kristian Glass as contributed to pep8
|
||||||
|
"""
|
||||||
|
if options:
|
||||||
|
return [o.strip() for o in options.split(split_token) if o.strip()]
|
||||||
|
else:
|
||||||
|
return options
|
||||||
|
|
||||||
|
|
||||||
def _disable_extensions(parser, options):
|
def _disable_extensions(parser, options):
|
||||||
ignored_extensions = set(getattr(parser, 'ignored_extensions', []))
|
ignored_extensions = set(getattr(parser, 'ignored_extensions', []))
|
||||||
select = set(options.select)
|
enabled = set(_parse_multi_options(options.enabled_extensions))
|
||||||
|
|
||||||
enabled_extensions = ignored_extensions.intersection(select)
|
|
||||||
# Remove any of the selected extensions from the extensions ignored by
|
# Remove any of the selected extensions from the extensions ignored by
|
||||||
# default.
|
# default.
|
||||||
ignored_extensions -= select
|
ignored_extensions -= enabled
|
||||||
|
|
||||||
for extension in enabled_extensions:
|
|
||||||
options.select.remove(extension)
|
|
||||||
|
|
||||||
# Whatever is left afterwards should be unioned with options.ignore and
|
# Whatever is left afterwards should be unioned with options.ignore and
|
||||||
# options.ignore should be updated with that.
|
# options.ignore should be updated with that.
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,18 @@ class TestEngine(unittest.TestCase):
|
||||||
self.assertTrue(isinstance(i, list))
|
self.assertTrue(isinstance(i, list))
|
||||||
self.assertTrue(register_check.called)
|
self.assertTrue(register_check.called)
|
||||||
|
|
||||||
|
def test_disable_extensions(self):
|
||||||
|
parser = mock.MagicMock()
|
||||||
|
options = mock.MagicMock()
|
||||||
|
|
||||||
|
parser.ignored_extensions = ['I123', 'I345', 'I678', 'I910']
|
||||||
|
|
||||||
|
options.enabled_extensions = 'I345,\nI678,I910'
|
||||||
|
options.ignore = ('E121', 'E123')
|
||||||
|
|
||||||
|
engine._disable_extensions(parser, options)
|
||||||
|
self.assertEqual(set(options.ignore), set(['E121', 'E123', 'I123']))
|
||||||
|
|
||||||
def test_get_parser(self):
|
def test_get_parser(self):
|
||||||
# setup
|
# setup
|
||||||
re = self.start_patch('flake8.engine._register_extensions')
|
re = self.start_patch('flake8.engine._register_extensions')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue