From e6abab64a9cf7565a2615739c112b9295bd4d885 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Wed, 17 Apr 2024 23:51:24 -0400 Subject: [PATCH 01/12] Add Python 3.10.8 to tox and GitHub Actions. --- .github/workflows/main.yml | 2 +- tox.ini | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9f69165c..7649683d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,7 +62,7 @@ jobs: strategy: matrix: os: [ubuntu, windows] - python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] + python-version: ['3.8', '3.9', '3.10.8', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 diff --git a/tox.ini b/tox.ini index 7bab7350..ce4330e4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 3.14.0 -envlist = py38, py39, py310, py311, py312, pytest-min, docs +envlist = py38, py39, py3108, py310, py311, py312, pytest-min, docs isolated_build = true passenv = CI @@ -25,6 +25,14 @@ commands = make test allowlist_externals = make +[testenv: py3108] +# Python 3.10.8 is the most recent version that triggers +# https://github.com/pytest-dev/pytest-asyncio/issues/757. +# +# We need to set basepython explicitly here because if we just added "py3109" to +# envlist, tox would interpret that as "any py310". +basepython = python3.10.8 + [testenv:docs] extras = docs deps = @@ -39,6 +47,7 @@ allowlist_externals = python = 3.8: py38, pytest-min 3.9: py39 + 3.10.9: py3109 3.10: py310 3.11: py311 3.12: py312 From 4f7306377eefce65f7c087ba35400d8ed7454742 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Wed, 17 Apr 2024 23:17:54 -0400 Subject: [PATCH 02/12] Ignore DeprecationWarnings here, too. --- pytest_asyncio/plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index b2c5c59e..3fc9b520 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -891,7 +891,9 @@ def wrap_in_sync( @functools.wraps(func) def inner(*args, **kwargs): coro = func(*args, **kwargs) - _loop = asyncio.get_event_loop() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + _loop = asyncio.get_event_loop() task = asyncio.ensure_future(coro, loop=_loop) try: _loop.run_until_complete(task) From fc507d6e0ed8d3e98cffaee9f17b6a420435c753 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 00:31:55 -0400 Subject: [PATCH 03/12] One more. --- pytest_asyncio/plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 3fc9b520..e9f7dc6e 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -673,7 +673,9 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No # subsequent tests from side-effects. We close this loop before restoring # the old loop to avoid ResourceWarnings. try: - asyncio.get_event_loop().close() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + asyncio.get_event_loop().close() except RuntimeError: pass asyncio.set_event_loop(old_loop) From 1c8fa8c5c48b5bb8995810324d75729c06240ee8 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 00:30:54 -0400 Subject: [PATCH 04/12] Fix up port_with_event_loop_finalizer(). --- tests/async_fixtures/test_async_fixtures_with_finalizer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/async_fixtures/test_async_fixtures_with_finalizer.py b/tests/async_fixtures/test_async_fixtures_with_finalizer.py index 699ac49d..3e4ad94e 100644 --- a/tests/async_fixtures/test_async_fixtures_with_finalizer.py +++ b/tests/async_fixtures/test_async_fixtures_with_finalizer.py @@ -1,5 +1,6 @@ import asyncio import functools +import warnings import pytest @@ -33,7 +34,11 @@ async def port_afinalizer(): # RuntimeError is raised if task is created on a different loop await finalizer - asyncio.get_event_loop().run_until_complete(port_afinalizer()) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + loop = asyncio.get_event_loop() + + loop.run_until_complete(port_afinalizer()) worker = asyncio.ensure_future(asyncio.sleep(0.2)) request.addfinalizer(functools.partial(port_finalizer, worker)) From bdfba4bfd44ed9fb867ec61def891c60cc22d98b Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 01:00:18 -0400 Subject: [PATCH 05/12] Appease the YAML linter. --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7649683d..efca24bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,7 +62,7 @@ jobs: strategy: matrix: os: [ubuntu, windows] - python-version: ['3.8', '3.9', '3.10.8', '3.10', '3.11', '3.12'] + python-version: ['3.8', '3.9', 3.10.8, '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 From 0f594818a432b403b0f1b3cd5d44a2da5e681cce Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 01:04:59 -0400 Subject: [PATCH 06/12] Fix 3109/3108 typo. --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index ce4330e4..6e40ca36 100644 --- a/tox.ini +++ b/tox.ini @@ -29,8 +29,8 @@ allowlist_externals = # Python 3.10.8 is the most recent version that triggers # https://github.com/pytest-dev/pytest-asyncio/issues/757. # -# We need to set basepython explicitly here because if we just added "py3109" to -# envlist, tox would interpret that as "any py310". +# We need to set basepython explicitly here because if we just added "py3108" to +# envlist, tox would interpret that as "py310". basepython = python3.10.8 [testenv:docs] From 9cef7ab88ddffe34433c8aacd8ea80c986bcf2b0 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 01:05:52 -0400 Subject: [PATCH 07/12] Fix GH Action mapping. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 6e40ca36..b00e28ad 100644 --- a/tox.ini +++ b/tox.ini @@ -47,7 +47,7 @@ allowlist_externals = python = 3.8: py38, pytest-min 3.9: py39 - 3.10.9: py3109 + 3.10.8: py3108 3.10: py310 3.11: py311 3.12: py312 From 11418e33a2c4abbae251034b82f882cd90df0a3b Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 18 Apr 2024 01:32:22 -0400 Subject: [PATCH 08/12] Remove extra space. I don't think this matters, but maybe... --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index b00e28ad..00f335d8 100644 --- a/tox.ini +++ b/tox.ini @@ -25,7 +25,7 @@ commands = make test allowlist_externals = make -[testenv: py3108] +[testenv:py3108] # Python 3.10.8 is the most recent version that triggers # https://github.com/pytest-dev/pytest-asyncio/issues/757. # From 48831c20bf7a61ee8a69c87ae554f5279a14ea36 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 25 Apr 2024 18:01:28 -0400 Subject: [PATCH 09/12] asyncio.run(port_afinalizer()) --- tests/async_fixtures/test_async_fixtures_with_finalizer.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/async_fixtures/test_async_fixtures_with_finalizer.py b/tests/async_fixtures/test_async_fixtures_with_finalizer.py index 3e4ad94e..b4d2ac94 100644 --- a/tests/async_fixtures/test_async_fixtures_with_finalizer.py +++ b/tests/async_fixtures/test_async_fixtures_with_finalizer.py @@ -1,6 +1,5 @@ import asyncio import functools -import warnings import pytest @@ -34,11 +33,7 @@ async def port_afinalizer(): # RuntimeError is raised if task is created on a different loop await finalizer - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - loop = asyncio.get_event_loop() - - loop.run_until_complete(port_afinalizer()) + asyncio.run(port_afinalizer()) worker = asyncio.ensure_future(asyncio.sleep(0.2)) request.addfinalizer(functools.partial(port_finalizer, worker)) From 0f43e3ed6592d67c575c728b9e2b697ddbace313 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 25 Apr 2024 18:17:20 -0400 Subject: [PATCH 10/12] Deduplicate simplefilter snippet. --- pytest_asyncio/plugin.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index e9f7dc6e..7dbb238d 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -657,9 +657,7 @@ def _patched_collect(): def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[None]: old_loop_policy = asyncio.get_event_loop_policy() try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - old_loop = asyncio.get_event_loop() + old_loop = _get_event_loop_no_warn() except RuntimeError: old_loop = None asyncio.set_event_loop_policy(policy) @@ -673,9 +671,7 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No # subsequent tests from side-effects. We close this loop before restoring # the old loop to avoid ResourceWarnings. try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - asyncio.get_event_loop().close() + _get_event_loop_no_warn().close() except RuntimeError: pass asyncio.set_event_loop(old_loop) @@ -765,9 +761,7 @@ def pytest_fixture_setup( ) policy = asyncio.get_event_loop_policy() try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - old_loop = policy.get_event_loop() + old_loop = _get_event_loop_no_warn(policy) is_pytest_asyncio_loop = getattr(old_loop, "__pytest_asyncio", False) if old_loop is not loop and not is_pytest_asyncio_loop: old_loop.close() @@ -829,9 +823,7 @@ def _restore_policy(): # Close any event loop associated with the old loop policy # to avoid ResourceWarnings in the _provide_clean_event_loop finalizer try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - loop = previous_policy.get_event_loop() + loop = _get_event_loop_no_warn(previous_policy) except RuntimeError: loop = None if loop: @@ -853,6 +845,17 @@ def _provide_clean_event_loop() -> None: policy.set_event_loop(new_loop) +def _get_event_loop_no_warn( + policy: Optional[AbstractEventLoopPolicy] = None, +) -> asyncio.AbstractEventLoop: + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + if policy is not None: + return policy.get_event_loop() + else: + return asyncio.get_event_loop() + + @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_pyfunc_call(pyfuncitem: Function) -> Optional[object]: """ @@ -893,9 +896,7 @@ def wrap_in_sync( @functools.wraps(func) def inner(*args, **kwargs): coro = func(*args, **kwargs) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - _loop = asyncio.get_event_loop() + _loop = _get_event_loop_no_warn() task = asyncio.ensure_future(coro, loop=_loop) try: _loop.run_until_complete(task) From 90e61373dcc3d69361fee104ac3f7eac159fd6ff Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Thu, 25 Apr 2024 18:24:59 -0400 Subject: [PATCH 11/12] Revert GitHub Actions and Tox changes. We can only reproduce this on specific Python 3.10.x patch versions, but we can't easily test those in CI. Give up the attempt. --- .github/workflows/main.yml | 2 +- tox.ini | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index efca24bc..9f69165c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,7 +62,7 @@ jobs: strategy: matrix: os: [ubuntu, windows] - python-version: ['3.8', '3.9', 3.10.8, '3.10', '3.11', '3.12'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 diff --git a/tox.ini b/tox.ini index 00f335d8..7bab7350 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 3.14.0 -envlist = py38, py39, py3108, py310, py311, py312, pytest-min, docs +envlist = py38, py39, py310, py311, py312, pytest-min, docs isolated_build = true passenv = CI @@ -25,14 +25,6 @@ commands = make test allowlist_externals = make -[testenv:py3108] -# Python 3.10.8 is the most recent version that triggers -# https://github.com/pytest-dev/pytest-asyncio/issues/757. -# -# We need to set basepython explicitly here because if we just added "py3108" to -# envlist, tox would interpret that as "py310". -basepython = python3.10.8 - [testenv:docs] extras = docs deps = @@ -47,7 +39,6 @@ allowlist_externals = python = 3.8: py38, pytest-min 3.9: py39 - 3.10.8: py3108 3.10: py310 3.11: py311 3.12: py312 From 8f9388f6865c91fac374d83ef9cbfe149fc817b5 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Tue, 14 May 2024 19:17:54 +0200 Subject: [PATCH 12/12] [docs] Add changelog entry. Signed-off-by: Michael Seifert --- docs/source/reference/changelog.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index 198baf68..21d6a391 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -2,6 +2,10 @@ Changelog ========= +0.23.7 (UNRELEASED) +=================== +- Silence deprecation warnings about unclosed event loops that occurred with certain CPython patch releases `#817 `_ + 0.23.6 (2024-03-19) =================== - Fix compatibility with pytest 8.2 `#800 `_