Skip to content

Commit ee6b62f

Browse files
Fix a false negative for --ignore-patterns (#9630) (#9636)
* Fix a false negative for ``--ignore-patterns`` when the directory to be linted is specified using a dot(``.``) and all files are ignored instead of only the files whose name begin with a dot. Closes #9273 (cherry picked from commit b3aceb0) Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
1 parent 08359be commit ee6b62f

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a false negative for ``--ignore-patterns`` when the directory to be linted is specified using a dot(``.``) and all files are ignored instead of only the files whose name begin with a dot.
2+
3+
Closes #9273

pylint/lint/expand_modules.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
from collections.abc import Sequence
10+
from pathlib import Path
1011
from re import Pattern
1112

1213
from astroid import modutils
@@ -58,7 +59,7 @@ def _is_ignored_file(
5859
ignore_list_paths_re: list[Pattern[str]],
5960
) -> bool:
6061
element = os.path.normpath(element)
61-
basename = os.path.basename(element)
62+
basename = Path(element).absolute().name
6263
return (
6364
basename in ignore_list
6465
or _is_in_ignore_list_re(basename, ignore_list_re)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import os
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import os

tests/test_self.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,27 @@ def test_ignore_pattern_recursive(self, ignore_pattern_value: str) -> None:
12021202
code=0,
12031203
)
12041204

1205+
@pytest.mark.parametrize("ignore_pattern_value", ["^\\.", "^\\..+", "^\\..*"])
1206+
def test_ignore_pattern_recursive_rel_path(self, ignore_pattern_value: str) -> None:
1207+
"""Test that ``--ignore-patterns`` strictly only ignores files
1208+
whose names begin with a "." when a dot is used to specify the
1209+
current directory.
1210+
"""
1211+
expected = "module.py:1:0: W0611: Unused import os (unused-import)"
1212+
unexpected = ".hidden/module.py:1:0: W0611: Unused import os (unused-import)"
1213+
1214+
with _test_cwd():
1215+
os.chdir(join(HERE, "regrtest_data", "ignore_pattern"))
1216+
self._test_output(
1217+
[
1218+
".",
1219+
"--recursive=y",
1220+
f"--ignore-patterns={ignore_pattern_value}",
1221+
],
1222+
expected_output=expected,
1223+
unexpected_output=unexpected,
1224+
)
1225+
12051226
def test_ignore_pattern_from_stdin(self) -> None:
12061227
"""Test if linter ignores standard input if the filename matches the ignore pattern."""
12071228
with mock.patch("pylint.lint.pylinter._read_stdin", return_value="import os\n"):

0 commit comments

Comments
 (0)