Skip to content

Commit

Permalink
fix incorrect basenames caused by a lack of __init__.py
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
temyurchenko committed Feb 17, 2025
1 parent e03d048 commit e1f3eea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
32 changes: 14 additions & 18 deletions pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,28 @@ def expand_modules(
is_namespace = modutils.is_namespace(spec)
is_directory = modutils.is_directory(spec)
if not is_namespace:
if filepath in result:
# Always set arg flag if module explicitly given.
result[filepath]["isarg"] = True
else:
result[filepath] = {
"path": filepath,
"name": modname,
"isarg": True,
"basepath": filepath,
"basename": modname,
"isignored": False,
}
default: ModuleDescriptionDict = {
"path": filepath,
"name": modname,
"isarg": True,
"basepath": filepath,
"basename": modname,
"isignored": False,
}
result.setdefault(filepath, default)["isarg"] = True
has_init = (
not (modname.endswith(".__init__") or modname == "__init__")
and os.path.basename(filepath) == "__init__.py"
modparts[-1] != "__init__" and os.path.basename(filepath) == "__init__.py"
)
if has_init or is_namespace or is_directory:
for subfilepath in modutils.get_module_files(
os.path.dirname(filepath) or ".", ignore_list, list_all=is_namespace
os.path.dirname(filepath) or ".", [], list_all=is_namespace
):
subfilepath = os.path.normpath(subfilepath)
if filepath == subfilepath:
continue
if _is_in_ignore_list_re(
os.path.basename(subfilepath), ignore_list_re
) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re):
if _is_ignored_file(
subfilepath, ignore_list, ignore_list_re, ignore_list_paths_re
):
result[subfilepath] = {
"path": subfilepath,
"name": "",
Expand Down
22 changes: 15 additions & 7 deletions tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,28 @@ def test__is_in_ignore_list_re_match() -> None:
# A directory that is not a python package.
REPORTERS_PATH = Path(__file__).parent.parent / "reporters"
test_reporters = { # pylint: disable=consider-using-namedtuple-or-dataclass
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
"name": "reporters.unittest_json_reporter",
"isarg": False,
str(REPORTERS_PATH / "__init__.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": True,
"name": "reporters",
"path": str(REPORTERS_PATH / "__init__.py"),
"isignored": False,
},
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": False,
"name": "reporters.unittest_json_reporter",
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
"isignored": False,
},
str(REPORTERS_PATH / "unittest_reporting.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": False,
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
"name": "reporters.unittest_reporting",
"isarg": False,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
"isignored": False,
},
}
Expand Down
Empty file added tests/reporters/__init__.py
Empty file.

0 comments on commit e1f3eea

Please sign in to comment.