Prevent import of unrealted packages during test collection #741
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.
#729 describes an issue where pytest-asyncio imports packages (i.e.
__init__.py
modules) which are not imported without pytest-asyncio. The imported packages could be test packages and non-test packages. This leads to two issues:pytest_collectstart
hook just before the collection phase. Pytest isn't equipped to deal with import errors or test skips in this hook, so the import can trigger an INTERNALERROR which aborts the test run completely.torch
package)This PR "tackles" the problem by monkey patching the Package.collect method, which delays any collection errors to the actual collection phase and addresses problem 1.
The patch also adds a call to
collector.funcnamefilter
, in order to skip packages that don't contain test code. The fixture function for a package-scoped loop is installed into a temporary Python module in the current package rather than into the__init__.py
to avoid further problems. This solves problem 2.This code is expected to be a temporary bandaid. The collection logic has changed in pytest 8, so that each Package only collects its current directory rather than all subdirectories. This will allow pytest-asyncio to get rid of the code that tracks sub-package paths and assembles fixture IDs based on them.
Moreover, there are efforts by the pytest developers to allow fixtures to be attached to a certain node, which would eliminate the monkey patching.