Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve file discovery for directories that are not packages #9768

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/user_guide/usage/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ On versions below 2.15, specifying a directory that is not an explicit package
mydir/__init__.py:1:0: F0010: error while code parsing: Unable to load file mydir/__init__.py:
[Errno 2] No such file or directory: 'mydir/__init__.py' (parse-error)

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

pylint --recursive=y mydir mymodule mypackage

Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9764.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve file discovery for directories that are not python packages.

Closes #9764
2 changes: 1 addition & 1 deletion pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def expand_modules(
)
except ImportError:
# Might not be acceptable, don't crash.
is_namespace = False
is_namespace = not os.path.exists(filepath)
is_directory = os.path.isdir(something)
else:
is_namespace = modutils.is_namespace(spec)
Expand Down
20 changes: 20 additions & 0 deletions tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ def test__is_in_ignore_list_re_match() -> None:
"path": INIT_PATH,
}

# 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,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
},
str(REPORTERS_PATH / "unittest_reporting.py"): {
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
"name": "reporters.unittest_reporting",
"isarg": False,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
},
}


def _list_expected_package_modules(
deduplicating: bool = False,
Expand Down Expand Up @@ -174,6 +193,7 @@ class Checker(BaseChecker):
for module in _list_expected_package_modules()
},
),
([str(Path(__file__).parent.parent / "reporters")], test_reporters),
],
)
@set_config(ignore_paths="")
Expand Down
Loading