mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-14 08:24:46 +00:00
Handle a previously unhandled code scenario
Previously, we didn't handle the case where an error code was implicitly ignored (by not being in --select) and implicitly selected (by not being in --ignore). This means we need to update StyleGuide#_decision_for and StyleGuide#is_user_selected to handle these cases. Closes #242 Related-to #239 Related-to !132
This commit is contained in:
parent
9553c8d8cc
commit
4f04ca549b
2 changed files with 39 additions and 20 deletions
|
|
@ -64,8 +64,9 @@ class StyleGuide(object):
|
||||||
self.stats = statistics.Statistics()
|
self.stats = statistics.Statistics()
|
||||||
self._selected = tuple(options.select)
|
self._selected = tuple(options.select)
|
||||||
self._extended_selected = tuple(options.extended_default_select)
|
self._extended_selected = tuple(options.extended_default_select)
|
||||||
self._ignored = tuple(options.ignore)
|
|
||||||
self._enabled_extensions = tuple(options.enable_extensions)
|
self._enabled_extensions = tuple(options.enable_extensions)
|
||||||
|
self._all_selected = self._selected + self._enabled_extensions
|
||||||
|
self._ignored = tuple(options.ignore)
|
||||||
self._decision_cache = {}
|
self._decision_cache = {}
|
||||||
self._parsed_diff = {}
|
self._parsed_diff = {}
|
||||||
|
|
||||||
|
|
@ -82,17 +83,15 @@ class StyleGuide(object):
|
||||||
Ignored.Implicitly if the selected list is not empty but no match
|
Ignored.Implicitly if the selected list is not empty but no match
|
||||||
was found.
|
was found.
|
||||||
"""
|
"""
|
||||||
if not (self._selected or self._enabled_extensions):
|
if self._all_selected and code.startswith(self._all_selected):
|
||||||
return Selected.Implicitly
|
|
||||||
|
|
||||||
if code.startswith(self._selected + self._enabled_extensions):
|
|
||||||
return Selected.Explicitly
|
return Selected.Explicitly
|
||||||
|
|
||||||
# If it was not explicitly selected, it may have been implicitly
|
if (not self._all_selected and
|
||||||
# selected because the check comes from a plugin that is enabled by
|
(self._extended_selected and
|
||||||
# default
|
code.startswith(self._extended_selected))):
|
||||||
if (self._extended_selected and
|
# If it was not explicitly selected, it may have been implicitly
|
||||||
code.startswith(self._extended_selected)):
|
# selected because the check comes from a plugin that is enabled by
|
||||||
|
# default
|
||||||
return Selected.Implicitly
|
return Selected.Implicitly
|
||||||
|
|
||||||
return Ignored.Implicitly
|
return Ignored.Implicitly
|
||||||
|
|
@ -118,10 +117,22 @@ class StyleGuide(object):
|
||||||
def _decision_for(self, code):
|
def _decision_for(self, code):
|
||||||
# type: (Error) -> Decision
|
# type: (Error) -> Decision
|
||||||
startswith = code.startswith
|
startswith = code.startswith
|
||||||
selected = sorted([s for s in self._selected if startswith(s)])[0]
|
try:
|
||||||
ignored = sorted([i for i in self._ignored if startswith(i)])[0]
|
selected = sorted([s for s in self._selected if startswith(s)])[0]
|
||||||
|
except IndexError:
|
||||||
|
selected = None
|
||||||
|
try:
|
||||||
|
ignored = sorted([i for i in self._ignored if startswith(i)])[0]
|
||||||
|
except IndexError:
|
||||||
|
ignored = None
|
||||||
|
|
||||||
if selected.startswith(ignored):
|
if selected is None:
|
||||||
|
return Decision.Ignored
|
||||||
|
|
||||||
|
if ignored is None:
|
||||||
|
return Decision.Selected
|
||||||
|
|
||||||
|
if selected.startswith(ignored) and selected != ignored:
|
||||||
return Decision.Selected
|
return Decision.Selected
|
||||||
return Decision.Ignored
|
return Decision.Ignored
|
||||||
|
|
||||||
|
|
@ -150,8 +161,10 @@ class StyleGuide(object):
|
||||||
selected is Selected.Implicitly) and
|
selected is Selected.Implicitly) and
|
||||||
ignored is Selected.Implicitly):
|
ignored is Selected.Implicitly):
|
||||||
decision = Decision.Selected
|
decision = Decision.Selected
|
||||||
elif (selected is Selected.Explicitly and
|
elif ((selected is Selected.Explicitly and
|
||||||
ignored is Ignored.Explicitly):
|
ignored is Ignored.Explicitly) or
|
||||||
|
(selected is Ignored.Implicitly and
|
||||||
|
ignored is Selected.Implicitly)):
|
||||||
decision = self._decision_for(code)
|
decision = self._decision_for(code)
|
||||||
elif (selected is Ignored.Implicitly or
|
elif (selected is Ignored.Implicitly or
|
||||||
ignored is Ignored.Explicitly):
|
ignored is Ignored.Explicitly):
|
||||||
|
|
|
||||||
|
|
@ -78,9 +78,14 @@ def test_is_user_selected_implicitly_selects_errors():
|
||||||
"""Verify we detect users implicitly selecting an error."""
|
"""Verify we detect users implicitly selecting an error."""
|
||||||
select_list = []
|
select_list = []
|
||||||
error_code = 'E121'
|
error_code = 'E121'
|
||||||
guide = style_guide.StyleGuide(create_options(select=select_list),
|
guide = style_guide.StyleGuide(
|
||||||
listener_trie=None,
|
create_options(
|
||||||
formatter=None)
|
select=select_list,
|
||||||
|
extended_default_select=['E'],
|
||||||
|
),
|
||||||
|
listener_trie=None,
|
||||||
|
formatter=None,
|
||||||
|
)
|
||||||
|
|
||||||
assert (guide.is_user_selected(error_code) is
|
assert (guide.is_user_selected(error_code) is
|
||||||
style_guide.Selected.Implicitly)
|
style_guide.Selected.Implicitly)
|
||||||
|
|
@ -114,9 +119,11 @@ def test_is_user_selected_excludes_errors(select_list, error_code):
|
||||||
(['E111', 'E121'], ['E2'], 'E122', style_guide.Decision.Ignored),
|
(['E111', 'E121'], ['E2'], 'E122', style_guide.Decision.Ignored),
|
||||||
(['E11', 'E12'], ['E13'], 'E132', style_guide.Decision.Ignored),
|
(['E11', 'E12'], ['E13'], 'E132', style_guide.Decision.Ignored),
|
||||||
(['E1', 'E3'], ['E32'], 'E321', style_guide.Decision.Ignored),
|
(['E1', 'E3'], ['E32'], 'E321', style_guide.Decision.Ignored),
|
||||||
([], ['E2', 'E12'], 'E410', style_guide.Decision.Selected),
|
([], ['E2', 'E12'], 'E410', style_guide.Decision.Ignored),
|
||||||
(['E4'], ['E2', 'E12', 'E41'], 'E410', style_guide.Decision.Ignored),
|
(['E4'], ['E2', 'E12', 'E41'], 'E410', style_guide.Decision.Ignored),
|
||||||
(['E41'], ['E2', 'E12', 'E4'], 'E410', style_guide.Decision.Selected),
|
(['E41'], ['E2', 'E12', 'E4'], 'E410', style_guide.Decision.Selected),
|
||||||
|
(['E'], ['F'], 'E410', style_guide.Decision.Selected),
|
||||||
|
(['F'], [], 'E410', style_guide.Decision.Ignored),
|
||||||
])
|
])
|
||||||
def test_should_report_error(select_list, ignore_list, error_code, expected):
|
def test_should_report_error(select_list, ignore_list, error_code, expected):
|
||||||
"""Verify we decide when to report an error."""
|
"""Verify we decide when to report an error."""
|
||||||
|
|
@ -172,7 +179,6 @@ def test_disable_is_inline_ignored():
|
||||||
(['E111', 'E121'], [], 'E111'),
|
(['E111', 'E121'], [], 'E111'),
|
||||||
(['E111', 'E121'], [], 'E121'),
|
(['E111', 'E121'], [], 'E121'),
|
||||||
(['E11', 'E121'], ['E1'], 'E112'),
|
(['E11', 'E121'], ['E1'], 'E112'),
|
||||||
([], ['E2', 'E12'], 'E410'),
|
|
||||||
(['E41'], ['E2', 'E12', 'E4'], 'E410'),
|
(['E41'], ['E2', 'E12', 'E4'], 'E410'),
|
||||||
])
|
])
|
||||||
def test_handle_error_notifies_listeners(select_list, ignore_list, error_code):
|
def test_handle_error_notifies_listeners(select_list, ignore_list, error_code):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue