Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
In PR#128768 (gh-128308), the asyncio task factory signature was changed from(loop, coro, context=None)
to(loop, coro, **kwargs)
. This seemingly minor change breaks backward compatibility for existing task factory implementations.
The documentation was updated to reflect this new contract:
- Old:
(loop, coro, context=None)
, where the callable must return aasyncio.Future
-compatible object. - New:
(loop, coro, **kwargs)
, where the callable must pass on all *kwargs, and return aasyncio.Task
-compatible object.
This change causes runtime errors for any code with task factories implemented according to the previous contract, as these factories now unexpectedly receive keyword arguments likename
that they aren't designed to handle.
Reproducer
importasyncio# Task factory implemented according to the previous API contractdefold_style_task_factory(loop,coro,context=None):print(f"Creating task with loop={loop}, coro={coro}, context={context}")returnasyncio.Task(coro,loop=loop)asyncdefmain():# Set up a custom task factory following the old documented APIloop=asyncio.get_event_loop()loop.set_task_factory(old_style_task_factory)# This will fail with TypeError due to unexpected 'name' argumentprint("Attempting to create task with name...")awaitloop.create_task(asyncio.sleep(0.1),name="test_task")print("Task completed successfully")# We won't reach this lineif__name__=="__main__":try:asyncio.run(main())exceptTypeErrorase:print(f"ERROR:{e}")# Output: ERROR: old_style_task_factory() got an unexpected keyword argument 'name'
This prints:
Attempting to create task with name...Creating task with loop=<_UnixSelectorEventLoop running=False closed=False debug=False>, coro=<coroutine object BaseEventLoop.shutdown_asyncgens at 0x72b413aa5450>, context=NoneCreating task with loop=<_UnixSelectorEventLoop running=False closed=False debug=False>, coro=<coroutine object BaseEventLoop.shutdown_default_executor at 0x72b413cc5910>, context=NoneERROR: old_style_task_factory() got an unexpected keyword argument 'name'<sys>:0: RuntimeWarning: coroutine 'sleep' was never awaitedRuntimeWarning: Enable tracemalloc to get the object allocation traceback
This is a breaking change introduced in a patch release, which violates our versioning policy. The change was intended to support thename
keyword argument for eager tasks, but the implementation method has unintentionally broken existing code that followed the previously documented API contract.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
Sub-issues
Metadata
Metadata
Assignees
Labels
Projects
Status
Status