Skip to content

Commit a19aadc

Browse files
committed
Alter fallback for source-roots
1 parent 8630cfd commit a19aadc

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

pylint/lint/expand_modules.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections.abc import Sequence
1010
from pathlib import Path
1111
from re import Pattern
12+
from typing import cast
1213

1314
from astroid import modutils
1415

@@ -24,7 +25,9 @@ def _is_package_cb(inner_path: str, parts: list[str]) -> bool:
2425
)
2526

2627

27-
def discover_package_path(modulepath: str, source_roots: Sequence[str]) -> str:
28+
def discover_package_path(
29+
modulepath: str, source_roots: Sequence[str]
30+
) -> Sequence[str]:
2831
"""Discover package path from one its modules and source roots."""
2932
dirname = os.path.realpath(os.path.expanduser(modulepath))
3033
if not os.path.isdir(dirname):
@@ -34,17 +37,21 @@ def discover_package_path(modulepath: str, source_roots: Sequence[str]) -> str:
3437
for source_root in source_roots:
3538
source_root = os.path.realpath(os.path.expanduser(source_root))
3639
if os.path.commonpath([source_root, dirname]) == source_root:
37-
return source_root
40+
#return cast(Sequence[str], [source_root])
41+
return [source_root]
42+
43+
if len(source_roots) != 0:
44+
return source_roots
3845

3946
# Fall back to legacy discovery by looking for __init__.py upwards as
4047
# it's the only way given that source root was not found or was not provided
4148
while True:
4249
if not os.path.exists(os.path.join(dirname, "__init__.py")):
43-
return dirname
50+
return [dirname]
4451
old_dirname = dirname
4552
dirname = os.path.dirname(dirname)
4653
if old_dirname == dirname:
47-
return os.getcwd()
54+
return [os.getcwd()]
4855

4956

5057
def _is_in_ignore_list_re(element: str, ignore_list_re: list[Pattern[str]]) -> bool:
@@ -88,8 +95,8 @@ def expand_modules(
8895
something, ignore_list, ignore_list_re, ignore_list_paths_re
8996
):
9097
continue
91-
module_package_path = discover_package_path(something, source_roots)
92-
additional_search_path = [".", module_package_path, *path]
98+
module_package_paths = discover_package_path(something, source_roots)
99+
additional_search_path = [".", *module_package_paths, *path]
93100
if os.path.exists(something):
94101
# this is a file or a directory
95102
try:

pylint/lint/pylinter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ def check(self, files_or_modules: Sequence[str]) -> None:
667667
extra_packages_paths = list(
668668
dict.fromkeys(
669669
[
670-
discover_package_path(file_or_module, self.config.source_roots)
671-
for file_or_module in files_or_modules
670+
path for file_or_module in files_or_modules
671+
for path in discover_package_path(file_or_module, self.config.source_roots)
672672
]
673673
).keys()
674674
)

pylint/pyreverse/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ def run(self, args: list[str]) -> int:
302302
print(self.help())
303303
return 1
304304
extra_packages_paths = list(
305-
{discover_package_path(arg, self.config.source_roots) for arg in args}
305+
{path for arg in args
306+
for path in discover_package_path(arg, self.config.source_roots)}
306307
)
307308
with augmented_sys_path(extra_packages_paths):
308309
project = project_from_files(

tests/checkers/unittest_imports.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_relative_beyond_top_level_four(capsys: CaptureFixture[str]) -> None:
9595
def test_wildcard_import_init(self) -> None:
9696
context_file = os.path.join(REGR_DATA, "dummy_wildcard.py")
9797

98-
with augmented_sys_path([discover_package_path(context_file, [])]):
98+
with augmented_sys_path(discover_package_path(context_file, [])):
9999
module = astroid.MANAGER.ast_from_module_name("init_wildcard", context_file)
100100
import_from = module.body[0]
101101

@@ -105,7 +105,7 @@ def test_wildcard_import_init(self) -> None:
105105
def test_wildcard_import_non_init(self) -> None:
106106
context_file = os.path.join(REGR_DATA, "dummy_wildcard.py")
107107

108-
with augmented_sys_path([discover_package_path(context_file, [])]):
108+
with augmented_sys_path(discover_package_path(context_file, [])):
109109
module = astroid.MANAGER.ast_from_module_name("wildcard", context_file)
110110
import_from = module.body[0]
111111
msg = MessageTest(

tests/lint/unittest_lint.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def test_one_arg(fake_path: list[str], case: list[str]) -> None:
133133
expected = [join(chroot, "a"), *fake_path]
134134

135135
extra_sys_paths = [
136-
expand_modules.discover_package_path(arg, []) for arg in case
136+
path for arg in case
137+
for path in expand_modules.discover_package_path(arg, [])
137138
]
138139

139140
assert sys.path == fake_path
@@ -157,7 +158,8 @@ def test_two_similar_args(fake_path: list[str], case: list[str]) -> None:
157158
expected = [join(chroot, "a"), *fake_path]
158159

159160
extra_sys_paths = [
160-
expand_modules.discover_package_path(arg, []) for arg in case
161+
path for arg in case
162+
for path in expand_modules.discover_package_path(arg, [])
161163
]
162164

163165
assert sys.path == fake_path
@@ -183,7 +185,8 @@ def test_more_args(fake_path: list[str], case: list[str]) -> None:
183185
] + fake_path
184186

185187
extra_sys_paths = [
186-
expand_modules.discover_package_path(arg, []) for arg in case
188+
path for arg in case
189+
for path in expand_modules.discover_package_path(arg, [])
187190
]
188191

189192
assert sys.path == fake_path
@@ -1242,7 +1245,7 @@ def test_import_sibling_module_from_namespace(initialized_linter: PyLinter) -> N
12421245
"""
12431246
)
12441247
os.chdir("namespace")
1245-
extra_sys_paths = [expand_modules.discover_package_path(tmpdir, [])]
1248+
extra_sys_paths = expand_modules.discover_package_path(tmpdir, [])
12461249

12471250
# Add the parent directory to sys.path
12481251
with lint.augmented_sys_path(extra_sys_paths):
@@ -1267,7 +1270,7 @@ def test_lint_namespace_package_under_dir_on_path(initialized_linter: PyLinter)
12671270
with tempdir() as tmpdir:
12681271
create_files(["namespace_on_path/submodule1.py"])
12691272
os.chdir(tmpdir)
1270-
extra_sys_paths = [expand_modules.discover_package_path(tmpdir, [])]
1273+
extra_sys_paths = expand_modules.discover_package_path(tmpdir, [])
12711274
with lint.augmented_sys_path(extra_sys_paths):
12721275
linter.check(["namespace_on_path"])
12731276
assert linter.file_state.base_name == "namespace_on_path"

tests/pyreverse/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _astroid_wrapper(
7777
) -> Module:
7878
return func(modname)
7979

80-
with augmented_sys_path([discover_package_path(module, [])]):
80+
with augmented_sys_path(discover_package_path(module, [])):
8181
project = project_from_files([module], _astroid_wrapper, project_name=name)
8282
return project
8383

tests/pyreverse/test_main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_project_root_in_sys_path() -> None:
6161
"""Test the context manager adds the project root directory to sys.path.
6262
This should happen when pyreverse is run from any directory.
6363
"""
64-
with augmented_sys_path([discover_package_path(TEST_DATA_DIR, [])]):
64+
with augmented_sys_path(discover_package_path(TEST_DATA_DIR, [])):
6565
assert sys.path == [PROJECT_ROOT_DIR]
6666

6767

0 commit comments

Comments
 (0)