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

Respect ignore configuration options when --recursive=y. #6528

Merged
merged 18 commits into from May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Expand Up @@ -317,6 +317,11 @@ What's New in Pylint 2.13.9?
============================
Release date: TBA


* Respect ignore configuration options when --recursive=y.
matusvalo marked this conversation as resolved.
Show resolved Hide resolved

Closes #6471

* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method.

Relates to #6531
Expand Down
19 changes: 17 additions & 2 deletions pylint/lint/pylinter.py
Expand Up @@ -697,8 +697,7 @@ def initialize(self) -> None:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = False

@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in sub-directory.

Returns iterator of paths to discovered modules and packages.
Expand All @@ -707,11 +706,27 @@ def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):

matusvalo marked this conversation as resolved.
Show resolved Hide resolved
skip_subtrees: list[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if any(
pattern.match(os.path.basename(root))
for pattern in self.config.ignore_patterns
):
# Skip if matches ignore-patterns list
skip_subtrees.append(root)
continue
if any(pattern.match(root) for pattern in self.config.ignore_paths):
# Skip if matches ignore-paths list
skip_subtrees.append(root)
continue
if os.path.basename(root) in self.config.ignore:
# Skip if in ignore list
skip_subtrees.append(root)
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
Expand Down
@@ -0,0 +1 @@
import re
59 changes: 58 additions & 1 deletion tests/test_self.py
Expand Up @@ -1239,6 +1239,63 @@ def test_recursive(self):
code=0,
)

def test_ignore_recursive(self):
self._runtest(
matusvalo marked this conversation as resolved.
Show resolved Hide resolved
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore=ignored_subdirectory",
],
code=0,
)

self._runtest(
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore=failing.py",
],
code=0,
)

def test_ignore_pattern_recursive(self):
self._runtest(
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore-pattern=ignored_.*",
],
code=0,
)

self._runtest(
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore-pattern=failing.*",
],
code=0,
)

def test_ignore_path_recursive(self):
self._runtest(
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore-path=.*ignored.*",
],
code=0,
)

self._runtest(
[
join(HERE, "regrtest_data", "directory"),
"--recursive=y",
"--ignore-path=.*failing.*",
],
code=0,
)

def test_recursive_current_dir(self):
with _test_sys_path():
# pytest is including directory HERE/regrtest_data to sys.path which causes
Expand All @@ -1249,7 +1306,7 @@ def test_recursive_current_dir(self):
if not os.path.basename(path) == "regrtest_data"
]
with _test_cwd():
os.chdir(join(HERE, "regrtest_data", "directory"))
os.chdir(join(HERE, "regrtest_data", "directory", "subdirectory"))
self._runtest(
[".", "--recursive=y"],
code=0,
Expand Down