mirror of https://github.com/tiangolo/fastapi.git
fix: pre-commit and type checks (ruff E731, mypy/ty cli and applications)
Made-with: Cursor
This commit is contained in:
parent
8b2028cb25
commit
c046d33b27
|
|
@ -1025,8 +1025,13 @@ class FastAPI(Starlette):
|
|||
"""
|
||||
),
|
||||
] = {}
|
||||
_inner_lifespan: Callable[[Any], Any]
|
||||
if lifespan is None:
|
||||
_inner_lifespan = lambda app: routing._DefaultLifespan(app.router)
|
||||
|
||||
def _default_lifespan(app: Any) -> Any:
|
||||
return routing._DefaultLifespan(app.router)
|
||||
|
||||
_inner_lifespan = _default_lifespan
|
||||
elif inspect.isasyncgenfunction(lifespan):
|
||||
_inner_lifespan = asynccontextmanager(lifespan)
|
||||
elif inspect.isgeneratorfunction(lifespan):
|
||||
|
|
@ -1050,7 +1055,7 @@ class FastAPI(Starlette):
|
|||
generate_unique_id_function=generate_unique_id_function,
|
||||
strict_content_type=strict_content_type,
|
||||
)
|
||||
self.router._fastapi_app = self
|
||||
self.router._fastapi_app = self # type: ignore[attr-defined]
|
||||
self.exception_handlers: dict[
|
||||
Any, Callable[[Request, Any], Response | Awaitable[Response]]
|
||||
] = {} if exception_handlers is None else dict(exception_handlers)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
try:
|
||||
from fastapi_cli.cli import main as cli_main
|
||||
from fastapi_cli.cli import main as cli_main # type: ignore[import-not-found]
|
||||
|
||||
except ImportError: # pragma: no cover
|
||||
cli_main = None # type: ignore
|
||||
cli_main = None
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not cli_main: # type: ignore[truthy-function] # ty: ignore[unused-ignore-comment]
|
||||
if not cli_main:
|
||||
message = 'To use the fastapi command, please install "fastapi[standard]":\n\n\tpip install "fastapi[standard]"\n'
|
||||
print(message)
|
||||
raise RuntimeError(message) # noqa: B904
|
||||
|
|
|
|||
|
|
@ -328,13 +328,18 @@ def get_dependant(
|
|||
'"request", it cannot depend on dependencies with scope "function".'
|
||||
)
|
||||
# Lifespan-scoped dependencies can only depend on other lifespan-scoped deps.
|
||||
if dependant.computed_scope == "lifespan" and param_details.depends.scope not in (
|
||||
None,
|
||||
"lifespan",
|
||||
if (
|
||||
dependant.computed_scope == "lifespan"
|
||||
and param_details.depends.scope
|
||||
not in (
|
||||
None,
|
||||
"lifespan",
|
||||
)
|
||||
):
|
||||
assert dependant.call
|
||||
call_name = getattr(dependant.call, "__name__", "<unnamed_callable>")
|
||||
raise DependencyScopeError(
|
||||
f'The dependency "{dependant.call.__name__}" has a scope of '
|
||||
f'The dependency "{call_name}" has a scope of '
|
||||
'"lifespan", it cannot depend on dependencies with scope '
|
||||
f'"{param_details.depends.scope}".'
|
||||
)
|
||||
|
|
|
|||
|
|
@ -231,7 +231,6 @@ async def _run_lifespan_dependencies(
|
|||
) -> None:
|
||||
"""Solve all lifespan-scoped dependencies and fill dependency_cache."""
|
||||
from starlette.requests import Request
|
||||
from starlette.types import Receive, Send
|
||||
|
||||
lifespan_deps = _collect_lifespan_dependants(router)
|
||||
if not lifespan_deps:
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ def test_lifespan_dependency_with_custom_lifespan() -> None:
|
|||
|
||||
@app.get("/")
|
||||
def root(
|
||||
pool: Annotated[str, Depends(get_pool, scope="lifespan")]
|
||||
pool: Annotated[str, Depends(get_pool, scope="lifespan")],
|
||||
) -> dict[str, str]:
|
||||
return {"pool": pool}
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ def test_lifespan_dependency_same_instance_across_requests() -> None:
|
|||
|
||||
@app.get("/")
|
||||
def root(
|
||||
s: Annotated[object, Depends(get_singleton, scope="lifespan")]
|
||||
s: Annotated[object, Depends(get_singleton, scope="lifespan")],
|
||||
) -> dict[str, bool]:
|
||||
return {"is_singleton": len(instances) == 1 and s is instances[0]}
|
||||
|
||||
|
|
@ -123,6 +123,7 @@ def test_collect_lifespan_dependants_route_level_scope() -> None:
|
|||
|
||||
def test_lifespan_dependency_synthetic_request_receive_send() -> None:
|
||||
"""Lifespan dep that uses Request.receive covers noop_receive during startup."""
|
||||
|
||||
async def lifespan_dep(request: Request) -> str:
|
||||
await request.receive()
|
||||
return "ok"
|
||||
|
|
|
|||
Loading…
Reference in New Issue