Skip to content

Commit cad50bc

Browse files
Improve file discovery for directories that are not packages (#9768)
1 parent c835139 commit cad50bc

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

doc/user_guide/usage/run.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ On versions below 2.15, specifying a directory that is not an explicit package
1818
mydir/__init__.py:1:0: F0010: error while code parsing: Unable to load file mydir/__init__.py:
1919
[Errno 2] No such file or directory: 'mydir/__init__.py' (parse-error)
2020

21-
Thus, on versions before 2.15, or when dealing with certain edge cases that have not yet been solved,
22-
using the ``--recursive=y`` option allows for linting a namespace package::
21+
Thus, on versions before 2.15 using the ``--recursive=y`` option allows for linting a namespace package::
2322

2423
pylint --recursive=y mydir mymodule mypackage
2524

doc/whatsnew/fragments/9764.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve file discovery for directories that are not python packages.
2+
3+
Closes #9764

pylint/lint/expand_modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def expand_modules(
122122
)
123123
except ImportError:
124124
# Might not be acceptable, don't crash.
125-
is_namespace = False
125+
is_namespace = not os.path.exists(filepath)
126126
is_directory = os.path.isdir(something)
127127
else:
128128
is_namespace = modutils.is_namespace(spec)

tests/lint/unittest_expand_modules.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ def test__is_in_ignore_list_re_match() -> None:
114114
"path": INIT_PATH,
115115
}
116116

117+
# A directory that is not a python package.
118+
REPORTERS_PATH = Path(__file__).parent.parent / "reporters"
119+
test_reporters = { # pylint: disable=consider-using-namedtuple-or-dataclass
120+
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
121+
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
122+
"name": "reporters.unittest_json_reporter",
123+
"isarg": False,
124+
"basepath": str(REPORTERS_PATH / "__init__.py"),
125+
"basename": "reporters",
126+
},
127+
str(REPORTERS_PATH / "unittest_reporting.py"): {
128+
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
129+
"name": "reporters.unittest_reporting",
130+
"isarg": False,
131+
"basepath": str(REPORTERS_PATH / "__init__.py"),
132+
"basename": "reporters",
133+
},
134+
}
135+
117136

118137
def _list_expected_package_modules(
119138
deduplicating: bool = False,
@@ -174,6 +193,7 @@ class Checker(BaseChecker):
174193
for module in _list_expected_package_modules()
175194
},
176195
),
196+
([str(Path(__file__).parent.parent / "reporters")], test_reporters),
177197
],
178198
)
179199
@set_config(ignore_paths="")

0 commit comments

Comments
 (0)