diff --git a/.github/workflows/pr-type-check.yml b/.github/workflows/pr-type-check.yml index c3547920..58a90ee3 100644 --- a/.github/workflows/pr-type-check.yml +++ b/.github/workflows/pr-type-check.yml @@ -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} diff --git a/sanic/app.py b/sanic/app.py index c393c715..ec35ee77 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1552,10 +1552,19 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): 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 @@ -1617,10 +1626,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): 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: @@ -1637,10 +1648,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): *, 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, ...] = () @@ -1659,10 +1672,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): ... 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() @@ -1675,10 +1690,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): 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() @@ -1692,10 +1709,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): @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()) # -------------------------------------------------------------------- # diff --git a/tests/test_create_task.py b/tests/test_create_task.py index c98666a9..a11bc302 100644 --- a/tests/test_create_task.py +++ b/tests/test_create_task.py @@ -2,6 +2,7 @@ import asyncio import sys from threading import Event +from unittest.mock import Mock import pytest @@ -77,6 +78,25 @@ def test_create_named_task(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():