Skip to content

Commit e1f3eea

Browse files
committed
fix incorrect basenames caused by a lack of __init__.py
The problem was caused by pylint-dev/astroid#2589, changing the package discovery. Since tests/reporters/ didn't have `__init__.py`, the new package discovery would resolve module names as one level higher than desired (so, `unittest_reporting` instead of `reporters.unittest_reporting`). We're fixing this, by adding the appropriate `__init__.py`. We're also adding the `ModuleDescriptionDict` corresponding to the new `__init__.py` to the `expand_modules` tests. The changes in `expand_modules.py` are small optimizations mostly done in the process of understanding what is happening there.
1 parent e03d048 commit e1f3eea

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

pylint/lint/expand_modules.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,28 @@ def expand_modules(
136136
is_namespace = modutils.is_namespace(spec)
137137
is_directory = modutils.is_directory(spec)
138138
if not is_namespace:
139-
if filepath in result:
140-
# Always set arg flag if module explicitly given.
141-
result[filepath]["isarg"] = True
142-
else:
143-
result[filepath] = {
144-
"path": filepath,
145-
"name": modname,
146-
"isarg": True,
147-
"basepath": filepath,
148-
"basename": modname,
149-
"isignored": False,
150-
}
139+
default: ModuleDescriptionDict = {
140+
"path": filepath,
141+
"name": modname,
142+
"isarg": True,
143+
"basepath": filepath,
144+
"basename": modname,
145+
"isignored": False,
146+
}
147+
result.setdefault(filepath, default)["isarg"] = True
151148
has_init = (
152-
not (modname.endswith(".__init__") or modname == "__init__")
153-
and os.path.basename(filepath) == "__init__.py"
149+
modparts[-1] != "__init__" and os.path.basename(filepath) == "__init__.py"
154150
)
155151
if has_init or is_namespace or is_directory:
156152
for subfilepath in modutils.get_module_files(
157-
os.path.dirname(filepath) or ".", ignore_list, list_all=is_namespace
153+
os.path.dirname(filepath) or ".", [], list_all=is_namespace
158154
):
159155
subfilepath = os.path.normpath(subfilepath)
160156
if filepath == subfilepath:
161157
continue
162-
if _is_in_ignore_list_re(
163-
os.path.basename(subfilepath), ignore_list_re
164-
) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re):
158+
if _is_ignored_file(
159+
subfilepath, ignore_list, ignore_list_re, ignore_list_paths_re
160+
):
165161
result[subfilepath] = {
166162
"path": subfilepath,
167163
"name": "",

tests/lint/unittest_expand_modules.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,28 @@ def test__is_in_ignore_list_re_match() -> None:
127127
# A directory that is not a python package.
128128
REPORTERS_PATH = Path(__file__).parent.parent / "reporters"
129129
test_reporters = { # pylint: disable=consider-using-namedtuple-or-dataclass
130-
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
131-
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
132-
"name": "reporters.unittest_json_reporter",
133-
"isarg": False,
130+
str(REPORTERS_PATH / "__init__.py"): {
131+
"basename": "reporters",
134132
"basepath": str(REPORTERS_PATH / "__init__.py"),
133+
"isarg": True,
134+
"name": "reporters",
135+
"path": str(REPORTERS_PATH / "__init__.py"),
136+
"isignored": False,
137+
},
138+
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
135139
"basename": "reporters",
140+
"basepath": str(REPORTERS_PATH / "__init__.py"),
141+
"isarg": False,
142+
"name": "reporters.unittest_json_reporter",
143+
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
136144
"isignored": False,
137145
},
138146
str(REPORTERS_PATH / "unittest_reporting.py"): {
147+
"basename": "reporters",
148+
"basepath": str(REPORTERS_PATH / "__init__.py"),
149+
"isarg": False,
139150
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
140151
"name": "reporters.unittest_reporting",
141-
"isarg": False,
142-
"basepath": str(REPORTERS_PATH / "__init__.py"),
143-
"basename": "reporters",
144152
"isignored": False,
145153
},
146154
}

tests/reporters/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)