summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Blessing <33834216+cblessing24@users.noreply.github.com>2022-09-03 20:44:32 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-09-06 23:33:59 +0200
commitd476a8bd2557ec1a075e4a8ac630469700ba753a (patch)
tree13e719c4a00a12af398807685a3ae0782f81de1a
parent7a1fbb9f9aa8d1938ad768ff3d0403aa7d0c4bfb (diff)
downloadpylint-git-d476a8bd2557ec1a075e4a8ac630469700ba753a.tar.gz
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 <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--doc/whatsnew/fragments/4354.bugfix3
-rw-r--r--pylint/lint/pylinter.py17
-rw-r--r--tests/test_self.py14
3 files changed, 28 insertions, 6 deletions
diff --git a/doc/whatsnew/fragments/4354.bugfix b/doc/whatsnew/fragments/4354.bugfix
new file mode 100644
index 000000000..09caf8d13
--- /dev/null
+++ b/doc/whatsnew/fragments/4354.bugfix
@@ -0,0 +1,3 @@
+Fix ignored files being linted when passed on stdin.
+
+Closes #4354
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index 472b3b030..8fa47ffe8 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -660,9 +660,7 @@ class PyLinter(
# 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)
@@ -817,14 +815,21 @@ class PyLinter(
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
@@ -833,7 +838,7 @@ class PyLinter(
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]
diff --git a/tests/test_self.py b/tests/test_self.py
index 53c9fb11b..f2587c5c7 100644
--- a/tests/test_self.py
+++ b/tests/test_self.py
@@ -1128,6 +1128,20 @@ a.py:1:4: E0001: Parsing failed: 'invalid syntax (<unknown>, line 1)' (syntax-er
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.