Skip to content
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

Fix KeyError raised when annotating a view with return annotations #1

Merged
merged 5 commits into from
Dec 12, 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
9 changes: 9 additions & 0 deletions starlite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pydantic.error_wrappers import ValidationError
from pydantic.fields import ModelField
from starlette.requests import Request
from starlette.responses import Response as StarletteResponse

from starlite.controller import Controller
from starlite.enums import HttpMethod, MediaType
Expand Down Expand Up @@ -74,6 +75,11 @@ class Config(BaseConfig):
signature = Signature.from_callable(fn)
field_definitions: Dict[str, Tuple[Any, Any]] = {}
for key, value in getfullargspec(fn).annotations.items():

# discard return annotations
if key == "return":
continue

parameter = signature.parameters[key]
if parameter.default is not signature.empty:
field_definitions[key] = (value, parameter.default)
Expand Down Expand Up @@ -132,6 +138,9 @@ async def handle_request(route_handler: "RouteHandler", request: Request) -> Res
if isawaitable(data):
data = await data

if isinstance(data, StarletteResponse):
return data

media_type = route_handler.media_type or response_class.media_type or MediaType.JSON
return response_class(
content=data,
Expand Down
31 changes: 30 additions & 1 deletion tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
from pydantic.fields import ModelField
from starlette.requests import Request

from starlite import HttpMethod, ImproperlyConfiguredException, Provide, get, route
from starlite import (
HttpMethod,
ImproperlyConfiguredException,
Provide,
Response,
get,
route,
)
from starlite.request import (
create_function_signature_model,
get_kwargs_from_request,
Expand Down Expand Up @@ -93,3 +100,25 @@ async def test_function(data: Person):

response = await handle_request(route_handler=cast(Any, test_function), request=request)
assert response.body.decode("utf-8") == person_instance.json()


@pytest.mark.asyncio
async def test_handle_return_annotation():
@get(path="/health", status_code=204)
async def health_check() -> None:
return

r = create_function_signature_model(health_check.fn)
assert r().dict() == {}


@pytest.mark.asyncio
async def test_handle_request_with_response():
@get(path="/health")
async def health_check():
return Response(status_code=204)

request = create_test_request(content=None, http_method=HttpMethod.GET)
response = await handle_request(route_handler=cast(Any, health_check), request=request)
assert response.status_code == 204
assert not hasattr(response, "content")