← oracle_full

fastapi_9753

resolved RESOLVED UNSUBMITTED PASS · None tool calls · 0 s · fastapi/fastapi

Task input

✨ Allow using dependables with `functools.partial()`

Discussion: https://github.com/tiangolo/fastapi/discussions/9744

Using `functools.partial()` with dependables feels like a very obvious idiom. `partial()` already works for dependables that are plain function, but it doesn't work consistently, partial of async and callable classes didn't work consistently.

This PR makes partial() work consistently in all combinations.

test_dependency_class.py have been renamed to test_dependency_types.py to reflect its bigger scope now.

There's quite a bit of repetition in the tests, part of me wants to just refactor those; but another part of me also wants to keep the tests simple.

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Patch

--- a/fastapi/dependencies/models.py
+++ b/fastapi/dependencies/models.py
@@ -1,7 +1,7 @@
 import inspect
 import sys
 from dataclasses import dataclass, field
-from functools import cached_property
+from functools import cached_property, partial
 from typing import Any, Callable, List, Optional, Sequence, Union
 
 from fastapi._compat import ModelField
@@ -79,7 +79,10 @@ def _uses_scopes(self) -> bool:
     def _unwrapped_call(self) -> Any:
         if self.call is None:
             return self.call  # pragma: no cover
-        return inspect.unwrap(self.call)
+        unwrapped = inspect.unwrap(self.call)
+        if isinstance(unwrapped, partial):
+            unwrapped = unwrapped.func
+        return unwrapped
 
     @cached_property
     def is_gen_callable(self) -> bool:

Test output

show
............                                                             [100%]
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45
  /Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
    _PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
12 passed, 1 warning in 0.53s