Skip to content

Commit

Permalink
[Backport maintenance/3.0.x] [bugfix] Find files with ./ as input wit…
Browse files Browse the repository at this point in the history
…h a __init__.py file (#9286)

* [bugfix] Find files with ./ as input with a __init__.py file (#9211)

Co-authored-by: Pierre Sassoulas <[email protected]>
Co-authored-by: Daniël van Noord <[email protected]>
(cherry picked from commit abdb874)
  • Loading branch information
github-actions[bot] authored Dec 7, 2023
1 parent 7f01d83 commit 81f0f2e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9210.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a bug where pylint was unable to walk recursively through a directory if the
directory has an `__init__.py` file.

Closes #9210
3 changes: 2 additions & 1 deletion pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ def expand_modules(
)
if has_init or is_namespace or is_directory:
for subfilepath in modutils.get_module_files(
os.path.dirname(filepath), ignore_list, list_all=is_namespace
os.path.dirname(filepath) or ".", ignore_list, list_all=is_namespace
):
subfilepath = os.path.normpath(subfilepath)
if filepath == subfilepath:
continue
if _is_in_ignore_list_re(
Expand Down
70 changes: 69 additions & 1 deletion tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

from __future__ import annotations

import copy
import os
import re
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path

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

TEST_DIRECTORY = Path(__file__).parent.parent
INIT_PATH = str(TEST_DIRECTORY / "lint/__init__.py")
EXPAND_MODULES = str(TEST_DIRECTORY / "lint/unittest_expand_modules.py")
EXPAND_MODULES_BASE = "unittest_expand_modules.py"
EXPAND_MODULES = str(TEST_DIRECTORY / "lint" / EXPAND_MODULES_BASE)
this_file = {
"basename": "lint.unittest_expand_modules",
"basepath": EXPAND_MODULES,
Expand All @@ -37,6 +42,14 @@ def test__is_in_ignore_list_re_match() -> None:
"path": EXPAND_MODULES,
}

this_file_relative_to_parent = {
"basename": "lint.unittest_expand_modules",
"basepath": EXPAND_MODULES_BASE,
"isarg": True,
"name": "lint.unittest_expand_modules",
"path": EXPAND_MODULES_BASE,
}

this_file_from_init = {
"basename": "lint",
"basepath": INIT_PATH,
Expand Down Expand Up @@ -117,6 +130,27 @@ def _list_expected_package_modules(
)


def _list_expected_package_modules_relative() -> tuple[dict[str, object], ...]:
"""Generates reusable list of modules for our package with relative path input."""
abs_result = copy.deepcopy(_list_expected_package_modules())
for item in abs_result:
assert isinstance(item["basepath"], str)
assert isinstance(item["path"], str)
item["basepath"] = os.path.relpath(item["basepath"], str(Path(__file__).parent))
item["path"] = os.path.relpath(item["path"], str(Path(__file__).parent))
return abs_result


@contextmanager
def pushd(path: Path) -> Iterator[None]:
prev = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev)


class TestExpandModules(CheckerTestCase):
"""Test the expand_modules function while allowing options to be set."""

Expand Down Expand Up @@ -159,6 +193,40 @@ def test_expand_modules(
assert modules == expected
assert not errors

@pytest.mark.parametrize(
"files_or_modules,expected",
[
(
[Path(__file__).name],
{this_file_relative_to_parent["path"]: this_file_relative_to_parent},
),
(
["./"],
{
module["path"]: module # pylint: disable=unsubscriptable-object
for module in _list_expected_package_modules_relative()
},
),
],
)
@set_config(ignore_paths="")
def test_expand_modules_relative_path(
self, files_or_modules: list[str], expected: dict[str, ModuleDescriptionDict]
) -> None:
"""Test expand_modules with the default value of ignore-paths and relative path as input."""
ignore_list: list[str] = []
ignore_list_re: list[re.Pattern[str]] = []
with pushd(Path(__file__).parent):
modules, errors = expand_modules(
files_or_modules,
[],
ignore_list,
ignore_list_re,
self.linter.config.ignore_paths,
)
assert modules == expected
assert not errors

@pytest.mark.parametrize(
"files_or_modules,expected",
[
Expand Down

0 comments on commit 81f0f2e

Please sign in to comment.