Skip to content

Commit

Permalink
Fix a false negative for --ignore-patterns (#9630)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mbyrnepr2 committed May 16, 2024
1 parent c6a9699 commit b3aceb0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9273.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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
3 changes: 2 additions & 1 deletion pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import sys
from collections.abc import Sequence
from pathlib import Path
from re import Pattern

from astroid import modutils
Expand Down Expand Up @@ -58,7 +59,7 @@ def _is_ignored_file(
ignore_list_paths_re: list[Pattern[str]],
) -> bool:
element = os.path.normpath(element)
basename = os.path.basename(element)
basename = Path(element).absolute().name
return (
basename in ignore_list
or _is_in_ignore_list_re(basename, ignore_list_re)
Expand Down
1 change: 1 addition & 0 deletions tests/regrtest_data/ignore_pattern/.hidden/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import os
1 change: 1 addition & 0 deletions tests/regrtest_data/ignore_pattern/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import os
21 changes: 21 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,27 @@ def test_ignore_pattern_recursive(self, ignore_pattern_value: str) -> None:
code=0,
)

@pytest.mark.parametrize("ignore_pattern_value", ["^\\.", "^\\..+", "^\\..*"])
def test_ignore_pattern_recursive_rel_path(self, ignore_pattern_value: str) -> None:
"""Test that ``--ignore-patterns`` strictly only ignores files
whose names begin with a "." when a dot is used to specify the
current directory.
"""
expected = "module.py:1:0: W0611: Unused import os (unused-import)"
unexpected = ".hidden/module.py:1:0: W0611: Unused import os (unused-import)"

with _test_cwd():
os.chdir(join(HERE, "regrtest_data", "ignore_pattern"))
self._test_output(
[
".",
"--recursive=y",
f"--ignore-patterns={ignore_pattern_value}",
],
expected_output=expected,
unexpected_output=unexpected,
)

def test_ignore_pattern_from_stdin(self) -> None:
"""Test if linter ignores standard input if the filename matches the ignore pattern."""
with mock.patch("pylint.lint.pylinter._read_stdin", return_value="import os\n"):
Expand Down

0 comments on commit b3aceb0

Please sign in to comment.