Skip to content
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

Support cyclic-import message with --jobs #8807

Merged
merged 2 commits into from
Jun 30, 2023
Merged
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
5 changes: 0 additions & 5 deletions doc/user_guide/usage/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ This will spawn 4 parallel Pylint sub-process, where each provided module will
be checked in parallel. Discovered problems by checkers are not displayed
immediately. They are shown just after checking a module is complete.

There is one known limitation with running checks in parallel as currently
implemented. Since the division of files into worker processes is indeterminate,
checkers that depend on comparing multiple files (e.g. ``cyclic-import``
and ``duplicate-code``) can produce indeterminate results.

Exit codes
----------

Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/4171.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support ``cyclic-import`` message when parallelizing with ``--jobs``.

Closes #4171
13 changes: 12 additions & 1 deletion pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def __init__(self, linter: PyLinter) -> None:
("RP0401", "External dependencies", self._report_external_dependencies),
("RP0402", "Modules dependencies graph", self._report_dependencies_graph),
)
self._excluded_edges: defaultdict[str, set[str]] = defaultdict(set)

def open(self) -> None:
"""Called before visiting project (i.e set of modules)."""
Expand All @@ -463,7 +464,6 @@ def open(self) -> None:
self.import_graph = defaultdict(set)
self._module_pkg = {} # mapping of modules to the pkg they belong in
self._current_module_package = False
self._excluded_edges: defaultdict[str, set[str]] = defaultdict(set)
self._ignored_modules: Sequence[str] = self.linter.config.ignored_modules
# Build a mapping {'module': 'preferred-module'}
self.preferred_modules = dict(
Expand All @@ -488,6 +488,17 @@ def close(self) -> None:
for cycle in get_cycles(graph, vertices=vertices):
self.add_message("cyclic-import", args=" -> ".join(cycle))

def get_map_data(self) -> defaultdict[str, set[str]]:
return self.import_graph

def reduce_map_data(
self, linter: PyLinter, data: list[defaultdict[str, set[str]]]
) -> None:
self.import_graph = defaultdict(set)
for graph in data:
self.import_graph.update(graph)
self.close()

def deprecated_modules(self) -> set[str]:
"""Callback returning the deprecated modules."""
# First get the modules the user indicated
Expand Down
30 changes: 30 additions & 0 deletions tests/test_check_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures.process import BrokenProcessPool
from pathlib import Path
from pickle import PickleError
from typing import TYPE_CHECKING
from unittest.mock import patch
Expand All @@ -23,11 +24,13 @@
import pylint.interfaces
import pylint.lint.parallel
from pylint.checkers import BaseRawFileChecker
from pylint.checkers.imports import ImportsChecker
from pylint.lint import PyLinter, augmented_sys_path
from pylint.lint.parallel import _worker_check_single_file as worker_check_single_file
from pylint.lint.parallel import _worker_initialize as worker_initialize
from pylint.lint.parallel import check_parallel
from pylint.testutils import GenericTestReporter as Reporter
from pylint.testutils.utils import _test_cwd
from pylint.typing import FileItem
from pylint.utils import LinterStats, ModuleStats

Expand Down Expand Up @@ -625,3 +628,30 @@ def test_no_deadlock_due_to_initializer_error(self) -> None:
# because arguments has to be an Iterable.
extra_packages_paths=1, # type: ignore[arg-type]
)

@pytest.mark.needs_two_cores
def test_cyclic_import_parallel(self) -> None:
tests_dir = Path("tests")
package_path = Path("input") / "func_w0401_package"
linter = PyLinter(reporter=Reporter())
linter.register_checker(ImportsChecker(linter))

with _test_cwd(tests_dir):
check_parallel(
linter,
jobs=2,
files=[
FileItem(
name="input.func_w0401_package.all_the_things",
filepath=str(package_path / "all_the_things.py"),
modpath="input.func_w0401_package",
),
FileItem(
name="input.func_w0401_package.thing2",
filepath=str(package_path / "thing2.py"),
modpath="input.func_w0401_package",
),
],
)

assert "cyclic-import" in linter.stats.by_msg
Loading