Skip to content

Commit 7300ed2

Browse files
Discover .pyi files (#9241)
1 parent 9dbf3df commit 7300ed2

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

.pyenchant_pylint_custom_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ proc
261261
py
262262
pyenchant
263263
pyfile
264+
pyi
264265
pylint
265266
pylintdict
266267
pylintrc

doc/whatsnew/fragments/9097.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Discover ``.pyi`` files when linting.
2+
3+
These can be ignored with the ``ignore-patterns`` setting.
4+
5+
Closes #9097

pylint/lint/pylinter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ def prepare_checkers(self) -> list[BaseChecker]:
584584
def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> bool:
585585
"""Returns whether a module should be checked.
586586
587-
This implementation returns True for all python source file, indicating
588-
that all files should be linted.
587+
This implementation returns True for all python source files (.py and .pyi),
588+
indicating that all files should be linted.
589589
590590
Subclasses may override this method to indicate that modules satisfying
591591
certain conditions should not be linted.
@@ -599,7 +599,7 @@ def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> b
599599
"""
600600
if is_argument:
601601
return True
602-
return path.endswith(".py")
602+
return path.endswith((".py", ".pyi"))
603603

604604
# pylint: enable=unused-argument
605605

@@ -646,7 +646,7 @@ def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]:
646646
yield from (
647647
os.path.join(root, file)
648648
for file in files
649-
if file.endswith(".py")
649+
if file.endswith((".py", ".pyi"))
650650
)
651651
else:
652652
yield something

tests/lint/unittest_lint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,28 @@ def test_by_module_statement_value(initialized_linter: PyLinter) -> None:
10471047
assert module_stats["statement"] == linter2.stats.statement
10481048

10491049

1050+
def test_finds_pyi_file() -> None:
1051+
run = Run(
1052+
[join(REGRTEST_DATA_DIR, "pyi")],
1053+
exit=False,
1054+
)
1055+
assert run.linter.current_file is not None
1056+
assert run.linter.current_file.endswith("foo.pyi")
1057+
1058+
1059+
def test_recursive_finds_pyi_file() -> None:
1060+
run = Run(
1061+
[
1062+
"--recursive",
1063+
"y",
1064+
join(REGRTEST_DATA_DIR, "pyi"),
1065+
],
1066+
exit=False,
1067+
)
1068+
assert run.linter.current_file is not None
1069+
assert run.linter.current_file.endswith("foo.pyi")
1070+
1071+
10501072
@pytest.mark.parametrize(
10511073
"ignore_parameter,ignore_parameter_value",
10521074
[

tests/regrtest_data/pyi/foo.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo = 1

0 commit comments

Comments
 (0)