From 4b496f289288c036fb06b0cfd8766db64395f80d Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 4 Jul 2024 13:25:21 -0400 Subject: [PATCH] Improve file discovery for directories that are not packages --- doc/user_guide/usage/run.rst | 3 +-- doc/whatsnew/fragments/9764.bugfix | 3 +++ pylint/lint/expand_modules.py | 2 +- tests/lint/unittest_expand_modules.py | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 doc/whatsnew/fragments/9764.bugfix diff --git a/doc/user_guide/usage/run.rst b/doc/user_guide/usage/run.rst index e7462a8f5d..8c4d520cea 100644 --- a/doc/user_guide/usage/run.rst +++ b/doc/user_guide/usage/run.rst @@ -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 diff --git a/doc/whatsnew/fragments/9764.bugfix b/doc/whatsnew/fragments/9764.bugfix new file mode 100644 index 0000000000..6ee88070a1 --- /dev/null +++ b/doc/whatsnew/fragments/9764.bugfix @@ -0,0 +1,3 @@ +Improve file discovery for directories that are not python packages. + +Closes #9764 diff --git a/pylint/lint/expand_modules.py b/pylint/lint/expand_modules.py index 04e7018843..f40bdeea5b 100644 --- a/pylint/lint/expand_modules.py +++ b/pylint/lint/expand_modules.py @@ -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) diff --git a/tests/lint/unittest_expand_modules.py b/tests/lint/unittest_expand_modules.py index 34133d759b..e8d38e6a57 100644 --- a/tests/lint/unittest_expand_modules.py +++ b/tests/lint/unittest_expand_modules.py @@ -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, @@ -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="")