Skip to content

Support globbing pattern for input specification #8312

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions doc/user_guide/usage/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ If the analyzed sources use implicit namespace packages (PEP 420), the source ro
be specified using the ``--source-roots`` option. Otherwise, the package names are
detected incorrectly, since implicit namespace packages don't contain an ``__init__.py``.

With globbing pattern
---------------------

It is also possible to specify both directories and files using globbing patterns::

pylint [options] packages/*/src

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo this should be in the On module packages or directories paragraph

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet it's also applicable to On files - it can't be in both paragraphs at the same time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO renaming the headline to Globbing support would be good, as it makes it a bit clearer that this section just informs you about the possibility to use glob patterns.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globbing permits to handle file/module/package, the distinction could be removed entirely maybe ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, that section of docs should be refactored, yet I don't have enough knowledge to do that confidently

Command line options
--------------------

Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8310.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support globbing pattern for input specification.

Closes #8310
13 changes: 11 additions & 2 deletions pylint/config/config_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import annotations

import sys
from glob import glob
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -118,6 +120,13 @@ def _config_initialization(
# Link the base Namespace object on the current directory
linter._directory_namespaces[Path(".").resolve()] = (linter.config, {})

# parsed_args_list should now only be a list of files/directories to lint.
# parsed_args_list should now only be a list of inputs to lint.
# All other options have been removed from the list.
return parsed_args_list
return list(
chain.from_iterable(
# NOTE: 'or [arg]' is needed to maintain backwards compatibility
# TODO: remove 'or [arg]' in 3.0
glob(arg, recursive=True) or [arg]
for arg in parsed_args_list
)
)
15 changes: 13 additions & 2 deletions tests/lint/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,8 +1220,6 @@ def test_recursive_implicit_namespace() -> None:
],
exit=False,
)
run.linter.set_reporter(testutils.GenericTestReporter())
run.linter.check([join(REGRTEST_DATA_DIR, "pep420", "basic")])
assert run.linter.file_state.base_name == "namespace.package"


Expand All @@ -1241,6 +1239,19 @@ def test_recursive_implicit_namespace_wrapper() -> None:
assert run.linter.reporter.messages == []


def test_globbing() -> None:
run = Run(
[
"--verbose",
"--source-roots",
join(REGRTEST_DATA_DIR, "pep420", "basic", "project"),
join(REGRTEST_DATA_DIR, "pep420", "basic", "project", "**", "__init__.py"),
],
exit=False,
)
assert run.linter.file_state.base_name == "namespace.package.__init__"


def test_relative_imports(initialized_linter: PyLinter) -> None:
"""Regression test for https://github.com/PyCQA/pylint/issues/3651"""
linter = initialized_linter
Expand Down
7 changes: 7 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,13 @@ def test_recursive(self) -> None:
code=0,
)

def test_recursive_globbing(self) -> None:
"""Tests if running linter over directory using --recursive=y and globbing"""
self._runtest(
[join(HERE, "regrtest_data", "d?rectory", "subd*"), "--recursive=y"],
code=0,
)

@pytest.mark.parametrize("ignore_value", ["ignored_subdirectory", "failing.py"])
def test_ignore_recursive(self, ignore_value: str) -> None:
"""Tests recursive run of linter ignoring directory using --ignore parameter.
Expand Down