[Python-checkins] bpo-34728: Remove deprecate *loop* argument in asyncio.sleep (GH-9415)

Carol Willing webhook-mailer at python.org
Mon Sep 24 05:51:26 EDT 2018


https://github.com/python/cpython/commit/558c49bcf3a8543d64a68de836b5d855efd56696
commit: 558c49bcf3a8543d64a68de836b5d855efd56696
branch: master
author: João Júnior <joaojunior.ma at gmail.com>
committer: Carol Willing <carolcode at willingconsulting.com>
date: 2018-09-24T05:51:22-04:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2018-09-20-16-55-43.bpo-34728.CUE8LU.rst
M Lib/asyncio/tasks.py
M Lib/test/test_asyncio/test_tasks.py

diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 7121aa65da0e..b52aad8c422d 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -382,7 +382,11 @@ def create_task(coro, *, name=None):
         raise ValueError(f'Invalid return_when value: {return_when}')
 
     if loop is None:
-        loop = events.get_event_loop()
+        loop = events.get_running_loop()
+    else:
+        warnings.warn("The loop argument is deprecated and scheduled for"
+                      "removal in Python 4.0.",
+                      DeprecationWarning, stacklevel=2)
 
     fs = {ensure_future(f, loop=loop) for f in set(fs)}
 
@@ -408,7 +412,11 @@ def _release_waiter(waiter, *args):
     This function is a coroutine.
     """
     if loop is None:
-        loop = events.get_event_loop()
+        loop = events.get_running_loop()
+    else:
+        warnings.warn("The loop argument is deprecated and scheduled for"
+                      "removal in Python 4.0.",
+                      DeprecationWarning, stacklevel=2)
 
     if timeout is None:
         return await fut
@@ -585,7 +593,12 @@ def __sleep0():
         return result
 
     if loop is None:
-        loop = events.get_event_loop()
+        loop = events.get_running_loop()
+    else:
+        warnings.warn("The loop argument is deprecated and scheduled for"
+                      "removal in Python 4.0.",
+                      DeprecationWarning, stacklevel=2)
+
     future = loop.create_future()
     h = loop.call_later(delay,
                         futures._set_result_unless_cancelled,
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 40d1953532bc..ea54706edc8e 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -3220,6 +3220,35 @@ def coro():
         self.loop.run_until_complete(coro())
         self.assertEqual(result, 11)
 
+    def test_loop_argument_is_deprecated(self):
+        # Remove test when loop argument is removed in Python 4.0
+        with self.assertWarns(DeprecationWarning):
+            self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
+
+class WaitTests(test_utils.TestCase):
+    def setUp(self):
+        super().setUp()
+        self.loop = asyncio.new_event_loop()
+        asyncio.set_event_loop(None)
+
+    def tearDown(self):
+        self.loop.close()
+        self.loop = None
+        super().tearDown()
+
+    def test_loop_argument_is_deprecated_in_wait(self):
+        # Remove test when loop argument is removed in Python 4.0
+        with self.assertWarns(DeprecationWarning):
+            self.loop.run_until_complete(
+                asyncio.wait([coroutine_function()], loop=self.loop))
+
+    def test_loop_argument_is_deprecated_in_wait_for(self):
+        # Remove test when loop argument is removed in Python 4.0
+        with self.assertWarns(DeprecationWarning):
+            self.loop.run_until_complete(
+                asyncio.wait_for(coroutine_function(), 0.01, loop=self.loop))
+
 
 class CompatibilityTests(test_utils.TestCase):
     # Tests for checking a bridge between old-styled coroutines
diff --git a/Misc/NEWS.d/next/Library/2018-09-20-16-55-43.bpo-34728.CUE8LU.rst b/Misc/NEWS.d/next/Library/2018-09-20-16-55-43.bpo-34728.CUE8LU.rst
new file mode 100644
index 000000000000..e06eb0f45c9f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-09-20-16-55-43.bpo-34728.CUE8LU.rst
@@ -0,0 +1,2 @@
+Add deprecation warning when `loop` is used in methods: `asyncio.sleep`,
+`asyncio.wait` and `asyncio.wait_for`.



More information about the Python-checkins mailing list