Skip to content

Commit

Permalink
[fix] Avoid trying to install scoped event loops for unknown test col…
Browse files Browse the repository at this point in the history
…lector types.

Signed-off-by: Michael Seifert <[email protected]>
  • Loading branch information
seifertm committed Jan 16, 2024
1 parent 256ef7d commit 463ce98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
0.23.4 (UNRELEASED)
===================
- pytest-asyncio no longer imports additional, unrelated packages during test collection `#729 <https://github.com/pytest-dev/pytest-asyncio/issues/729>`_
- Addresses further issues that caused an internal pytest error during test collection

Known issues
------------
Expand Down
40 changes: 19 additions & 21 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,21 +608,7 @@ def scoped_event_loop(
# know it exists. We work around this by attaching the fixture function to the
# collected Python object, where it will be picked up by pytest.Class.collect()
# or pytest.Module.collect(), respectively
if type(collector) is Module:
# Accessing Module.obj triggers a module import executing module-level
# statements. A module-level pytest.skip statement raises the "Skipped"
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
# kwargs is missing. These cases are handled correctly when they happen inside
# Collector.collect(), but this hook runs before the actual collect call.
# Therefore, we monkey patch Module.collect to add the scoped fixture to the
# module before it runs the actual collection.
def _patched_collect():
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
return collector.__original_collect()

collector.__original_collect = collector.collect
collector.collect = _patched_collect
elif type(collector) is Package:
if type(collector) is Package:

def _patched_collect():
# When collector is a Package, collector.obj is the package's
Expand All @@ -648,12 +634,24 @@ def _patched_collect():

collector.__original_collect = collector.collect
collector.collect = _patched_collect
else:
pyobject = collector.obj
# If the collected module is a DoctestTextfile, collector.obj is None
if pyobject is None:
return
pyobject.__pytest_asyncio_scoped_event_loop = scoped_event_loop
elif isinstance(collector, Module):
# Accessing Module.obj triggers a module import executing module-level
# statements. A module-level pytest.skip statement raises the "Skipped"
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
# kwargs is missing. These cases are handled correctly when they happen inside
# Collector.collect(), but this hook runs before the actual collect call.
# Therefore, we monkey patch Module.collect to add the scoped fixture to the
# module before it runs the actual collection.
def _patched_collect():
# If the collected module is a DoctestTextfile, collector.obj is None
if collector.obj is not None:
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
return collector.__original_collect()

collector.__original_collect = collector.collect
collector.collect = _patched_collect
elif isinstance(collector, Class):
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop


def _removesuffix(s: str, suffix: str) -> str:
Expand Down

0 comments on commit 463ce98

Please sign in to comment.