Skip to content

Commit

Permalink
Discover .pyi files (#9241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Feb 23, 2024
1 parent 9dbf3df commit 7300ed2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions .pyenchant_pylint_custom_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ proc
py
pyenchant
pyfile
pyi
pylint
pylintdict
pylintrc
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/9097.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Discover ``.pyi`` files when linting.

These can be ignored with the ``ignore-patterns`` setting.

Closes #9097
8 changes: 4 additions & 4 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/lint/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
1 change: 1 addition & 0 deletions tests/regrtest_data/pyi/foo.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo = 1

0 comments on commit 7300ed2

Please sign in to comment.