同步操作将从 Gitee 极速下载/FastAPI 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from collections.abc import AsyncGenerator, Generatorfrom functools import partialfrom typing import Annotatedimport pytestfrom fastapi import Depends, FastAPIfrom fastapi.testclient import TestClientapp = FastAPI()def function_dependency(value: str) -> str:return valueasync def async_function_dependency(value: str) -> str:return valuedef gen_dependency(value: str) -> Generator[str, None, None]:yield valueasync def async_gen_dependency(value: str) -> AsyncGenerator[str, None]:yield valueclass CallableDependency:def __call__(self, value: str) -> str:return valueclass CallableGenDependency:def __call__(self, value: str) -> Generator[str, None, None]:yield valueclass AsyncCallableDependency:async def __call__(self, value: str) -> str:return valueclass AsyncCallableGenDependency:async def __call__(self, value: str) -> AsyncGenerator[str, None]:yield valueclass MethodsDependency:def synchronous(self, value: str) -> str:return valueasync def asynchronous(self, value: str) -> str:return valuedef synchronous_gen(self, value: str) -> Generator[str, None, None]:yield valueasync def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]:yield valuecallable_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 valueclient = 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.textassert response.json() == value
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。