From 840d02f3f0cd341207db6d918ce7f0987be9952e Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 13 Jun 2023 11:36:40 +0530 Subject: GH-105684: Require `asyncio.Task` implementations to support `set_name` method (#105685) --- Lib/asyncio/base_events.py | 2 +- Lib/asyncio/taskgroups.py | 2 +- Lib/asyncio/tasks.py | 15 +-------------- Lib/test/test_asyncio/test_runners.py | 2 ++ .../2023-06-12-10-40-38.gh-issue-105684.yiHkFD.rst | 3 +++ 5 files changed, 8 insertions(+), 16 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-12-10-40-38.gh-issue-105684.yiHkFD.rst diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 32d7e1c..f650e6b 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -443,7 +443,7 @@ class BaseEventLoop(events.AbstractEventLoop): else: task = self._task_factory(self, coro, context=context) - tasks._set_task_name(task, name) + task.set_name(name) return task diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 06b2e0d..bf92bba 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -163,7 +163,7 @@ class TaskGroup: task = self._loop.create_task(coro) else: task = self._loop.create_task(coro, context=context) - tasks._set_task_name(task, name) + task.set_name(name) # optimization: Immediately call the done callback if the task is # already done (e.g. if the coro was able to complete eagerly), # and skip scheduling a done callback diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 4250bb0..75dd3cb 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -68,19 +68,6 @@ def all_tasks(loop=None): if futures._get_loop(t) is loop and not t.done()} -def _set_task_name(task, name): - if name is not None: - try: - set_name = task.set_name - except AttributeError: - warnings.warn("Task.set_name() was added in Python 3.8, " - "the method support will be mandatory for third-party " - "task implementations since 3.13.", - DeprecationWarning, stacklevel=3) - else: - set_name(name) - - class Task(futures._PyFuture): # Inherit Python Task implementation # from a Python Future implementation. @@ -412,7 +399,7 @@ def create_task(coro, *, name=None, context=None): else: task = loop.create_task(coro, context=context) - _set_task_name(task, name) + task.set_name(name) return task diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 811cf8b..b1eb6f4 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -243,6 +243,8 @@ class RunTests(BaseTest): def get_loop(self, *args, **kwargs): return self._task.get_loop(*args, **kwargs) + def set_name(self, *args, **kwargs): + return self._task.set_name(*args, **kwargs) async def main(): interrupt_self() diff --git a/Misc/NEWS.d/next/Library/2023-06-12-10-40-38.gh-issue-105684.yiHkFD.rst b/Misc/NEWS.d/next/Library/2023-06-12-10-40-38.gh-issue-105684.yiHkFD.rst new file mode 100644 index 0000000..b0d4eb3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-12-10-40-38.gh-issue-105684.yiHkFD.rst @@ -0,0 +1,3 @@ +Supporting :meth:`asyncio.Task.set_name` is now mandatory for third party task implementations. +The undocumented :func:`!_set_task_name` function (deprecated since 3.8) has been removed. +Patch by Kumar Aditya. -- cgit v0.12