Skip to content

bpo-40967: Remove deprecated asyncio.Task.current_task() and asyncio.Task.all_tasks() #20874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -962,31 +962,6 @@ Task Object

.. versionadded:: 3.8

.. classmethod:: all_tasks(loop=None)

Return a set of all tasks for an event loop.

By default all tasks for the current event loop are returned.
If *loop* is ``None``, the :func:`get_event_loop` function
is used to get the current loop.

.. deprecated-removed:: 3.7 3.9

Do not call this as a task method. Use the :func:`asyncio.all_tasks`
function instead.

.. classmethod:: current_task(loop=None)

Return the currently running task or ``None``.

If *loop* is ``None``, the :func:`get_event_loop` function
is used to get the current loop.

.. deprecated-removed:: 3.7 3.9

Do not call this as a task method. Use the
:func:`asyncio.current_task` function instead.


.. _asyncio_generator_based_coro:

Expand Down
28 changes: 0 additions & 28 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,6 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
# status is still pending
_log_destroy_pending = True

@classmethod
def current_task(cls, loop=None):
"""Return the currently running task in an event loop or None.

By default the current task for the current event loop is returned.

None is returned when called not in the context of a Task.
"""
warnings.warn("Task.current_task() is deprecated since Python 3.7, "
"use asyncio.current_task() instead",
DeprecationWarning,
stacklevel=2)
if loop is None:
loop = events.get_event_loop()
return current_task(loop)

@classmethod
def all_tasks(cls, loop=None):
"""Return a set of all tasks for an event loop.

By default all tasks for the current event loop are returned.
"""
warnings.warn("Task.all_tasks() is deprecated since Python 3.7, "
"use asyncio.all_tasks() instead",
DeprecationWarning,
stacklevel=2)
return _all_tasks_compat(loop)

def __init__(self, coro, *, loop=None, name=None):
super().__init__(loop=loop)
if self._source_traceback:
Expand Down
46 changes: 1 addition & 45 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,32 +1943,6 @@ async def coro():
self.assertEqual(res, 'test')
self.assertIsNone(t2.result())


def test_current_task_deprecated(self):
Task = self.__class__.Task

with self.assertWarns(DeprecationWarning):
self.assertIsNone(Task.current_task(loop=self.loop))

async def coro(loop):
with self.assertWarns(DeprecationWarning):
self.assertIs(Task.current_task(loop=loop), task)

# See http://bugs.python.org/issue29271 for details:
asyncio.set_event_loop(loop)
try:
with self.assertWarns(DeprecationWarning):
self.assertIs(Task.current_task(None), task)
with self.assertWarns(DeprecationWarning):
self.assertIs(Task.current_task(), task)
finally:
asyncio.set_event_loop(None)

task = self.new_task(self.loop, coro(self.loop))
self.loop.run_until_complete(task)
with self.assertWarns(DeprecationWarning):
self.assertIsNone(Task.current_task(loop=self.loop))

def test_current_task(self):
self.assertIsNone(asyncio.current_task(loop=self.loop))

Expand Down Expand Up @@ -2305,16 +2279,6 @@ def foo():
self.assertIsInstance(exception, Exception)
self.assertEqual(exception.args, ("foo", ))

def test_all_tasks_deprecated(self):
Task = self.__class__.Task

async def coro():
with self.assertWarns(DeprecationWarning):
assert Task.all_tasks(self.loop) == {t}

t = self.new_task(self.loop, coro())
self.loop.run_until_complete(t)

def test_log_destroyed_pending_task(self):
Task = self.__class__.Task

Expand All @@ -2337,15 +2301,7 @@ def kill_me(loop):

self.assertEqual(asyncio.all_tasks(loop=self.loop), {task})

# See http://bugs.python.org/issue29271 for details:
asyncio.set_event_loop(self.loop)
try:
with self.assertWarns(DeprecationWarning):
self.assertEqual(Task.all_tasks(), {task})
with self.assertWarns(DeprecationWarning):
self.assertEqual(Task.all_tasks(None), {task})
finally:
asyncio.set_event_loop(None)
asyncio.set_event_loop(None)

# execute the task so it waits for future
self.loop._run_once()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Removed :meth:`asyncio.Task.current_task` and
:meth:`asyncio.Task.all_tasks`. Patch contributed by Rémi Lapeyre.