|
26 | 26 | from pylint import checkers, exceptions, interfaces, reporters
|
27 | 27 | from pylint.checkers.base_checker import BaseChecker
|
28 | 28 | from pylint.config.arguments_manager import _ArgumentsManager
|
| 29 | +from pylint.config.config_initialization import _config_initialization |
| 30 | +from pylint.config.find_default_config_files import find_subdirectory_config_files |
29 | 31 | from pylint.constants import (
|
30 | 32 | MAIN_CHECKER_NAME,
|
31 | 33 | MSG_TYPES,
|
@@ -616,6 +618,37 @@ def initialize(self) -> None:
|
616 | 618 | if not msg.may_be_emitted(self.config.py_version):
|
617 | 619 | self._msgs_state[msg.msgid] = False
|
618 | 620 |
|
| 621 | + def register_local_config(self, file_or_dir: str) -> None: |
| 622 | + if os.path.isdir(file_or_dir): |
| 623 | + basedir = Path(file_or_dir) |
| 624 | + else: |
| 625 | + basedir = Path(os.path.dirname(file_or_dir)) |
| 626 | + |
| 627 | + if self.config.use_parent_configs is False: |
| 628 | + # exit loop after first iteration |
| 629 | + scan_root_dir = basedir |
| 630 | + elif _is_relative_to(basedir, Path(os.getcwd())): |
| 631 | + scan_root_dir = Path(os.getcwd()) |
| 632 | + else: |
| 633 | + scan_root_dir = Path("/") |
| 634 | + |
| 635 | + while basedir.resolve() not in self._directory_namespaces and _is_relative_to( |
| 636 | + basedir, scan_root_dir |
| 637 | + ): |
| 638 | + local_conf = next(find_subdirectory_config_files(basedir), None) |
| 639 | + if local_conf is not None: |
| 640 | + _config_initialization(self, self._cli_args, config_file=local_conf) |
| 641 | + self._directory_namespaces[Path(basedir).resolve()] = ( |
| 642 | + self.config, |
| 643 | + {}, |
| 644 | + ) |
| 645 | + self.config = self._base_config |
| 646 | + break |
| 647 | + if basedir.parent != basedir: |
| 648 | + basedir = basedir.parent |
| 649 | + else: |
| 650 | + break |
| 651 | + |
619 | 652 | def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]:
|
620 | 653 | """Discover python modules and packages in sub-directory.
|
621 | 654 |
|
@@ -666,12 +699,15 @@ def check(self, files_or_modules: Sequence[str]) -> None:
|
666 | 699 | "Missing filename required for --from-stdin"
|
667 | 700 | )
|
668 | 701 |
|
669 |
| - extra_packages_paths = list( |
670 |
| - { |
| 702 | + if self.config.use_local_configs is True: |
| 703 | + for file_or_module in files_or_modules: |
| 704 | + self.register_local_config(file_or_module) |
| 705 | + extra_packages_paths_set = set() |
| 706 | + for file_or_module in files_or_modules: |
| 707 | + extra_packages_paths_set.add( |
671 | 708 | discover_package_path(file_or_module, self.config.source_roots)
|
672 |
| - for file_or_module in files_or_modules |
673 |
| - } |
674 |
| - ) |
| 709 | + ) |
| 710 | + extra_packages_paths = list(extra_packages_paths_set) |
675 | 711 |
|
676 | 712 | # TODO: Move the parallel invocation into step 3 of the checking process
|
677 | 713 | if not self.config.from_stdin and self.config.jobs > 1:
|
|
0 commit comments