Skip to content

Commit

Permalink
Change _iterate_file_descrs to _get_file_items.
Browse files Browse the repository at this point in the history
Returning a materialized list instead of an iterator lets us do
simple progress reporting in the future.  As the list of FileItems
is very lightweight, there shouldn't be a performance hit.
  • Loading branch information
jzohrab committed Dec 18, 2024
1 parent f6af12d commit 04e2cee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
33 changes: 16 additions & 17 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def check(self, files_or_modules: Sequence[str]) -> None:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
self._get_file_items(files_or_modules),
extra_packages_paths,
)
sys.path = original_sys_path
Expand All @@ -690,7 +690,7 @@ def check(self, files_or_modules: Sequence[str]) -> None:
fileitems = self._get_file_descr_from_stdin(files_or_modules[0])
data: str | None = _read_stdin()
else:
fileitems = self._iterate_file_descrs(files_or_modules)
fileitems = self._get_file_items(files_or_modules)
data = None

# The contextmanager also opens all checkers and sets up the PyLinter class
Expand All @@ -703,7 +703,7 @@ def check(self, files_or_modules: Sequence[str]) -> None:
self._lint_files(ast_per_fileitem, check_astroid_module)

def _get_asts(
self, fileitems: Iterator[FileItem], data: str | None
self, fileitems: list[FileItem], data: str | None
) -> dict[FileItem, nodes.Module | None]:
"""Get the AST for all given FileItems."""
ast_per_fileitem: dict[FileItem, nodes.Module | None] = {}
Expand Down Expand Up @@ -837,9 +837,9 @@ def _check_file(
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)

def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]:
"""Return file description (tuple of module name, file path, base name) from
given file path.
def _get_file_descr_from_stdin(self, filepath: str) -> list[FileItem]:
"""Return single-entry list file description (tuple of module
name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
Expand All @@ -850,7 +850,7 @@ def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]:
self.config.ignore_patterns,
self.config.ignore_paths,
):
return
return []

try:
# Note that this function does not really perform an
Expand All @@ -860,20 +860,19 @@ def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]:
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]

yield FileItem(modname, filepath, filepath)
return [FileItem(modname, filepath, filepath)]

def _iterate_file_descrs(
self, files_or_modules: Sequence[str]
) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file
def _get_file_items(self, files_or_modules: Sequence[str]) -> list[FileItem]:
"""Return list of file descriptions (tuples of module name, file
path, base name).
The returned generator yield one item for each Python module that should be linted.
Each element in the list is a Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules).values():
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
return [
FileItem(d["name"], d["path"], d["basename"])
for d in self._expand_files(files_or_modules).values()
if self.should_analyze_file(d["name"], d["path"], d["isarg"])
]

def _expand_files(
self, files_or_modules: Sequence[str]
Expand Down
2 changes: 1 addition & 1 deletion tests/lint/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ def test_recursive_ignore(ignore_parameter: str, ignore_parameter_value: str) ->
exit=False,
)

linted_files = run.linter._iterate_file_descrs(
linted_files = run.linter._get_file_items(
tuple(run.linter._discover_files([join(REGRTEST_DATA_DIR, "directory")]))
)
linted_file_paths = [file_item.filepath for file_item in linted_files]
Expand Down
10 changes: 5 additions & 5 deletions tests/test_check_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_sequential_checkers_work(self) -> None:
check_parallel(
linter,
jobs=1,
files=iter(single_file_container),
files=single_file_container,
)
assert len(linter.get_checkers()) == 2, (
"We should only have the 'main' and 'sequential-checker' "
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_invoke_single_job(self) -> None:
check_parallel(
linter,
jobs=1,
files=iter(single_file_container),
files=single_file_container,
)

assert {
Expand Down Expand Up @@ -523,7 +523,7 @@ def test_compare_workers_to_single_proc(
assert (
linter.config.jobs == 1
), "jobs>1 are ignored when calling _lint_files"
ast_mapping = linter._get_asts(iter(file_infos), None)
ast_mapping = linter._get_asts(file_infos, None)
with linter._astroid_module_checker() as check_astroid_module:
linter._lint_files(ast_mapping, check_astroid_module)
assert linter.msg_status == 0, "We should not fail the lint"
Expand Down Expand Up @@ -590,7 +590,7 @@ def test_map_reduce(self, num_files: int, num_jobs: int, num_checkers: int) -> N
assert (
linter.config.jobs == 1
), "jobs>1 are ignored when calling _lint_files"
ast_mapping = linter._get_asts(iter(file_infos), None)
ast_mapping = linter._get_asts(file_infos, None)
with linter._astroid_module_checker() as check_astroid_module:
linter._lint_files(ast_mapping, check_astroid_module)
stats_single_proc = linter.stats
Expand Down Expand Up @@ -624,7 +624,7 @@ def test_no_deadlock_due_to_initializer_error(self) -> None:
check_parallel(
linter,
jobs=1,
files=iter(single_file_container),
files=single_file_container,
# This will trigger an exception in the initializer for the parallel jobs
# because arguments has to be an Iterable.
extra_packages_paths=1, # type: ignore[arg-type]
Expand Down

0 comments on commit 04e2cee

Please sign in to comment.