mirror of https://github.com/tiangolo/fastapi.git
🎨 Auto format
This commit is contained in:
parent
9e7ca4d454
commit
a975a1704e
|
|
@ -305,9 +305,7 @@ def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:
|
|||
return annotation
|
||||
|
||||
|
||||
def _try_resolve_annotated_string(
|
||||
annotation_str: str, globalns: Dict[str, Any]
|
||||
) -> Any:
|
||||
def _try_resolve_annotated_string(annotation_str: str, globalns: Dict[str, Any]) -> Any:
|
||||
"""
|
||||
Try to partially resolve an Annotated string annotation.
|
||||
|
||||
|
|
@ -347,9 +345,7 @@ def _try_resolve_annotated_string(
|
|||
|
||||
# Try to resolve the type part, keep as ForwardRef if it fails
|
||||
try:
|
||||
resolved_type = evaluate_forwardref(
|
||||
ForwardRef(type_part), globalns, globalns
|
||||
)
|
||||
resolved_type = evaluate_forwardref(ForwardRef(type_part), globalns, globalns)
|
||||
except Exception:
|
||||
resolved_type = ForwardRef(type_part)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
"""Test case for issue #13056: Can't use `Annotated` with `ForwardRef`"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
|
@ -11,12 +12,12 @@ app = FastAPI()
|
|||
|
||||
|
||||
def get_potato() -> Potato:
|
||||
return Potato(color='red', size=10)
|
||||
return Potato(color="red", size=10)
|
||||
|
||||
|
||||
@app.get('/')
|
||||
@app.get("/")
|
||||
async def read_root(potato: Annotated[Potato, Depends(get_potato)]):
|
||||
return {'color': potato.color, 'size': potato.size}
|
||||
return {"color": potato.color, "size": potato.size}
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -30,24 +31,26 @@ client = TestClient(app)
|
|||
|
||||
def test_annotated_forward_ref():
|
||||
"""Test that forward references work correctly with Annotated dependencies."""
|
||||
response = client.get('/')
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data == {'color': 'red', 'size': 10}
|
||||
assert data == {"color": "red", "size": 10}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
"""Test that OpenAPI schema is generated correctly."""
|
||||
response = client.get('/openapi.json')
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
schema = response.json()
|
||||
# The root path should NOT have query parameters for potato
|
||||
# It should only be a dependency
|
||||
root_path = schema['paths']['/']['get']
|
||||
root_path = schema["paths"]["/"]["get"]
|
||||
# Check that potato is not a query parameter
|
||||
parameters = root_path.get('parameters', [])
|
||||
potato_params = [p for p in parameters if 'potato' in p.get('name', '').lower()]
|
||||
assert len(potato_params) == 0, f"Potato should not appear as a query parameter: {potato_params}"
|
||||
parameters = root_path.get("parameters", [])
|
||||
potato_params = [p for p in parameters if "potato" in p.get("name", "").lower()]
|
||||
assert len(potato_params) == 0, (
|
||||
f"Potato should not appear as a query parameter: {potato_params}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in New Issue