From 7300ed2687701bbcb02eac9baa7f5c2197cf1397 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 23 Feb 2024 18:08:33 -0500 Subject: [PATCH] Discover `.pyi` files (#9241) --- .pyenchant_pylint_custom_dict.txt | 1 + doc/whatsnew/fragments/9097.feature | 5 +++++ pylint/lint/pylinter.py | 8 ++++---- tests/lint/unittest_lint.py | 22 ++++++++++++++++++++++ tests/regrtest_data/pyi/foo.pyi | 1 + 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 doc/whatsnew/fragments/9097.feature create mode 100644 tests/regrtest_data/pyi/foo.pyi diff --git a/.pyenchant_pylint_custom_dict.txt b/.pyenchant_pylint_custom_dict.txt index 712ab70cca..fd4fed00c3 100644 --- a/.pyenchant_pylint_custom_dict.txt +++ b/.pyenchant_pylint_custom_dict.txt @@ -261,6 +261,7 @@ proc py pyenchant pyfile +pyi pylint pylintdict pylintrc diff --git a/doc/whatsnew/fragments/9097.feature b/doc/whatsnew/fragments/9097.feature new file mode 100644 index 0000000000..51874b3604 --- /dev/null +++ b/doc/whatsnew/fragments/9097.feature @@ -0,0 +1,5 @@ +Discover ``.pyi`` files when linting. + +These can be ignored with the ``ignore-patterns`` setting. + +Closes #9097 diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 541d05f8ef..30250154e6 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -584,8 +584,8 @@ def prepare_checkers(self) -> list[BaseChecker]: def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> bool: """Returns whether a module should be checked. - This implementation returns True for all python source file, indicating - that all files should be linted. + This implementation returns True for all python source files (.py and .pyi), + indicating that all files should be linted. Subclasses may override this method to indicate that modules satisfying certain conditions should not be linted. @@ -599,7 +599,7 @@ def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> b """ if is_argument: return True - return path.endswith(".py") + return path.endswith((".py", ".pyi")) # pylint: enable=unused-argument @@ -646,7 +646,7 @@ def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]: yield from ( os.path.join(root, file) for file in files - if file.endswith(".py") + if file.endswith((".py", ".pyi")) ) else: yield something diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py index 7e4b4dfcca..00e410b469 100644 --- a/tests/lint/unittest_lint.py +++ b/tests/lint/unittest_lint.py @@ -1047,6 +1047,28 @@ def test_by_module_statement_value(initialized_linter: PyLinter) -> None: assert module_stats["statement"] == linter2.stats.statement +def test_finds_pyi_file() -> None: + run = Run( + [join(REGRTEST_DATA_DIR, "pyi")], + exit=False, + ) + assert run.linter.current_file is not None + assert run.linter.current_file.endswith("foo.pyi") + + +def test_recursive_finds_pyi_file() -> None: + run = Run( + [ + "--recursive", + "y", + join(REGRTEST_DATA_DIR, "pyi"), + ], + exit=False, + ) + assert run.linter.current_file is not None + assert run.linter.current_file.endswith("foo.pyi") + + @pytest.mark.parametrize( "ignore_parameter,ignore_parameter_value", [ diff --git a/tests/regrtest_data/pyi/foo.pyi b/tests/regrtest_data/pyi/foo.pyi new file mode 100644 index 0000000000..c4e5bcc800 --- /dev/null +++ b/tests/regrtest_data/pyi/foo.pyi @@ -0,0 +1 @@ +foo = 1