Skip to content

Change _iterate_file_descrs to _get_file_items #10142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ def __init__(
self.option_groups_descs[opt_group[0]] = opt_group[1]
self._option_groups: tuple[tuple[str, str], ...] = (
*option_groups,
("Messages control", "Options controlling analysis messages"),
("Reports", "Options related to output formatting and reporting"),
*PyLinter.option_groups_descs.items(),
)
self.fail_on_symbols: list[str] = []
"""List of message symbols on which pylint should fail, set by --fail-on."""
Expand Down Expand Up @@ -679,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 @@ -691,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 @@ -704,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 @@ -838,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 @@ -851,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 @@ -861,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 @@ -11,7 +11,7 @@
import re
import sys
import tempfile
from collections.abc import Iterator

Check failure on line 14 in tests/lint/unittest_lint.py

View workflow job for this annotation

GitHub Actions / pylint

E0401

Unable to import 'collections.abc'
from contextlib import contextmanager
from io import StringIO
from os import chdir, getcwd
Expand Down Expand Up @@ -1113,7 +1113,7 @@
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
Loading