From e1facbb9d2413c520f39657f4b1ac271f7e58584 Mon Sep 17 00:00:00 2001 From: Rob Dailey Date: Tue, 31 Oct 2023 21:41:39 -0400 Subject: [PATCH 1/2] Migrated from deprecated `event_loop` fixture --- CHANGELOG.md | 3 +++ tests/test_contextvars.py | 33 ++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbc966e4..d8dff611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/ - `structlog.threadlocal.tmp_bind()` now also works with `BoundLoggerLazyProxy` (in other words: before anything is bound to a bound logger). +- Migrated tests away from depreciated `event_loop` usage to per recommendations from `pytest-asyncio`. + [pytest-dev/pytest-asyncio#638](https://github.com/pytest-dev/pytest-asyncio/issues/638) + ## [23.2.0](https://github.com/hynek/structlog/compare/23.1.0...23.2.0) - 2023-10-09 diff --git a/tests/test_contextvars.py b/tests/test_contextvars.py index 12535845..d9426006 100644 --- a/tests/test_contextvars.py +++ b/tests/test_contextvars.py @@ -33,11 +33,12 @@ def _clear_contextvars(): class TestContextvars: - async def test_bind(self, event_loop): + async def test_bind(self): """ Binding a variable causes it to be included in the result of merge_contextvars. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -45,11 +46,12 @@ async def coro(): assert {"a": 1, "b": 2} == await event_loop.create_task(coro()) - async def test_multiple_binds(self, event_loop): + async def test_multiple_binds(self): """ Multiple calls to bind_contextvars accumulate values instead of replacing them. But they override redefined ones. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1, c=3) @@ -63,11 +65,12 @@ async def coro(): "d": 4, } == await event_loop.create_task(coro()) - async def test_reset(self, event_loop): + async def test_reset(self): """ reset_contextvars allows resetting contexvars to previously-set values. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -87,10 +90,11 @@ async def nested_coro(): await event_loop.create_task(coro()) - async def test_nested_async_bind(self, event_loop): + async def test_nested_async_bind(self): """ Context is passed correctly between "nested" concurrent operations. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -102,22 +106,24 @@ async def nested_coro(): assert {"a": 1, "b": 2, "c": 3} == await event_loop.create_task(coro()) - async def test_merge_works_without_bind(self, event_loop): + async def test_merge_works_without_bind(self): """ merge_contextvars returns values as normal even when there has been no previous calls to bind_contextvars. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): return merge_contextvars(None, None, {"b": 2}) assert {"b": 2} == await event_loop.create_task(coro()) - async def test_merge_overrides_bind(self, event_loop): + async def test_merge_overrides_bind(self): """ Variables included in merge_contextvars override previously bound variables. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -125,11 +131,12 @@ async def coro(): assert {"a": 111, "b": 2} == await event_loop.create_task(coro()) - async def test_clear(self, event_loop): + async def test_clear(self): """ The context-local context can be cleared, causing any previously bound variables to not be included in merge_contextvars's result. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -138,11 +145,12 @@ async def coro(): assert {"b": 2} == await event_loop.create_task(coro()) - async def test_clear_without_bind(self, event_loop): + async def test_clear_without_bind(self): """ The context-local context can be cleared, causing any previously bound variables to not be included in merge_contextvars's result. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): clear_contextvars() @@ -150,11 +158,12 @@ async def coro(): assert {} == await event_loop.create_task(coro()) - async def test_unbind(self, event_loop): + async def test_unbind(self): """ Unbinding a previously bound variable causes it to be removed from the result of merge_contextvars. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -163,10 +172,11 @@ async def coro(): assert {"b": 2} == await event_loop.create_task(coro()) - async def test_unbind_not_bound(self, event_loop): + async def test_unbind_not_bound(self): """ Unbinding a not bound variable causes doesn't raise an exception. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() async def coro(): # Since unbinding means "setting to Ellipsis", we have to make @@ -177,11 +187,12 @@ async def coro(): assert {"b": 2} == await event_loop.create_task(coro()) - async def test_parallel_binds(self, event_loop): + async def test_parallel_binds(self): """ Binding a variable causes it to be included in the result of merge_contextvars. """ + event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() coro1_bind = asyncio.Event() coro2_bind = asyncio.Event() From c69e1d64c2cb2d65a667595a520f0d808c3dbaa5 Mon Sep 17 00:00:00 2001 From: rob dailey Date: Wed, 1 Nov 2023 07:50:02 -0400 Subject: [PATCH 2/2] Removed redundant types and changelog as requested. --- CHANGELOG.md | 3 --- tests/test_contextvars.py | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8dff611..cbc966e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,9 +28,6 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/ - `structlog.threadlocal.tmp_bind()` now also works with `BoundLoggerLazyProxy` (in other words: before anything is bound to a bound logger). -- Migrated tests away from depreciated `event_loop` usage to per recommendations from `pytest-asyncio`. - [pytest-dev/pytest-asyncio#638](https://github.com/pytest-dev/pytest-asyncio/issues/638) - ## [23.2.0](https://github.com/hynek/structlog/compare/23.1.0...23.2.0) - 2023-10-09 diff --git a/tests/test_contextvars.py b/tests/test_contextvars.py index d9426006..d34a16e7 100644 --- a/tests/test_contextvars.py +++ b/tests/test_contextvars.py @@ -38,7 +38,7 @@ async def test_bind(self): Binding a variable causes it to be included in the result of merge_contextvars. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -51,7 +51,7 @@ async def test_multiple_binds(self): Multiple calls to bind_contextvars accumulate values instead of replacing them. But they override redefined ones. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1, c=3) @@ -70,7 +70,7 @@ async def test_reset(self): reset_contextvars allows resetting contexvars to previously-set values. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -94,7 +94,7 @@ async def test_nested_async_bind(self): """ Context is passed correctly between "nested" concurrent operations. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -111,7 +111,7 @@ async def test_merge_works_without_bind(self): merge_contextvars returns values as normal even when there has been no previous calls to bind_contextvars. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): return merge_contextvars(None, None, {"b": 2}) @@ -123,7 +123,7 @@ async def test_merge_overrides_bind(self): Variables included in merge_contextvars override previously bound variables. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -136,7 +136,7 @@ async def test_clear(self): The context-local context can be cleared, causing any previously bound variables to not be included in merge_contextvars's result. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -150,7 +150,7 @@ async def test_clear_without_bind(self): The context-local context can be cleared, causing any previously bound variables to not be included in merge_contextvars's result. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): clear_contextvars() @@ -163,7 +163,7 @@ async def test_unbind(self): Unbinding a previously bound variable causes it to be removed from the result of merge_contextvars. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): bind_contextvars(a=1) @@ -176,7 +176,7 @@ async def test_unbind_not_bound(self): """ Unbinding a not bound variable causes doesn't raise an exception. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() async def coro(): # Since unbinding means "setting to Ellipsis", we have to make @@ -192,7 +192,7 @@ async def test_parallel_binds(self): Binding a variable causes it to be included in the result of merge_contextvars. """ - event_loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() + event_loop = asyncio.get_running_loop() coro1_bind = asyncio.Event() coro2_bind = asyncio.Event()