Fix importlib mode shadowing an installed package with a namespace package (#13257)#14709
Open
vismaytiwari wants to merge 1 commit into
Open
Conversation
…ckage (pytest-dev#13257) Since pytest-dev#12752, importing a test module under `--import-mode=importlib` eagerly builds its parent packages. When a test file's rootdir-relative path reuses the name of an installed package -- for example `mypkg/tests/test_foo.py` living next to the real `mypkg/mypkg` package -- pytest bound the outer directory as a namespace package under that name in `sys.modules`, shadowing the real package. `from mypkg import version` in the tests then failed with "cannot import name ... (unknown location)". Defer to the real package when a directory that would become a namespace parent already contains an importable package of the same name (the real package lives inside that directory). A test directory named after a standard-library module lives elsewhere and is still shadowed by a namespace package, so test_importlib_same_name_as_stl and the pytest-dev#12592 behavior are preserved.
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.
Fixes #13257.
Since #12752,
--import-mode=importlibeagerly builds a test module's parent packages. When a test file's rootdir-relative path reuses the name of an installed package — e.g.mypkg/tests/test_foo.pyliving next to the realmypkg/mypkgpackage — pytest bound the outer directory as a namespace package under that name insys.modules, shadowing the real installed package.from mypkg import versionin the tests then failed with:This regressed in 8.3.4 (the reporter bisected it to 3d3ec57 / #12752, which fixed #12592).
Fix
_import_module_using_specnow defers to the real package when the directory it would otherwise turn into a namespace parent already contains an importable package of the same fully-qualified name (i.e. the real package lives inside that directory).The "inside that directory" check is what keeps this narrow: a test directory named after a standard-library module (
math,time, …) has its real module living elsewhere, so it is still shadowed by a namespace package as before —test_importlib_same_name_as_stland the #12592 behavior are unchanged.Testing
test_ns_import_shadowed_by_installed_package_13257.main, and passes with this change.testing/test_pathlib.py(incl. the stdlib-shadow andKeyErrorfired fromimportlibduring collection when running with--import-mode=importlibon a directory containing a same-named one #12592 tests),testing/acceptance_test.py, andtesting/test_collection.pyall pass.