Skip to content

Commit

Permalink
Do not lint ignored file on stdin (pylint-dev#7220)
Browse files Browse the repository at this point in the history
Previously pylint would lint a file passed on stdin even if the user
meant to ignore the file. This commit fixes that issue.

Co-authored-by: Daniël van Noord <[email protected]>
  • Loading branch information
2 people authored and Pierre-Sassoulas committed Sep 6, 2022
1 parent c4a770d commit 8a8de1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/4354.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ignored files being linted when passed on stdin.

Closes #4354
17 changes: 11 additions & 6 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,7 @@ def check(self, files_or_modules: Sequence[str] | str) -> None:
# 3) Get all FileItems
with fix_import_path(files_or_modules):
if self.config.from_stdin:
fileitems = iter(
(self._get_file_descr_from_stdin(files_or_modules[0]),)
)
fileitems = self._get_file_descr_from_stdin(files_or_modules[0])
data: str | None = _read_stdin()
else:
fileitems = self._iterate_file_descrs(files_or_modules)
Expand Down Expand Up @@ -817,14 +815,21 @@ def _check_file(
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)

@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]:
"""Return file description (tuple of module name, file path, base name) from
given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
if _is_ignored_file(
filepath,
self.config.ignore,
self.config.ignore_patterns,
self.config.ignore_paths,
):
return

try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
Expand All @@ -833,7 +838,7 @@ def _get_file_descr_from_stdin(filepath: str) -> FileItem:
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]

return FileItem(modname, filepath, filepath)
yield FileItem(modname, filepath, filepath)

def _iterate_file_descrs(
self, files_or_modules: Sequence[str]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,20 @@ def test_ignore_pattern_recursive(self, ignore_pattern_value: str) -> None:
code=0,
)

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"):
self._runtest(
[
"--from-stdin",
"mymodule.py",
"--disable=all",
"--enable=unused-import",
"--ignore-patterns=mymodule.py",
],
code=0,
)

@pytest.mark.parametrize("ignore_path_value", [".*ignored.*", ".*failing.*"])
def test_ignore_path_recursive(self, ignore_path_value: str) -> None:
"""Tests recursive run of linter ignoring directory using --ignore-path parameter.
Expand Down

0 comments on commit 8a8de1f

Please sign in to comment.