Skip to content

Commit

Permalink
Improve file discovery for directories that are not packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jul 4, 2024
1 parent c835139 commit 4b496f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
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

0 comments on commit 4b496f2

Please sign in to comment.