Skip to content

Commit

Permalink
removed Optional and raise an exception if not in ASGI mode
Browse files Browse the repository at this point in the history
  • Loading branch information
azimovMichael committed Apr 24, 2022
1 parent 7d32174 commit 0450728
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions sanic/request.py
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_request.py
Expand Up @@ -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()
Expand Down

0 comments on commit 0450728

Please sign in to comment.