Skip to content

Commit c82bc4e

Browse files
authored
Merge pull request #17 from nolar/fix-for-pytest-asyncio
Fix for pytest asyncio >= 1.1.0
2 parents 69e3301 + 4060aad commit c82bc4e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

looptime/plugin.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,18 @@ def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.Fix
171171
should_patch = _should_patch(fixturedef, request)
172172
is_loop = isinstance(result, asyncio.BaseEventLoop)
173173
is_runner = False if sys.version_info < (3, 11) else isinstance(result, asyncio.Runner)
174-
if should_patch and (is_loop or is_runner):
174+
is_bp_runner = False if sys.version_info < (3, 11) else isinstance(result, asyncio.Runner)
175+
176+
# We avoid extra dependencies, but pytest-asyncio>=1.1.0 uses it, and we need to detect it.
177+
# TODO: remove when Python 3.10 is dropped (≈October 2026).
178+
try:
179+
from backports.asyncio.runner import Runner as bp_Runner
180+
except ImportError:
181+
is_bp_runner = False
182+
else:
183+
is_bp_runner = isinstance(result, bp_Runner)
184+
185+
if should_patch and (is_loop or is_runner or is_bp_runner):
175186

176187
# Populate the helper mapper of names-to-scopes, as used in the test hook below.
177188
if EVENT_LOOP_SCOPES not in request.session.stash:
@@ -193,6 +204,10 @@ def pytest_fixture_setup(fixturedef: pytest.FixtureDef[Any], request: pytest.Fix
193204
loop = result.get_loop()
194205
if isinstance(loop, asyncio.BaseEventLoop):
195206
patchers.patch_event_loop(loop, _enabled=False)
207+
elif is_bp_runner: # TODO: drop this branch with Python 3.10
208+
loop = result.get_loop()
209+
if isinstance(loop, asyncio.BaseEventLoop):
210+
patchers.patch_event_loop(loop, _enabled=False)
196211

197212
return result
198213

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dev = [
5252
"coverage",
5353
"coveralls",
5454
"pytest",
55-
"pytest-asyncio<1.1.0",
55+
"pytest-asyncio",
5656
"pytest-cov",
5757
"pytest-mock",
5858
]

0 commit comments

Comments
 (0)