From b3f2db28278eaff9201896f1e54d77988b22b892 Mon Sep 17 00:00:00 2001 From: Romain Picard Date: Tue, 18 Jan 2022 22:34:07 +0100 Subject: [PATCH] replace coroutine decorator with async keyword fixes #588 --- examples/asyncio/toasyncgenerator.py | 8 +++----- tests/test_observable/test_fromfuture.py | 12 ++++------- tests/test_observable/test_start.py | 7 ++----- .../test_eventloop/test_asyncioscheduler.py | 15 ++++++-------- .../test_asynciothreadsafescheduler.py | 20 ++++++++----------- 5 files changed, 23 insertions(+), 39 deletions(-) diff --git a/examples/asyncio/toasyncgenerator.py b/examples/asyncio/toasyncgenerator.py index 11d081417..4ae044f99 100644 --- a/examples/asyncio/toasyncgenerator.py +++ b/examples/asyncio/toasyncgenerator.py @@ -37,8 +37,7 @@ def on_next(value): source.pipe(ops.materialize()).subscribe(on_next) - @asyncio.coroutine - def gen(): + async def gen(): """Generator producing futures""" nonlocal future @@ -50,8 +49,7 @@ def gen(): return _to_async_generator -@asyncio.coroutine -def go(loop): +async def go(loop): scheduler = AsyncIOScheduler(loop) xs = rx.from_([x for x in range(10)], scheduler=scheduler) @@ -60,7 +58,7 @@ def go(loop): # Wish we could write something like: # ys = (x for x in yield from gen()) while True: - x = yield from gen() + x = await gen() if x is None: break print(x) diff --git a/tests/test_observable/test_fromfuture.py b/tests/test_observable/test_fromfuture.py index f4ac2eb74..068a527f0 100644 --- a/tests/test_observable/test_fromfuture.py +++ b/tests/test_observable/test_fromfuture.py @@ -11,8 +11,7 @@ def test_future_success(self): loop = asyncio.get_event_loop() success = [False, True, False] - @asyncio.coroutine - def go(): + async def go(): future = Future() future.set_result(42) @@ -36,8 +35,7 @@ def test_future_failure(self): loop = asyncio.get_event_loop() success = [True, False, True] - @asyncio.coroutine - def go(): + async def go(): error = Exception('woops') future = Future() @@ -63,8 +61,7 @@ def test_future_cancel(self): loop = asyncio.get_event_loop() success = [True, False, True] - @asyncio.coroutine - def go(): + async def go(): future = Future() source = rx.from_future(future) @@ -87,8 +84,7 @@ def test_future_dispose(self): loop = asyncio.get_event_loop() success = [True, True, True] - @asyncio.coroutine - def go(): + async def go(): future = Future() future.set_result(42) diff --git a/tests/test_observable/test_start.py b/tests/test_observable/test_start.py index 3211f0d00..606cbfa57 100644 --- a/tests/test_observable/test_start.py +++ b/tests/test_observable/test_start.py @@ -20,8 +20,7 @@ def test_start_async(self): loop = asyncio.get_event_loop() success = [False] - @asyncio.coroutine - def go(): + async def go(): def func(): future = Future() future.set_result(42) @@ -40,8 +39,7 @@ def test_start_async_error(self): loop = asyncio.get_event_loop() success = [False] - @asyncio.coroutine - def go(): + async def go(): def func(): future = Future() future.set_exception(Exception(str(42))) @@ -74,7 +72,6 @@ def func(): assert(done) - def test_start_func2(self): scheduler = TestScheduler() diff --git a/tests/test_scheduler/test_eventloop/test_asyncioscheduler.py b/tests/test_scheduler/test_eventloop/test_asyncioscheduler.py index c0aaad0ca..2460955dd 100644 --- a/tests/test_scheduler/test_eventloop/test_asyncioscheduler.py +++ b/tests/test_scheduler/test_eventloop/test_asyncioscheduler.py @@ -25,8 +25,7 @@ def test_asyncio_schedule_now_units(self): def test_asyncio_schedule_action(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): scheduler = AsyncIOScheduler(loop) ran = False @@ -36,7 +35,7 @@ def action(scheduler, state): scheduler.schedule(action) - yield from asyncio.sleep(0.1) + await asyncio.sleep(0.1) assert ran is True loop.run_until_complete(go()) @@ -44,8 +43,7 @@ def action(scheduler, state): def test_asyncio_schedule_action_due(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): scheduler = AsyncIOScheduler(loop) starttime = loop.time() endtime = None @@ -56,7 +54,7 @@ def action(scheduler, state): scheduler.schedule_relative(0.2, action) - yield from asyncio.sleep(0.3) + await asyncio.sleep(0.3) assert endtime is not None diff = endtime - starttime assert diff > 0.18 @@ -66,8 +64,7 @@ def action(scheduler, state): def test_asyncio_schedule_action_cancel(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): ran = False scheduler = AsyncIOScheduler(loop) @@ -78,7 +75,7 @@ def action(scheduler, state): d = scheduler.schedule_relative(0.05, action) d.dispose() - yield from asyncio.sleep(0.3) + await asyncio.sleep(0.3) assert ran is False loop.run_until_complete(go()) diff --git a/tests/test_scheduler/test_eventloop/test_asynciothreadsafescheduler.py b/tests/test_scheduler/test_eventloop/test_asynciothreadsafescheduler.py index b571e3712..47b36e4e9 100644 --- a/tests/test_scheduler/test_eventloop/test_asynciothreadsafescheduler.py +++ b/tests/test_scheduler/test_eventloop/test_asynciothreadsafescheduler.py @@ -26,8 +26,7 @@ def test_asyncio_threadsafe_schedule_now_units(self): def test_asyncio_threadsafe_schedule_action(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): scheduler = AsyncIOThreadSafeScheduler(loop) ran = False @@ -40,7 +39,7 @@ def schedule(): threading.Thread(target=schedule).start() - yield from asyncio.sleep(0.1) + await asyncio.sleep(0.1) assert ran is True loop.run_until_complete(go()) @@ -48,8 +47,7 @@ def schedule(): def test_asyncio_threadsafe_schedule_action_due(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): scheduler = AsyncIOThreadSafeScheduler(loop) starttime = loop.time() endtime = None @@ -63,7 +61,7 @@ def schedule(): threading.Thread(target=schedule).start() - yield from asyncio.sleep(0.3) + await asyncio.sleep(0.3) assert endtime is not None diff = endtime - starttime assert diff > 0.18 @@ -73,8 +71,7 @@ def schedule(): def test_asyncio_threadsafe_schedule_action_cancel(self): loop = asyncio.get_event_loop() - @asyncio.coroutine - def go(): + async def go(): ran = False scheduler = AsyncIOThreadSafeScheduler(loop) @@ -88,7 +85,7 @@ def schedule(): threading.Thread(target=schedule).start() - yield from asyncio.sleep(0.3) + await asyncio.sleep(0.3) assert ran is False loop.run_until_complete(go()) @@ -110,9 +107,8 @@ def thread_target(): test_body(scheduler, action, update_state) - @asyncio.coroutine - def go(): - yield from asyncio.sleep(0.2) + async def go(): + await asyncio.sleep(0.2) loop.run_until_complete(go())