Merge pull request #1476 from asottile/files_not_nullable

refactor run_checks to not take an Optional list of filenames
This commit is contained in:
Anthony Sottile 2021-11-25 15:48:38 -05:00 committed by GitHub
commit b857d25c81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 22 deletions

View file

@ -103,7 +103,8 @@ class StyleGuide:
:rtype: :rtype:
flake8.api.legacy.Report flake8.api.legacy.Report
""" """
self._application.run_checks(paths) self._application.options.filenames = paths
self._application.run_checks()
self._application.report_errors() self._application.report_errors()
return Report(self._application) return Report(self._application)

View file

@ -56,21 +56,18 @@ class Manager:
together and make our output deterministic. together and make our output deterministic.
""" """
def __init__(self, style_guide, arguments, checker_plugins): def __init__(self, style_guide, checker_plugins):
"""Initialize our Manager instance. """Initialize our Manager instance.
:param style_guide: :param style_guide:
The instantiated style guide for this instance of Flake8. The instantiated style guide for this instance of Flake8.
:type style_guide: :type style_guide:
flake8.style_guide.StyleGuide flake8.style_guide.StyleGuide
:param list arguments:
The extra arguments parsed from the CLI (if any)
:param checker_plugins: :param checker_plugins:
The plugins representing checks parsed from entry-points. The plugins representing checks parsed from entry-points.
:type checker_plugins: :type checker_plugins:
flake8.plugins.manager.Checkers flake8.plugins.manager.Checkers
""" """
self.arguments = arguments
self.style_guide = style_guide self.style_guide = style_guide
self.options = style_guide.options self.options = style_guide.options
self.checks = checker_plugins self.checks = checker_plugins
@ -112,7 +109,7 @@ class Manager:
) )
return 0 return 0
if utils.is_using_stdin(self.arguments): if utils.is_using_stdin(self.options.filenames):
LOG.warning( LOG.warning(
"The --jobs option is not compatible with supplying " "The --jobs option is not compatible with supplying "
"input using - . Ignoring --jobs arguments." "input using - . Ignoring --jobs arguments."
@ -159,7 +156,7 @@ class Manager:
def make_checkers(self, paths: Optional[List[str]] = None) -> None: def make_checkers(self, paths: Optional[List[str]] = None) -> None:
"""Create checkers for each file.""" """Create checkers for each file."""
if paths is None: if paths is None:
paths = self.arguments paths = self.options.filenames
checks = self.checks.to_dictionary() checks = self.checks.to_dictionary()
self._all_checkers = [ self._all_checkers = [

View file

@ -246,28 +246,25 @@ class Application:
def make_file_checker_manager(self) -> None: def make_file_checker_manager(self) -> None:
"""Initialize our FileChecker Manager.""" """Initialize our FileChecker Manager."""
assert self.options is not None
self.file_checker_manager = checker.Manager( self.file_checker_manager = checker.Manager(
style_guide=self.guide, style_guide=self.guide,
arguments=self.options.filenames,
checker_plugins=self.check_plugins, checker_plugins=self.check_plugins,
) )
def run_checks(self, files: Optional[List[str]] = None) -> None: def run_checks(self) -> None:
"""Run the actual checks with the FileChecker Manager. """Run the actual checks with the FileChecker Manager.
This method encapsulates the logic to make a This method encapsulates the logic to make a
:class:`~flake8.checker.Manger` instance run the checks it is :class:`~flake8.checker.Manger` instance run the checks it is
managing. managing.
:param list files:
List of filenames to process
""" """
assert self.file_checker_manager is not None assert self.file_checker_manager is not None
if self.running_against_diff: if self.running_against_diff:
files = sorted(self.parsed_diff) files: Optional[List[str]] = sorted(self.parsed_diff)
if not files: if not files:
return return
else:
files = None
self.file_checker_manager.start(files) self.file_checker_manager.start(files)
try: try:

View file

@ -293,7 +293,7 @@ def test_report_order(results, expected_order):
# Create a placeholder manager without arguments or plugins # Create a placeholder manager without arguments or plugins
# Just add one custom file checker which just provides the results # Just add one custom file checker which just provides the results
manager = checker.Manager(style_guide, [], []) manager = checker.Manager(style_guide, [])
manager.checkers = manager._all_checkers = [file_checker] manager.checkers = manager._all_checkers = [file_checker]
# _handle_results is the first place which gets the sorted result # _handle_results is the first place which gets the sorted result

View file

@ -22,7 +22,7 @@ def style_guide_mock():
def _parallel_checker_manager(): def _parallel_checker_manager():
"""Call Manager.run() and return the number of calls to `run_serial`.""" """Call Manager.run() and return the number of calls to `run_serial`."""
style_guide = style_guide_mock() style_guide = style_guide_mock()
manager = checker.Manager(style_guide, [], []) manager = checker.Manager(style_guide, [])
# multiple checkers is needed for parallel mode # multiple checkers is needed for parallel mode
manager.checkers = [mock.Mock(), mock.Mock()] manager.checkers = [mock.Mock(), mock.Mock()]
return manager return manager
@ -54,7 +54,7 @@ def test_oserrors_are_reraised(_):
def test_multiprocessing_is_disabled(_): def test_multiprocessing_is_disabled(_):
"""Verify not being able to import multiprocessing forces jobs to 0.""" """Verify not being able to import multiprocessing forces jobs to 0."""
style_guide = style_guide_mock() style_guide = style_guide_mock()
manager = checker.Manager(style_guide, [], []) manager = checker.Manager(style_guide, [])
assert manager.jobs == 0 assert manager.jobs == 0
@ -68,7 +68,7 @@ def test_multiprocessing_cpu_count_not_implemented():
"cpu_count", "cpu_count",
side_effect=NotImplementedError, side_effect=NotImplementedError,
): ):
manager = checker.Manager(style_guide, [], []) manager = checker.Manager(style_guide, [])
assert manager.jobs == 0 assert manager.jobs == 0
@ -76,14 +76,14 @@ def test_multiprocessing_cpu_count_not_implemented():
def test_make_checkers(_): def test_make_checkers(_):
"""Verify that we create a list of FileChecker instances.""" """Verify that we create a list of FileChecker instances."""
style_guide = style_guide_mock() style_guide = style_guide_mock()
files = ["file1", "file2"] style_guide.options.filenames = ["file1", "file2"]
checkplugins = mock.Mock() checkplugins = mock.Mock()
checkplugins.to_dictionary.return_value = { checkplugins.to_dictionary.return_value = {
"ast_plugins": [], "ast_plugins": [],
"logical_line_plugins": [], "logical_line_plugins": [],
"physical_line_plugins": [], "physical_line_plugins": [],
} }
manager = checker.Manager(style_guide, files, checkplugins) manager = checker.Manager(style_guide, checkplugins)
with mock.patch("flake8.utils.fnmatch", return_value=True): with mock.patch("flake8.utils.fnmatch", return_value=True):
with mock.patch("flake8.processor.FileProcessor"): with mock.patch("flake8.processor.FileProcessor"):
@ -91,5 +91,5 @@ def test_make_checkers(_):
assert manager._all_checkers assert manager._all_checkers
for file_checker in manager._all_checkers: for file_checker in manager._all_checkers:
assert file_checker.filename in files assert file_checker.filename in style_guide.options.filenames
assert not manager.checkers # the files don't exist assert not manager.checkers # the files don't exist

View file

@ -68,7 +68,8 @@ def test_styleguide_check_files():
style_guide = api.StyleGuide(app) style_guide = api.StyleGuide(app)
report = style_guide.check_files(paths) report = style_guide.check_files(paths)
app.run_checks.assert_called_once_with(paths) assert app.options.filenames == paths
app.run_checks.assert_called_once_with()
app.report_errors.assert_called_once_with() app.report_errors.assert_called_once_with()
assert isinstance(report, api.Report) assert isinstance(report, api.Report)