From 9f29f28af5c7cf77c6ebeb75d20eb2e2f6a72106 Mon Sep 17 00:00:00 2001 From: Christoph Blessing <33834216+cblessing24@users.noreply.github.com> Date: Fri, 22 Jul 2022 12:02:01 +0200 Subject: [PATCH 1/4] Test if ignored files passed on stdin are ignored --- tests/test_self.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_self.py b/tests/test_self.py index 8401195867..b09c07278e 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -1113,6 +1113,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. From 6e35080d891628b33560d0671f827168f704fe36 Mon Sep 17 00:00:00 2001 From: Christoph Blessing <33834216+cblessing24@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:45:45 +0200 Subject: [PATCH 2/4] Do not lint ignored file when passed on stdin Previously pylint would lint a file passed on stdin even if the user meant to ignore the file. This commit fixes that issue. --- pylint/lint/pylinter.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index bacce60e11..ea75122157 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -641,6 +641,13 @@ def check(self, files_or_modules: Sequence[str] | str) -> None: filepath = files_or_modules[0] with fix_import_path(files_or_modules): + if _is_ignored_file( + filepath, + self.config.ignore, + self.config.ignore_patterns, + self.config.ignore_paths, + ): + return self._check_files( functools.partial(self.get_ast, data=_read_stdin()), [self._get_file_descr_from_stdin(filepath)], From 30d85dfbc529efd0a2a7f351632710e31229f1c7 Mon Sep 17 00:00:00 2001 From: Christoph Blessing <33834216+cblessing24@users.noreply.github.com> Date: Fri, 22 Jul 2022 14:04:44 +0200 Subject: [PATCH 3/4] Add news fragment for issue #4354 --- doc/whatsnew/fragments/4354.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/4354.bugfix diff --git a/doc/whatsnew/fragments/4354.bugfix b/doc/whatsnew/fragments/4354.bugfix new file mode 100644 index 0000000000..09caf8d139 --- /dev/null +++ b/doc/whatsnew/fragments/4354.bugfix @@ -0,0 +1,3 @@ +Fix ignored files being linted when passed on stdin. + +Closes #4354 From 9da9b5d44bac02e9c7dded54cfda3c1dde1667bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 2 Sep 2022 10:34:42 +0200 Subject: [PATCH 4/4] Small refactor --- pylint/lint/pylinter.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 472b3b0302..8fa47ffe83 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -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) @@ -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 @@ -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]