Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover .pyi files #9241

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading