Skip to content

Commit d025472

Browse files
Support globbing patterns for source-roots (#8281)
Co-authored-by: Andreas Finkler <3929834+DudeNr33@users.noreply.github.com>
1 parent 3f8fd45 commit d025472

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

doc/whatsnew/fragments/8290.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add globbing pattern support for ``--source-roots``.
2+
3+
Closes #8290

pylint/config/argument.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re
1616
import sys
1717
from collections.abc import Callable
18+
from glob import glob
1819
from typing import Any, Pattern, Sequence, Tuple, Union
1920

2021
from pylint import interfaces
@@ -88,13 +89,13 @@ def _path_transformer(value: str) -> str:
8889
return os.path.expandvars(os.path.expanduser(value))
8990

9091

91-
def _paths_csv_transformer(value: str) -> Sequence[str]:
92+
def _glob_paths_csv_transformer(value: str) -> Sequence[str]:
9293
"""Transforms a comma separated list of paths while expanding user and
93-
variables.
94+
variables and glob patterns.
9495
"""
9596
paths: list[str] = []
9697
for path in _csv_transformer(value):
97-
paths.append(os.path.expandvars(os.path.expanduser(path)))
98+
paths.extend(glob(_path_transformer(path), recursive=True))
9899
return paths
99100

100101

@@ -148,7 +149,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
148149
"confidence": _confidence_transformer,
149150
"non_empty_string": _non_empty_string_transformer,
150151
"path": _path_transformer,
151-
"paths_csv": _paths_csv_transformer,
152+
"glob_paths_csv": _glob_paths_csv_transformer,
152153
"py_version": _py_version_transformer,
153154
"regexp": _regex_transformer,
154155
"regexp_csv": _regexp_csv_transfomer,

pylint/config/option.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _py_version_validator(_: Any, name: str, value: Any) -> tuple[int, int, int]
117117
"string": utils._unquote,
118118
"int": int,
119119
"float": float,
120-
"paths_csv": _csv_validator,
120+
"glob_paths_csv": _csv_validator,
121121
"regexp": lambda pattern: re.compile(pattern or ""),
122122
"regexp_csv": _regexp_csv_validator,
123123
"regexp_paths_csv": _regexp_paths_csv_validator,
@@ -164,7 +164,7 @@ def _validate(value: Any, optdict: Any, name: str = "") -> Any:
164164
# pylint: disable=no-member
165165
class Option(optparse.Option):
166166
TYPES = optparse.Option.TYPES + (
167-
"paths_csv",
167+
"glob_paths_csv",
168168
"regexp",
169169
"regexp_csv",
170170
"regexp_paths_csv",
@@ -177,7 +177,7 @@ class Option(optparse.Option):
177177
)
178178
ATTRS = optparse.Option.ATTRS + ["hide", "level"]
179179
TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER)
180-
TYPE_CHECKER["paths_csv"] = _csv_validator
180+
TYPE_CHECKER["glob_paths_csv"] = _csv_validator
181181
TYPE_CHECKER["regexp"] = _regexp_validator
182182
TYPE_CHECKER["regexp_csv"] = _regexp_csv_validator
183183
TYPE_CHECKER["regexp_paths_csv"] = _regexp_paths_csv_validator

pylint/lint/base_options.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,13 @@ def _make_linter_options(linter: PyLinter) -> Options:
346346
(
347347
"source-roots",
348348
{
349-
"type": "paths_csv",
349+
"type": "glob_paths_csv",
350350
"metavar": "<path>[,<path>...]",
351351
"default": (),
352-
"help": "Add paths to the list of the source roots. The source root is an absolute "
353-
"path or a path relative to the current working directory used to "
354-
"determine a package namespace for modules located under the source root.",
352+
"help": "Add paths to the list of the source roots. Supports globbing patterns. "
353+
"The source root is an absolute path or a path relative to the current working "
354+
"directory used to determine a package namespace for modules located under the "
355+
"source root.",
355356
},
356357
),
357358
(

pylint/pyreverse/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@
238238
(
239239
"source-roots",
240240
{
241-
"type": "paths_csv",
241+
"type": "glob_paths_csv",
242242
"metavar": "<path>[,<path>...]",
243243
"default": (),
244-
"help": "Add paths to the list of the source roots. The source root is an absolute "
245-
"path or a path relative to the current working directory used to "
246-
"determine a package namespace for modules located under the source root.",
244+
"help": "Add paths to the list of the source roots. Supports globbing patterns. The "
245+
"source root is an absolute path or a path relative to the current working directory "
246+
"used to determine a package namespace for modules located under the source root.",
247247
},
248248
),
249249
)

tests/lint/unittest_lint.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,20 @@ def test_recursive_ignore(ignore_parameter: str, ignore_parameter_value: str) ->
12081208
assert module in linted_file_paths
12091209

12101210

1211+
def test_source_roots_globbing() -> None:
1212+
run = Run(
1213+
[
1214+
"--source-roots",
1215+
join(REGRTEST_DATA_DIR, "pep420", "basic", "*"),
1216+
join(REGRTEST_DATA_DIR, "pep420", "basic", "project"),
1217+
],
1218+
exit=False,
1219+
)
1220+
assert run.linter.config.source_roots == [
1221+
join(REGRTEST_DATA_DIR, "pep420", "basic", "project")
1222+
]
1223+
1224+
12111225
def test_recursive_implicit_namespace() -> None:
12121226
run = Run(
12131227
[

0 commit comments

Comments
 (0)