Skip to content

Commit 61f551f

Browse files
Support cyclic-import message with --jobs
1 parent 3a99820 commit 61f551f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

doc/whatsnew/fragments/4171.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Support ``cyclic-import`` message when parallelizing with ``--jobs``.
2+
3+
Closes #4171

pylint/checkers/imports.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ def __init__(self, linter: PyLinter) -> None:
455455
("RP0401", "External dependencies", self._report_external_dependencies),
456456
("RP0402", "Modules dependencies graph", self._report_dependencies_graph),
457457
)
458+
self._excluded_edges: defaultdict[str, set[str]] = defaultdict(set)
458459

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

491+
def get_map_data(self) -> defaultdict[str, set[str]]:
492+
return self.import_graph
493+
494+
def reduce_map_data(
495+
self, linter: PyLinter, data: list[defaultdict[str, set[str]]]
496+
) -> None:
497+
self.import_graph = defaultdict(set)
498+
for graph in data:
499+
self.import_graph.update(graph)
500+
self.close()
501+
491502
def deprecated_modules(self) -> set[str]:
492503
"""Callback returning the deprecated modules."""
493504
# First get the modules the user indicated

tests/test_check_parallel.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
from concurrent.futures import ProcessPoolExecutor
1515
from concurrent.futures.process import BrokenProcessPool
16+
from pathlib import Path
1617
from pickle import PickleError
1718
from typing import TYPE_CHECKING
1819
from unittest.mock import patch
@@ -23,11 +24,13 @@
2324
import pylint.interfaces
2425
import pylint.lint.parallel
2526
from pylint.checkers import BaseRawFileChecker
27+
from pylint.checkers.imports import ImportsChecker
2628
from pylint.lint import PyLinter, augmented_sys_path
2729
from pylint.lint.parallel import _worker_check_single_file as worker_check_single_file
2830
from pylint.lint.parallel import _worker_initialize as worker_initialize
2931
from pylint.lint.parallel import check_parallel
3032
from pylint.testutils import GenericTestReporter as Reporter
33+
from pylint.testutils.utils import _test_cwd
3134
from pylint.typing import FileItem
3235
from pylint.utils import LinterStats, ModuleStats
3336

@@ -625,3 +628,30 @@ def test_no_deadlock_due_to_initializer_error(self) -> None:
625628
# because arguments has to be an Iterable.
626629
extra_packages_paths=1, # type: ignore[arg-type]
627630
)
631+
632+
@pytest.mark.needs_two_cores
633+
def test_cyclic_import_parallel(self) -> None:
634+
tests_dir = Path("tests")
635+
package_path = Path("input") / "func_w0401_package"
636+
linter = PyLinter(reporter=Reporter())
637+
linter.register_checker(ImportsChecker(linter))
638+
639+
with _test_cwd(tests_dir):
640+
check_parallel(
641+
linter,
642+
jobs=2,
643+
files=[
644+
FileItem(
645+
name="input.func_w0401_package.all_the_things",
646+
filepath=str(package_path / "all_the_things.py"),
647+
modpath="input.func_w0401_package",
648+
),
649+
FileItem(
650+
name="input.func_w0401_package.thing2",
651+
filepath=str(package_path / "thing2.py"),
652+
modpath="input.func_w0401_package",
653+
),
654+
],
655+
)
656+
657+
assert "cyclic-import" in linter.stats.by_msg

0 commit comments

Comments
 (0)