Skip to content

Commit

Permalink
Ignore name argument on Python 3.7 (#2355)
Browse files Browse the repository at this point in the history
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
Co-authored-by: Ryu juheon <saidbysolo@gmail.com>
  • Loading branch information
3 people committed Jan 6, 2022
1 parent 34d1dee commit a23547d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-type-check.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
os: [ubuntu-latest]
config:
- { python-version: 3.7, tox-env: type-checking}
# - { python-version: 3.7, tox-env: type-checking}
- { python-version: 3.8, tox-env: type-checking}
- { python-version: 3.9, tox-env: type-checking}
- { python-version: "3.10", tox-env: type-checking}
Expand Down
55 changes: 37 additions & 18 deletions sanic/app.py
Expand Up @@ -1552,10 +1552,19 @@ def _loop_add_task(
name: Optional[str] = None,
register: bool = True,
) -> Task:
prepped = cls._prep_task(task, app, loop)
task = loop.create_task(prepped, name=name)
if not isinstance(task, Future):
prepped = cls._prep_task(task, app, loop)
if sys.version_info < (3, 8):
if name:
error_logger.warning(
"Cannot set a name for a task when using Python 3.7. "
"Your task will be created without a name."
)
task = loop.create_task(prepped)
else:
task = loop.create_task(prepped, name=name)

if name and register:
if name and register and sys.version_info > (3, 7):
app._task_registry[name] = task

return task
Expand Down Expand Up @@ -1617,10 +1626,12 @@ def add_task(
def get_task(
self, name: str, *, raise_exception: bool = True
) -> Optional[Task]:
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (get_task) is only supported on using "
"Python 3.8+."
)
return
try:
return self._task_registry[name]
except KeyError:
Expand All @@ -1637,10 +1648,12 @@ async def cancel_task(
*,
raise_exception: bool = True,
) -> None:
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (cancel_task) is only supported on using "
"Python 3.8+."
)
return
task = self.get_task(name, raise_exception=raise_exception)
if task and not task.cancelled():
args: Tuple[str, ...] = ()
Expand All @@ -1659,10 +1672,12 @@ async def cancel_task(
...

def purge_tasks(self):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (purge_tasks) is only supported on using "
"Python 3.8+."
)
return
for task in self.tasks:
if task.done() or task.cancelled():
name = task.get_name()
Expand All @@ -1675,10 +1690,12 @@ def purge_tasks(self):
def shutdown_tasks(
self, timeout: Optional[float] = None, increment: float = 0.1
):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (shutdown_tasks) is only supported on using "
"Python 3.8+."
)
return
for task in self.tasks:
task.cancel()

Expand All @@ -1692,10 +1709,12 @@ def shutdown_tasks(

@property
def tasks(self):
if sys.version_info == (3, 7):
raise RuntimeError(
"This feature is only supported on using Python 3.8+."
if sys.version_info < (3, 8):
error_logger.warning(
"This feature (tasks) is only supported on using "
"Python 3.8+."
)
return
return iter(self._task_registry.values())

# -------------------------------------------------------------------- #
Expand Down
20 changes: 20 additions & 0 deletions tests/test_create_task.py
Expand Up @@ -2,6 +2,7 @@
import sys

from threading import Event
from unittest.mock import Mock

import pytest

Expand Down Expand Up @@ -77,6 +78,25 @@ async def stop(app, _):
app.run()


def test_named_task_called(app):
e = Event()

async def coro():
e.set()

@app.route("/")
async def isset(request):
await asyncio.sleep(0.05)
return text(str(e.is_set()))

@app.before_server_start
async def setup(app, _):
app.add_task(coro, name="dummy_task")

request, response = app.test_client.get("/")
assert response.body == b"True"


@pytest.mark.skipif(sys.version_info < (3, 8), reason="Not supported in 3.7")
def test_create_named_task_fails_outside_app(app):
async def dummy():
Expand Down

0 comments on commit a23547d

Please sign in to comment.