Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 558c49b

Browse files
joaojuniorwillingc
authored andcommitted
bpo-34728: Remove deprecate *loop* argument in asyncio.sleep (GH-9415)
* Insert the warn in the asyncio.sleep when the loop argument is used * Insert the warn in the asyncio.wait and asyncio.wait_for when the loop argument is used * Better format of the code * Add news file * change calls for get_event_loop() to calls for get_running_loop() * Change message to be more clear in News * Improve the comments in test_tasks
1 parent a0fd7f1 commit 558c49b

3 files changed

Lines changed: 47 additions & 3 deletions

File tree

Lib/asyncio/tasks.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
382382
raise ValueError(f'Invalid return_when value: {return_when}')
383383

384384
if loop is None:
385-
loop = events.get_event_loop()
385+
loop = events.get_running_loop()
386+
else:
387+
warnings.warn("The loop argument is deprecated and scheduled for"
388+
"removal in Python 4.0.",
389+
DeprecationWarning, stacklevel=2)
386390

387391
fs = {ensure_future(f, loop=loop) for f in set(fs)}
388392

@@ -408,7 +412,11 @@ async def wait_for(fut, timeout, *, loop=None):
408412
This function is a coroutine.
409413
"""
410414
if loop is None:
411-
loop = events.get_event_loop()
415+
loop = events.get_running_loop()
416+
else:
417+
warnings.warn("The loop argument is deprecated and scheduled for"
418+
"removal in Python 4.0.",
419+
DeprecationWarning, stacklevel=2)
412420

413421
if timeout is None:
414422
return await fut
@@ -585,7 +593,12 @@ async def sleep(delay, result=None, *, loop=None):
585593
return result
586594

587595
if loop is None:
588-
loop = events.get_event_loop()
596+
loop = events.get_running_loop()
597+
else:
598+
warnings.warn("The loop argument is deprecated and scheduled for"
599+
"removal in Python 4.0.",
600+
DeprecationWarning, stacklevel=2)
601+
589602
future = loop.create_future()
590603
h = loop.call_later(delay,
591604
futures._set_result_unless_cancelled,

Lib/test/test_asyncio/test_tasks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,35 @@ def coro():
32203220
self.loop.run_until_complete(coro())
32213221
self.assertEqual(result, 11)
32223222

3223+
def test_loop_argument_is_deprecated(self):
3224+
# Remove test when loop argument is removed in Python 4.0
3225+
with self.assertWarns(DeprecationWarning):
3226+
self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
3227+
3228+
3229+
class WaitTests(test_utils.TestCase):
3230+
def setUp(self):
3231+
super().setUp()
3232+
self.loop = asyncio.new_event_loop()
3233+
asyncio.set_event_loop(None)
3234+
3235+
def tearDown(self):
3236+
self.loop.close()
3237+
self.loop = None
3238+
super().tearDown()
3239+
3240+
def test_loop_argument_is_deprecated_in_wait(self):
3241+
# Remove test when loop argument is removed in Python 4.0
3242+
with self.assertWarns(DeprecationWarning):
3243+
self.loop.run_until_complete(
3244+
asyncio.wait([coroutine_function()], loop=self.loop))
3245+
3246+
def test_loop_argument_is_deprecated_in_wait_for(self):
3247+
# Remove test when loop argument is removed in Python 4.0
3248+
with self.assertWarns(DeprecationWarning):
3249+
self.loop.run_until_complete(
3250+
asyncio.wait_for(coroutine_function(), 0.01, loop=self.loop))
3251+
32233252

32243253
class CompatibilityTests(test_utils.TestCase):
32253254
# Tests for checking a bridge between old-styled coroutines
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add deprecation warning when `loop` is used in methods: `asyncio.sleep`,
2+
`asyncio.wait` and `asyncio.wait_for`.

0 commit comments

Comments
 (0)