Skip to content

Commit b8c2c22

Browse files
committed
update async syntax and backport the patch.dict implementation from python/cpython#98095
1 parent 4b1284d commit b8c2c22

File tree

3 files changed

+141
-101
lines changed

3 files changed

+141
-101
lines changed

asynctest/case.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ def _run_test_method(self, method):
353353
if asyncio.iscoroutine(result):
354354
self.loop.run_until_complete(result)
355355

356-
@asyncio.coroutine
357-
def doCleanups(self):
356+
async def doCleanups(self):
358357
"""
359358
Execute all cleanup functions. Normally called for you after tearDown.
360359
"""
@@ -363,7 +362,7 @@ def doCleanups(self):
363362
function, args, kwargs = self._cleanups.pop()
364363
with outcome.testPartExecutor(self):
365364
if asyncio.iscoroutinefunction(function):
366-
yield from function(*args, **kwargs)
365+
await function(*args, **kwargs)
367366
else:
368367
function(*args, **kwargs)
369368

@@ -377,8 +376,7 @@ def addCleanup(self, function, *args, **kwargs):
377376
"""
378377
return super().addCleanup(function, *args, **kwargs)
379378

380-
@asyncio.coroutine
381-
def assertAsyncRaises(self, exception, awaitable):
379+
async def assertAsyncRaises(self, exception, awaitable):
382380
"""
383381
Test that an exception of type ``exception`` is raised when an
384382
exception is raised when awaiting ``awaitable``, a future or coroutine.
@@ -391,40 +389,37 @@ def assertAsyncRaises(self, exception, awaitable):
391389
:see: :meth:`unittest.TestCase.assertRaises()`
392390
"""
393391
with self.assertRaises(exception):
394-
return (yield from awaitable)
392+
return await awaitable
395393

396-
@asyncio.coroutine
397-
def assertAsyncRaisesRegex(self, exception, regex, awaitable):
394+
async def assertAsyncRaisesRegex(self, exception, regex, awaitable):
398395
"""
399396
Like :meth:`assertAsyncRaises()` but also tests that ``regex`` matches
400397
on the string representation of the raised exception.
401398
402399
:see: :meth:`unittest.TestCase.assertRaisesRegex()`
403400
"""
404401
with self.assertRaisesRegex(exception, regex):
405-
return (yield from awaitable)
402+
return await awaitable
406403

407-
@asyncio.coroutine
408-
def assertAsyncWarns(self, warning, awaitable):
404+
async def assertAsyncWarns(self, warning, awaitable):
409405
"""
410406
Test that a warning is triggered when awaiting ``awaitable``, a future
411407
or a coroutine.
412408
413409
:see: :meth:`unittest.TestCase.assertWarns()`
414410
"""
415411
with self.assertWarns(warning):
416-
return (yield from awaitable)
412+
return await awaitable
417413

418-
@asyncio.coroutine
419-
def assertAsyncWarnsRegex(self, warning, regex, awaitable):
414+
async def assertAsyncWarnsRegex(self, warning, regex, awaitable):
420415
"""
421416
Like :meth:`assertAsyncWarns()` but also tests that ``regex`` matches
422417
on the message of the triggered warning.
423418
424419
:see: :meth:`unittest.TestCase.assertWarnsRegex()`
425420
"""
426421
with self.assertWarnsRegex(warning, regex):
427-
return (yield from awaitable)
422+
return await awaitable
428423

429424

430425
class FunctionTestCase(TestCase, unittest.FunctionTestCase):
@@ -446,8 +441,7 @@ def _init_loop(self):
446441
self.loop.time = functools.wraps(self.loop.time)(lambda: self._time)
447442
self._time = 0
448443

449-
@asyncio.coroutine
450-
def advance(self, seconds):
444+
async def advance(self, seconds):
451445
"""
452446
Fast forward time by a number of ``seconds``.
453447
@@ -468,7 +462,7 @@ def advance(self, seconds):
468462
raise ValueError(
469463
'Cannot go back in time ({} seconds)'.format(seconds))
470464

471-
yield from self._drain_loop()
465+
await self._drain_loop()
472466

473467
target_time = self._time + seconds
474468
while True:
@@ -477,26 +471,25 @@ def advance(self, seconds):
477471
break
478472

479473
self._time = next_time
480-
yield from self._drain_loop()
474+
await self._drain_loop()
481475

482476
self._time = target_time
483-
yield from self._drain_loop()
477+
await self._drain_loop()
484478

485479
def _next_scheduled(self):
486480
try:
487481
return self.loop._scheduled[0]._when
488482
except IndexError:
489483
return None
490484

491-
@asyncio.coroutine
492-
def _drain_loop(self):
485+
async def _drain_loop(self):
493486
while True:
494487
next_time = self._next_scheduled()
495488
if not self.loop._ready and (next_time is None or
496489
next_time > self._time):
497490
break
498491

499-
yield from asyncio.sleep(0)
492+
await asyncio.sleep(0)
500493
self.loop._TestCase_asynctest_ran = True
501494

502495

asynctest/helpers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import asyncio
1010

1111

12-
@asyncio.coroutine
13-
def exhaust_callbacks(loop):
12+
async def exhaust_callbacks(loop):
1413
"""
1514
Run the loop until all ready callbacks are executed.
1615
@@ -21,4 +20,4 @@ def exhaust_callbacks(loop):
2120
:param loop: event loop
2221
"""
2322
while loop._ready:
24-
yield from asyncio.sleep(0, loop=loop)
23+
await asyncio.sleep(0)

0 commit comments

Comments
 (0)