mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix evaluating stringified annotations in Python 3.10 (#11355)
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> Co-authored-by: svlandeg <svlandeg@github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
parent
811fa89875
commit
861598b4e3
|
|
@ -1,5 +1,6 @@
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import inspect
|
import inspect
|
||||||
|
import sys
|
||||||
from contextlib import AsyncExitStack, contextmanager
|
from contextlib import AsyncExitStack, contextmanager
|
||||||
from copy import copy, deepcopy
|
from copy import copy, deepcopy
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
@ -191,7 +192,10 @@ def get_flat_params(dependant: Dependant) -> List[ModelField]:
|
||||||
|
|
||||||
|
|
||||||
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
|
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
|
||||||
signature = inspect.signature(call)
|
if sys.version_info >= (3, 10):
|
||||||
|
signature = inspect.signature(call, eval_str=True)
|
||||||
|
else:
|
||||||
|
signature = inspect.signature(call)
|
||||||
unwrapped = inspect.unwrap(call)
|
unwrapped = inspect.unwrap(call)
|
||||||
globalns = getattr(unwrapped, "__globals__", {})
|
globalns = getattr(unwrapped, "__globals__", {})
|
||||||
typed_params = [
|
typed_params = [
|
||||||
|
|
@ -217,7 +221,10 @@ def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:
|
||||||
|
|
||||||
|
|
||||||
def get_typed_return_annotation(call: Callable[..., Any]) -> Any:
|
def get_typed_return_annotation(call: Callable[..., Any]) -> Any:
|
||||||
signature = inspect.signature(call)
|
if sys.version_info >= (3, 10):
|
||||||
|
signature = inspect.signature(call, eval_str=True)
|
||||||
|
else:
|
||||||
|
signature = inspect.signature(call)
|
||||||
unwrapped = inspect.unwrap(call)
|
unwrapped = inspect.unwrap(call)
|
||||||
annotation = signature.return_annotation
|
annotation = signature.return_annotation
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from fastapi import Depends, FastAPI, Request
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
from .utils import needs_py310
|
||||||
|
|
||||||
|
|
||||||
|
class Dep:
|
||||||
|
def __call__(self, request: Request):
|
||||||
|
return "test"
|
||||||
|
|
||||||
|
|
||||||
|
@needs_py310
|
||||||
|
def test_stringified_annotations():
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
@app.get("/test/")
|
||||||
|
def call(test: Annotated[str, Depends(Dep())]):
|
||||||
|
return {"test": test}
|
||||||
|
|
||||||
|
response = client.get("/test")
|
||||||
|
assert response.status_code == 200
|
||||||
Loading…
Reference in New Issue