Skip to content

Commit 5597aae

Browse files
Fix source root not recognized (#10036) (#10083)
(cherry picked from commit be53085) Co-authored-by: Julfried <51880314+Julfried@users.noreply.github.com>
1 parent ea8ed7e commit 5597aae

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

doc/whatsnew/fragments/10026.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes the issue with --source-root option not working when the source files are in a subdirectory of the source root (e.g. when using a /src layout).
2+
3+
Closes #10026

pylint/lint/expand_modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def discover_package_path(modulepath: str, source_roots: Sequence[str]) -> str:
3333
# Look for a source root that contains the module directory
3434
for source_root in source_roots:
3535
source_root = os.path.realpath(os.path.expanduser(source_root))
36-
if os.path.commonpath([source_root, dirname]) == source_root:
36+
if os.path.commonpath([source_root, dirname]) in [dirname, source_root]:
3737
return source_root
3838

3939
# Fall back to legacy discovery by looking for __init__.py upwards as

tests/pyreverse/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ def test_project_root_in_sys_path() -> None:
6565
assert sys.path == [PROJECT_ROOT_DIR]
6666

6767

68+
def test_discover_package_path_source_root_as_parent(tmp_path: Any) -> None:
69+
"""Test discover_package_path when source root is a parent of the module."""
70+
# Create this temporary structure:
71+
# /tmp_path/
72+
# └── project/
73+
# └── my-package/
74+
# └── __init__.py
75+
project_dir = tmp_path / "project"
76+
package_dir = project_dir / "mypackage"
77+
package_dir.mkdir(parents=True)
78+
(package_dir / "__init__.py").touch()
79+
80+
# Test with project_dir as source root (parent of package)
81+
result = discover_package_path(str(package_dir), [str(project_dir)])
82+
assert result == str(project_dir)
83+
84+
85+
def test_discover_package_path_source_root_as_child(tmp_path: Any) -> None:
86+
"""Test discover_package_path when source root is a child of the module."""
87+
# Create this temporary structure:
88+
# /tmp_path/
89+
# └── project/
90+
# └── src/
91+
# └── my-package/
92+
# └── __init__.py
93+
project_dir = tmp_path / "project"
94+
src_dir = project_dir / "src"
95+
package_dir = src_dir / "mypackage"
96+
package_dir.mkdir(parents=True)
97+
(package_dir / "__init__.py").touch()
98+
99+
# Test with src_dir as source root (child of project)
100+
result = discover_package_path(str(project_dir), [str(src_dir)])
101+
assert result == str(src_dir)
102+
103+
68104
@mock.patch("pylint.pyreverse.main.Linker", new=mock.MagicMock())
69105
@mock.patch("pylint.pyreverse.main.DiadefsHandler", new=mock.MagicMock())
70106
@mock.patch("pylint.pyreverse.main.writer")

0 commit comments

Comments
 (0)