Skip to content

Commit 81f0f2e

Browse files
github-actions[bot]Pierre-SassoulasDanielNoord
authored
[Backport maintenance/3.0.x] [bugfix] Find files with ./ as input with a __init__.py file (#9286)
* [bugfix] Find files with ./ as input with a __init__.py file (#9211) Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com> Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> (cherry picked from commit abdb874)
1 parent 7f01d83 commit 81f0f2e

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

doc/whatsnew/fragments/9210.bugfix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a bug where pylint was unable to walk recursively through a directory if the
2+
directory has an `__init__.py` file.
3+
4+
Closes #9210

pylint/lint/expand_modules.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ def expand_modules(
144144
)
145145
if has_init or is_namespace or is_directory:
146146
for subfilepath in modutils.get_module_files(
147-
os.path.dirname(filepath), ignore_list, list_all=is_namespace
147+
os.path.dirname(filepath) or ".", ignore_list, list_all=is_namespace
148148
):
149+
subfilepath = os.path.normpath(subfilepath)
149150
if filepath == subfilepath:
150151
continue
151152
if _is_in_ignore_list_re(

tests/lint/unittest_expand_modules.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
from __future__ import annotations
66

7+
import copy
8+
import os
79
import re
10+
from collections.abc import Iterator
11+
from contextlib import contextmanager
812
from pathlib import Path
913

1014
import pytest
@@ -28,7 +32,8 @@ def test__is_in_ignore_list_re_match() -> None:
2832

2933
TEST_DIRECTORY = Path(__file__).parent.parent
3034
INIT_PATH = str(TEST_DIRECTORY / "lint/__init__.py")
31-
EXPAND_MODULES = str(TEST_DIRECTORY / "lint/unittest_expand_modules.py")
35+
EXPAND_MODULES_BASE = "unittest_expand_modules.py"
36+
EXPAND_MODULES = str(TEST_DIRECTORY / "lint" / EXPAND_MODULES_BASE)
3237
this_file = {
3338
"basename": "lint.unittest_expand_modules",
3439
"basepath": EXPAND_MODULES,
@@ -37,6 +42,14 @@ def test__is_in_ignore_list_re_match() -> None:
3742
"path": EXPAND_MODULES,
3843
}
3944

45+
this_file_relative_to_parent = {
46+
"basename": "lint.unittest_expand_modules",
47+
"basepath": EXPAND_MODULES_BASE,
48+
"isarg": True,
49+
"name": "lint.unittest_expand_modules",
50+
"path": EXPAND_MODULES_BASE,
51+
}
52+
4053
this_file_from_init = {
4154
"basename": "lint",
4255
"basepath": INIT_PATH,
@@ -117,6 +130,27 @@ def _list_expected_package_modules(
117130
)
118131

119132

133+
def _list_expected_package_modules_relative() -> tuple[dict[str, object], ...]:
134+
"""Generates reusable list of modules for our package with relative path input."""
135+
abs_result = copy.deepcopy(_list_expected_package_modules())
136+
for item in abs_result:
137+
assert isinstance(item["basepath"], str)
138+
assert isinstance(item["path"], str)
139+
item["basepath"] = os.path.relpath(item["basepath"], str(Path(__file__).parent))
140+
item["path"] = os.path.relpath(item["path"], str(Path(__file__).parent))
141+
return abs_result
142+
143+
144+
@contextmanager
145+
def pushd(path: Path) -> Iterator[None]:
146+
prev = os.getcwd()
147+
os.chdir(path)
148+
try:
149+
yield
150+
finally:
151+
os.chdir(prev)
152+
153+
120154
class TestExpandModules(CheckerTestCase):
121155
"""Test the expand_modules function while allowing options to be set."""
122156

@@ -159,6 +193,40 @@ def test_expand_modules(
159193
assert modules == expected
160194
assert not errors
161195

196+
@pytest.mark.parametrize(
197+
"files_or_modules,expected",
198+
[
199+
(
200+
[Path(__file__).name],
201+
{this_file_relative_to_parent["path"]: this_file_relative_to_parent},
202+
),
203+
(
204+
["./"],
205+
{
206+
module["path"]: module # pylint: disable=unsubscriptable-object
207+
for module in _list_expected_package_modules_relative()
208+
},
209+
),
210+
],
211+
)
212+
@set_config(ignore_paths="")
213+
def test_expand_modules_relative_path(
214+
self, files_or_modules: list[str], expected: dict[str, ModuleDescriptionDict]
215+
) -> None:
216+
"""Test expand_modules with the default value of ignore-paths and relative path as input."""
217+
ignore_list: list[str] = []
218+
ignore_list_re: list[re.Pattern[str]] = []
219+
with pushd(Path(__file__).parent):
220+
modules, errors = expand_modules(
221+
files_or_modules,
222+
[],
223+
ignore_list,
224+
ignore_list_re,
225+
self.linter.config.ignore_paths,
226+
)
227+
assert modules == expected
228+
assert not errors
229+
162230
@pytest.mark.parametrize(
163231
"files_or_modules,expected",
164232
[

0 commit comments

Comments
 (0)