Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
✨ Allow using dependables withfunctools.partial()#9753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
tiangolo merged 20 commits intofastapi:masterfromlieryan:lieryan-dependencies-with-functools-partialDec 2, 2025
+256 −2
Merged
Changes fromall commits
Commits
Show all changes
20 commits Select commitHold shift + click to select a range
d689b00 Rename tests/test_dependency_class.py -> tests/test_dependency_types.py
lieryan57e1718 Add test for sync/async function/gen dependencies
lieryanf16dd38 Add support for functools.partial()-wrapped dependables
lieryan3ba104e 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot]05ab699 Fix async_gen_dependency() should use yield; also fix type annotation
lieryanb593e49 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot]f20d338 Merge branch 'master' into lieryan-dependencies-with-functools-partial
YuriiMotova6e9c03 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot]5407429 Merge remote-tracking branch 'upstream/master' into lieryan-dependenc…
YuriiMotov9a434c4 Restore `test_dependency_class.py`
YuriiMotovce77af9 Clean up tests for partial dependencies and rename to `test_dependenc…
YuriiMotov41d7016 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot]53b67a2 Merge branch 'master' into lieryan-dependencies-with-functools-partial
YuriiMotov0448ff1 Merge branch 'master' into lieryan-dependencies-with-functools-partial
YuriiMotovd193739 Merge remote-tracking branch 'upstream/master' into lieryan-dependenc…
YuriiMotova5f1199 Remove unused import, make changes in `models.py`
YuriiMotovc687deb Merge branch 'master' into lieryan-dependencies-with-functools-partial
YuriiMotov1202318 ♻️ Move partial extraction logic to Dependant._unwrapped_call
tiangolod940395 ♻️ Tweak tests to use Annotated for dependencies
tiangolo128fdcc Merge branch 'master' into lieryan-dependencies-with-functools-partial
tiangoloFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 5 additions & 2 deletionsfastapi/dependencies/models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletionstests/test_dependency_partial.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| from functools import partial | ||
| from typing import AsyncGenerator, Generator | ||
| import pytest | ||
| from fastapi import Depends, FastAPI | ||
| from fastapi.testclient import TestClient | ||
| from typing_extensions import Annotated | ||
| app = FastAPI() | ||
| def function_dependency(value: str) -> str: | ||
| return value | ||
| async def async_function_dependency(value: str) -> str: | ||
| return value | ||
| def gen_dependency(value: str) -> Generator[str, None, None]: | ||
| yield value | ||
| async def async_gen_dependency(value: str) -> AsyncGenerator[str, None]: | ||
| yield value | ||
| class CallableDependency: | ||
| def __call__(self, value: str) -> str: | ||
| return value | ||
| class CallableGenDependency: | ||
| def __call__(self, value: str) -> Generator[str, None, None]: | ||
| yield value | ||
| class AsyncCallableDependency: | ||
| async def __call__(self, value: str) -> str: | ||
| return value | ||
| class AsyncCallableGenDependency: | ||
| async def __call__(self, value: str) -> AsyncGenerator[str, None]: | ||
| yield value | ||
| class MethodsDependency: | ||
| def synchronous(self, value: str) -> str: | ||
| return value | ||
| async def asynchronous(self, value: str) -> str: | ||
| return value | ||
| def synchronous_gen(self, value: str) -> Generator[str, None, None]: | ||
| yield value | ||
| async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]: | ||
| yield value | ||
| callable_dependency = CallableDependency() | ||
| callable_gen_dependency = CallableGenDependency() | ||
| async_callable_dependency = AsyncCallableDependency() | ||
| async_callable_gen_dependency = AsyncCallableGenDependency() | ||
| methods_dependency = MethodsDependency() | ||
| @app.get("/partial-function-dependency") | ||
| async def get_partial_function_dependency( | ||
| value: Annotated[ | ||
| str, Depends(partial(function_dependency, "partial-function-dependency")) | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-async-function-dependency") | ||
| async def get_partial_async_function_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial(async_function_dependency, "partial-async-function-dependency") | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-gen-dependency") | ||
| async def get_partial_gen_dependency( | ||
| value: Annotated[str, Depends(partial(gen_dependency, "partial-gen-dependency"))], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-async-gen-dependency") | ||
| async def get_partial_async_gen_dependency( | ||
| value: Annotated[ | ||
| str, Depends(partial(async_gen_dependency, "partial-async-gen-dependency")) | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-callable-dependency") | ||
| async def get_partial_callable_dependency( | ||
| value: Annotated[ | ||
| str, Depends(partial(callable_dependency, "partial-callable-dependency")) | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-callable-gen-dependency") | ||
| async def get_partial_callable_gen_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency")), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-async-callable-dependency") | ||
| async def get_partial_async_callable_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial(async_callable_dependency, "partial-async-callable-dependency") | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-async-callable-gen-dependency") | ||
| async def get_partial_async_callable_gen_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial( | ||
| async_callable_gen_dependency, "partial-async-callable-gen-dependency" | ||
| ) | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-synchronous-method-dependency") | ||
| async def get_partial_synchronous_method_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial( | ||
| methods_dependency.synchronous, "partial-synchronous-method-dependency" | ||
| ) | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-synchronous-method-gen-dependency") | ||
| async def get_partial_synchronous_method_gen_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial( | ||
| methods_dependency.synchronous_gen, | ||
| "partial-synchronous-method-gen-dependency", | ||
| ) | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-asynchronous-method-dependency") | ||
| async def get_partial_asynchronous_method_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial( | ||
| methods_dependency.asynchronous, | ||
| "partial-asynchronous-method-dependency", | ||
| ) | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| @app.get("/partial-asynchronous-method-gen-dependency") | ||
| async def get_partial_asynchronous_method_gen_dependency( | ||
| value: Annotated[ | ||
| str, | ||
| Depends( | ||
| partial( | ||
| methods_dependency.asynchronous_gen, | ||
| "partial-asynchronous-method-gen-dependency", | ||
| ) | ||
| ), | ||
| ], | ||
| ) -> str: | ||
| return value | ||
| client = TestClient(app) | ||
| @pytest.mark.parametrize( | ||
| "route,value", | ||
| [ | ||
| ("/partial-function-dependency", "partial-function-dependency"), | ||
| ( | ||
| "/partial-async-function-dependency", | ||
| "partial-async-function-dependency", | ||
| ), | ||
| ("/partial-gen-dependency", "partial-gen-dependency"), | ||
| ("/partial-async-gen-dependency", "partial-async-gen-dependency"), | ||
| ("/partial-callable-dependency", "partial-callable-dependency"), | ||
| ("/partial-callable-gen-dependency", "partial-callable-gen-dependency"), | ||
| ("/partial-async-callable-dependency", "partial-async-callable-dependency"), | ||
| ( | ||
| "/partial-async-callable-gen-dependency", | ||
| "partial-async-callable-gen-dependency", | ||
| ), | ||
| ( | ||
| "/partial-synchronous-method-dependency", | ||
| "partial-synchronous-method-dependency", | ||
| ), | ||
| ( | ||
| "/partial-synchronous-method-gen-dependency", | ||
| "partial-synchronous-method-gen-dependency", | ||
| ), | ||
| ( | ||
| "/partial-asynchronous-method-dependency", | ||
| "partial-asynchronous-method-dependency", | ||
| ), | ||
| ( | ||
| "/partial-asynchronous-method-gen-dependency", | ||
| "partial-asynchronous-method-gen-dependency", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_dependency_types_with_partial(route: str, value: str) -> None: | ||
| response = client.get(route) | ||
| assert response.status_code == 200, response.text | ||
| assert response.json() == value |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.