Skip to content

[3.9] bpo-44815: Always show deprecation in asyncio.gather/sleep() #27569

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 8 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 10 additions & 8 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,17 @@ def __sleep0():

async def sleep(delay, result=None, *, loop=None):
"""Coroutine that completes after a given time (in seconds)."""
if loop is not None:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if delay <= 0:
await __sleep0()
return result

if loop is None:
loop = events.get_running_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

future = loop.create_future()
h = loop.call_later(delay,
Expand Down Expand Up @@ -750,13 +751,14 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
after catching an exception (raised by one of the awaitables) from
gather won't cancel any other awaitables.
"""
if loop is not None:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if not coros_or_futures:
if loop is None:
loop = events.get_event_loop()
else:
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
outer = loop.create_future()
outer.set_result([])
return outer
Expand Down
37 changes: 33 additions & 4 deletions Lib/test/test_asyncio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import os
from test.support import load_package_tests, import_module
from test import support
import unittest

# Skip tests if we don't have concurrent.futures.
import_module('concurrent.futures')
support.import_module('concurrent.futures')

def load_tests(*args):
return load_package_tests(os.path.dirname(__file__), *args)

def load_tests(loader, _, pattern):
pkg_dir = os.path.dirname(__file__)
suite = AsyncioTestSuite()
return support.load_package_tests(pkg_dir, loader, suite, pattern)


class AsyncioTestSuite(unittest.TestSuite):
"""A custom test suite that also runs setup/teardown for the whole package.

Normally unittest only runs setUpModule() and tearDownModule() within each
test module part of the test suite. Copying those functions to each file
would be tedious, let's run this once and for all.
"""
def run(self, result, debug=False):
ignore = support.ignore_deprecations_from
tokens = {
ignore("asyncio.base_events", like=r".*loop argument.*"),
ignore("asyncio.unix_events", like=r".*loop argument.*"),
ignore("asyncio.futures", like=r".*loop argument.*"),
ignore("asyncio.runners", like=r".*loop argument.*"),
ignore("asyncio.subprocess", like=r".*loop argument.*"),
ignore("asyncio.tasks", like=r".*loop argument.*"),
ignore("test.test_asyncio.test_queues", like=r".*loop argument.*"),
ignore("test.test_asyncio.test_tasks", like=r".*loop argument.*"),
}
try:
super().run(result, debug=debug)
finally:
support.clear_ignored_deprecations(*tokens)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Always show ``loop=`` arg deprecations in :func:`asyncio.gather` and
:func:`asyncio.sleep`