From a975a1704e4120b61e812f5d2935d7b6c1956fef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 17 Dec 2025 16:28:11 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 8 ++------ tests/test_annotated_forward_ref.py | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 57a32b0155..8998c73679 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -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) diff --git a/tests/test_annotated_forward_ref.py b/tests/test_annotated_forward_ref.py index d46bce017b..b82547197a 100644 --- a/tests/test_annotated_forward_ref.py +++ b/tests/test_annotated_forward_ref.py @@ -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__":