From 0450728ca39c6e1c9039e06ceed4aa1b05156cb5 Mon Sep 17 00:00:00 2001 From: azimovMichael Date: Sun, 24 Apr 2022 15:55:51 +0300 Subject: [PATCH] removed Optional and raise an exception if not in ASGI mode --- sanic/request.py | 8 +++++--- tests/test_request.py | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index cab8e58317..938554fc56 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -821,13 +821,15 @@ def url_for(self, view_name: str, **kwargs) -> str: ) @property - def scope(self) -> Optional[ASGIScope]: + def scope(self) -> ASGIScope: """ - :return: The ASGI scope of the request. If the app isn't an ASGI app, then returns None. + :return: The ASGI scope of the request. If the app isn't an ASGI app, then raises an exception. :rtype: Optional[ASGIScope] """ + if not self.app.asgi: + raise NotImplementedError("App isn't running in ASGI mode. Scope is only available for ASGI apps.") - return self.transport.scope if self.app.asgi else None + return self.transport.scope class File(NamedTuple): diff --git a/tests/test_request.py b/tests/test_request.py index 4e1132142d..5c21b99d8e 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -193,19 +193,19 @@ def test_bad_url_parse(): ) -def test_request_scope_is_none_when_no_asgi(): +def test_request_scope_raises_exception_when_no_asgi(): app = Sanic("no_asgi") @app.get("/") async def get(request): - return response.empty() + return request.scope - request, _ = app.test_client.get("/") - assert request.scope is None + _, response = app.test_client.get("/") + assert response.status == 500 @pytest.mark.asyncio -async def test_request_scope_is_not_none_when_runnin_in_asgi(app): +async def test_request_scope_is_not_none_when_running_in_asgi(app): @app.get("/") async def get(request): return response.empty()