From 580dafd94ea963f3dcdf9c46ec7592092399213e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Wed, 10 Dec 2025 13:00:12 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20usage=20of=20in?= =?UTF-8?q?spect.signature=20in=20its=20own=20function,=20for=20reuse=20an?= =?UTF-8?q?d=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 2abae433b..262dba6fd 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -209,7 +209,7 @@ def get_flat_params(dependant: Dependant) -> List[ModelField]: return path_params + query_params + header_params + cookie_params -def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: +def _get_signature(call: Callable[..., Any]) -> inspect.Signature: if sys.version_info >= (3, 10): try: signature = inspect.signature(call, eval_str=True) @@ -219,6 +219,11 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: signature = inspect.signature(call) else: signature = inspect.signature(call) + return signature + + +def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: + signature = _get_signature(call) unwrapped = inspect.unwrap(call) globalns = getattr(unwrapped, "__globals__", {}) typed_params = [ @@ -244,15 +249,7 @@ def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any: def get_typed_return_annotation(call: Callable[..., Any]) -> Any: - if sys.version_info >= (3, 10): - try: - signature = inspect.signature(call, eval_str=True) - except NameError: - # Handle type annotations with if TYPE_CHECKING, not used by FastAPI - # e.g. dependency return types - signature = inspect.signature(call) - else: - signature = inspect.signature(call) + signature = _get_signature(call) unwrapped = inspect.unwrap(call) annotation = signature.return_annotation