mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
Typing cleanup
- Compatibility with older Python versions - use `Optional` and `Union` instead of `... | None` and `a | b` - use `typing_extensions.Protocol` instead of `typing.Protocol` - use `typing.Dict`, `typing.List`, etc. instead of the concrete types. - Fix backend `.get()` annotations; not all were marked as `Optional[str]` - Don't return anything from `Backend.set()` methods. - The `Coder.decode_as_type()` type parameter must be a type to be compatible with `ModelField(..., type_=...)`. - Clean up `Optional[]` use, remove where it is not needed. - Clean up variable use in decorator, keeping the raw cached value separate from the return value from the wrapped endpoint. - Annotate the wrapper as returning either the original type _or_ a Response (returning a 304 Not Modified response). - Clean up small edge-case where `response` could be `None`. - Correct type annotation on `JsonCoder.decode()` to match `Coder.decode()`.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from typing import Dict, Optional
|
||||
|
||||
import pendulum
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
@@ -84,9 +86,9 @@ app.get("/method")(cache(namespace="test")(instance.handler_method))
|
||||
# cache a Pydantic model instance; the return type annotation is required in this case
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str | None = None
|
||||
description: Optional[str] = None
|
||||
price: float
|
||||
tax: float | None = None
|
||||
tax: Optional[float] = None
|
||||
|
||||
|
||||
@app.get("/pydantic_instance")
|
||||
@@ -110,7 +112,7 @@ async def uncached_put():
|
||||
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python")
|
||||
def namespaced_injection(
|
||||
__fastapi_cache_request: int = 42, __fastapi_cache_response: int = 17
|
||||
) -> dict[str, int]:
|
||||
) -> Dict[str, int]:
|
||||
return {
|
||||
"__fastapi_cache_request": __fastapi_cache_request,
|
||||
"__fastapi_cache_response": __fastapi_cache_response,
|
||||
|
||||
Reference in New Issue
Block a user