Skip to content

Commit d476a8b

Browse files
christoph-blessingDanielNoord
authored andcommitted
Do not lint ignored file on stdin (#7220)
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]>
1 parent 7a1fbb9 commit d476a8b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

doc/whatsnew/fragments/4354.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ignored files being linted when passed on stdin.
2+
3+
Closes #4354

pylint/lint/pylinter.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,7 @@ def check(self, files_or_modules: Sequence[str] | str) -> None:
660660
# 3) Get all FileItems
661661
with fix_import_path(files_or_modules):
662662
if self.config.from_stdin:
663-
fileitems = iter(
664-
(self._get_file_descr_from_stdin(files_or_modules[0]),)
665-
)
663+
fileitems = self._get_file_descr_from_stdin(files_or_modules[0])
666664
data: str | None = _read_stdin()
667665
else:
668666
fileitems = self._iterate_file_descrs(files_or_modules)
@@ -817,14 +815,21 @@ def _check_file(
817815
for msgid, line, args in spurious_messages:
818816
self.add_message(msgid, line, None, args)
819817

820-
@staticmethod
821-
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
818+
def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]:
822819
"""Return file description (tuple of module name, file path, base name) from
823820
given file path.
824821
825822
This method is used for creating suitable file description for _check_files when the
826823
source is standard input.
827824
"""
825+
if _is_ignored_file(
826+
filepath,
827+
self.config.ignore,
828+
self.config.ignore_patterns,
829+
self.config.ignore_paths,
830+
):
831+
return
832+
828833
try:
829834
# Note that this function does not really perform an
830835
# __import__ but may raise an ImportError exception, which
@@ -833,7 +838,7 @@ def _get_file_descr_from_stdin(filepath: str) -> FileItem:
833838
except ImportError:
834839
modname = os.path.splitext(os.path.basename(filepath))[0]
835840

836-
return FileItem(modname, filepath, filepath)
841+
yield FileItem(modname, filepath, filepath)
837842

838843
def _iterate_file_descrs(
839844
self, files_or_modules: Sequence[str]

tests/test_self.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,20 @@ def test_ignore_pattern_recursive(self, ignore_pattern_value: str) -> None:
11281128
code=0,
11291129
)
11301130

1131+
def test_ignore_pattern_from_stdin(self) -> None:
1132+
"""Test if linter ignores standard input if the filename matches the ignore pattern."""
1133+
with mock.patch("pylint.lint.pylinter._read_stdin", return_value="import os\n"):
1134+
self._runtest(
1135+
[
1136+
"--from-stdin",
1137+
"mymodule.py",
1138+
"--disable=all",
1139+
"--enable=unused-import",
1140+
"--ignore-patterns=mymodule.py",
1141+
],
1142+
code=0,
1143+
)
1144+
11311145
@pytest.mark.parametrize("ignore_path_value", [".*ignored.*", ".*failing.*"])
11321146
def test_ignore_path_recursive(self, ignore_path_value: str) -> None:
11331147
"""Tests recursive run of linter ignoring directory using --ignore-path parameter.

0 commit comments

Comments
 (0)