gh-157144: Let unittest discovery and regrtest find tests inside archives on sys.path - #157145
Draft
gpshead wants to merge 3 commits into
Draft
gh-157144: Let unittest discovery and regrtest find tests inside archives on sys.path#157145gpshead wants to merge 3 commits into
gpshead wants to merge 3 commits into
Conversation
When the standard library is a zip archive on sys.path, the test package is not a directory: findtests() failed with NotADirectoryError from os.listdir() and load_package_tests() failed with "Start directory is not importable" from unittest discovery. Fall back to listing the test modules with pkgutil.iter_modules() through the import system.
gpshead
commented
Sep 8, 2026
Comment on lines
+204
to
+211
| def _load_package_tests_from_import_system(pkg_dir, loader, pattern, | ||
| package=None): | ||
| """Load the tests of a package which is not on the file system. | ||
|
|
||
| Mimic unittest discovery for a package that unittest cannot walk, | ||
| e.g. a package inside a zip archive, by asking the import system for | ||
| its modules. | ||
| """ |
Member
Author
There was a problem hiding this comment.
if we want to support this at all, this is probably too targeted. why not fix unittest discovery itself instead of just doing this in regrtest?
… regrtest Make unittest.TestLoader.discover() work when the start directory is inside an archive on sys.path, such as a zip file: the archive's path entry finder lists the entries through pkgutil.iter_modules() and tells modules and packages apart, and the rest of discovery (module naming, importing, the load_tests protocol) already works on such paths. With that, test.support.load_package_tests() needs no special case and goes back to plain discovery. libregrtest's findtests() keeps a small fallback for listing the top level test directory, now keyed on os.path.isdir() rather than on the exception os.listdir() raises, which is NotADirectoryError on POSIX but FileNotFoundError on Windows.
Documentation build overview
|
gpshead
commented
Sep 8, 2026
Member
Author
There was a problem hiding this comment.
drop this one, the unittest news entry is sufficient.
gpshead
commented
Sep 8, 2026
|
|
||
| Loads tests from a single file, or a directories' __init__.py when | ||
| passed the directory. | ||
| passed the directory. *kind* is 'module' or 'package' for an entry |
Member
Author
There was a problem hiding this comment.
lets use something like defined module level _CONSTANTS rather than a inline magic values. make their names clearly indicate when something refers to an archive member or not and do not use None to indicate anything.
Address review: describe what _find_tests() hands _find_test_path() with module-level constants (_FILESYSTEM_ENTRY, _FILESYSTEM_MODULE, _FILESYSTEM_PACKAGE, _ARCHIVE_MODULE, _ARCHIVE_PACKAGE) instead of inline strings and None, and split the archive helper into a predicate and a listing function so that neither returns None to mean anything. Drop the regrtest NEWS entry; the unittest one covers the change.
serhiy-storchaka
self-requested a review
September 9, 2026 14:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the
testpackage lives inside an archive onsys.path(a zipped standard library, a zipapp), the test suite cannot run:unittest.TestLoader.discover()requires a real directory and raises "Start directory is not importable", which breaks every test package whoseload_tests()usestest.support.load_package_tests().libregrtest.findtests()callsos.listdir()on it, which raisesNotADirectoryError(FileNotFoundErroron Windows).Fix this in
unittestrather than in regrtest: when the start directory is not on the file system but a path hook claims it,discover()lists it through the archive's path entry finder withpkgutil.iter_modules(), which also distinguishes modules from packages. Module naming, importing and theload_testsprotocol already work on such paths.findtests()gets the samepkgutilfallback for the top-level listing, keyed onos.path.isdir().(thank you Claude Fable 5.1)