Skip to content

Commit

Permalink
Merge pull request #47 from adhamsalama/fix-return-type-hint-validation
Browse files Browse the repository at this point in the history
fix(validation): Fix return type hint validation
  • Loading branch information
adhamsalama authored Apr 19, 2023
2 parents f807bbe + 5694500 commit 580cb72
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simplestapi"
version = "0.1.4"
version = "0.1.5"
description = "SimpleAPI is a minimalistic, unopinionated web framework for Python, inspired by FastAPI & Flask"
authors = ["Adham Salama <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion simpleapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.2"
__version__ = "0.1.5"

from .custom_types import RouteHandler, ViewFunction, Middleware, Query
from .request import Request
Expand Down
4 changes: 4 additions & 0 deletions simpleapi/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def handle_request(
return middleware_response

handler_type_hints = get_type_hints(handler["handler"])
# Remove return type hint as we don't need it because it had caused
# a validation error for any handler function that had a return type hint
if "return" in handler_type_hints:
del handler_type_hints["return"]
dependency_injection: dict[str, Any] = {}
for k, v in handler_type_hints.items():
if v == Request:
Expand Down
5 changes: 4 additions & 1 deletion tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def always_reject_middleware(request: Request):


@app.get("/set-cookie")
def set_cookies(request: Request):
# Don't remove the return type hint
# this catches the case where handler function
# has return type hint which throw a validation error
def set_cookies(request: Request) -> JSONResponse:
"""Sets cookies"""
res = JSONResponse(body=request.cookies, headers=[("header1", "value1")])
res.set_cookie("key1", "value1")
Expand Down

0 comments on commit 580cb72

Please sign in to comment.