[PR #13053/28e1e25a backport][8.3.x] fix: Resolved an ImportError
crash when using --import-mode=importlib
#13054
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.
This is a backport of PR #13053 as merged into main (28e1e25).
Regression in #12716
In short:
PathFinder.find_spec
received the argument/cow/moo
but loaded/cow/moo/moo.py
instead.Trigger conditions:
/cow/moo/moo.py
exists (a file and directory with the same name)./cow/moo/test_moo.py
exists (test case resides in the directory).When pytest loads test files in
importlib
mode, it continues recursive loading upward:cow.moo
, it should return a namespace but unexpectedly returns a module.cow.moo.moo
, it should return a module but unexpectedly returns a namespace.Complete example: [GitHub repository](https://github.com/dongfangtianyu/pytest_importlib_issue)
main.py
: Reproduces the error.debug.py
: Demonstrates the behavior ofPathFinder.find_spec
.Context:
#12592 (comment)
#12592 (comment)