Skip to content

Commit 6b277e9

Browse files
Discover .pyi files
1 parent 51c28bc commit 6b277e9

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
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
@@ -585,8 +585,8 @@ def prepare_checkers(self) -> list[BaseChecker]:
585585
def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> bool:
586586
"""Returns whether a module should be checked.
587587
588-
This implementation returns True for all python source file, indicating
589-
that all files should be linted.
588+
This implementation returns True for all python source files (.py and .pyi),
589+
indicating that all files should be linted.
590590
591591
Subclasses may override this method to indicate that modules satisfying
592592
certain conditions should not be linted.
@@ -600,7 +600,7 @@ def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> b
600600
"""
601601
if is_argument:
602602
return True
603-
return path.endswith(".py")
603+
return path.endswith(("py", ".pyi"))
604604

605605
# pylint: enable=unused-argument
606606

@@ -647,7 +647,7 @@ def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]:
647647
yield from (
648648
os.path.join(root, file)
649649
for file in files
650-
if file.endswith(".py")
650+
if file.endswith((".py", ".pyi"))
651651
)
652652
else:
653653
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)