Skip to content

Commit

Permalink
Update fastapi version (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostsijm authored Oct 21, 2024
1 parent 39cc9e0 commit 9439cfb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## 0.17.3 (unreleased)
----------------------

- Nothing changed yet.
- Bumped FastAPI to 0.115.* so that pydantic models are directly supported to
declare query parameters. This deprecates the `RequestQuery.depends()` syntax,
use `Annotated[RequestQuery, Query()]` instead.


## 0.17.2 (2024-10-10)
Expand Down
15 changes: 10 additions & 5 deletions clean_python/fastapi/request_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from inspect import signature
from typing import ClassVar
from typing import Literal
from warnings import warn

from fastapi import Depends
from fastapi import Query
Expand All @@ -23,7 +24,7 @@ class RequestQuery(ValueObject):
Example usage in a Resource:
@get("/books")
def list_books(self, q: RequestQuery = RequestQuery.depends()):
def list_books(self, q: Annotated[RequestQuery, Query()]):
return self.manage.filter(q.filters(), q.as_page_options())
"""

Expand Down Expand Up @@ -85,15 +86,19 @@ def filters(self) -> list[Filter]:

@classmethod
def depends(cls) -> Depends:
"""FastAPI does not directly support pydantic models for query parameters.
Specifically, pydantic ValidationErrors lead to an internal server error. For this to work,
we wrap the RequestQuery, forwarding the type signature.
"""DEPRECATED: FastAPI now does directly support pydantic models for query parameters.
See class docstring for usage.
Source:
- https://fastapi.tiangolo.com/tutorial/query-param-models/
- https://github.com/tiangolo/fastapi/issues/1474
"""
warn(
"Deprecated: Pydantic models as query parameters are now supported, see class docstring.",
DeprecationWarning,
stacklevel=2,
)

def wrapper(*args, **kwargs):
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test = [
"python-multipart",
"pytest-celery<1"
]
fastapi = ["fastapi==0.114.*"]
fastapi = ["fastapi==0.115.*"]
auth = ["pyjwt==2.9.*", "cryptography==43.0.*"] # pyjwt[crypto]
amqp = ["pika==1.3.*"]
celery = ["celery==5.4.*"]
Expand Down
3 changes: 2 additions & 1 deletion tests/fastapi/test_request_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from http import HTTPStatus
from typing import Annotated
from typing import List
from typing import Literal
from typing import Optional
Expand Down Expand Up @@ -91,7 +92,7 @@ def test_filters_comparison(values, expected):

class FooResource(Resource, version=v(1), name="testing"):
@get("/query")
def query(self, q: SomeQuery = SomeQuery.depends()):
def query(self, q: Annotated[SomeQuery, Query()]):
return q.model_dump()


Expand Down

0 comments on commit 9439cfb

Please sign in to comment.