new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

dependencies

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Depends() import and usage

The Depends() function is imported from fastapi and is used to declare dependencies in FastAPI. It takes a callable as its parameter.

Security() import and usage

The Security() function is imported from fastapi and is used to handle security scenarios including authorization and authentication with dependencies. It can declare OAuth2 scopes, unlike Depends().

When to use Security() instead of Depends()

Use Security() instead of Depends() when you want to declare OAuth2 scopes for security handling.

FastAPI has dependency injection system

FastAPI provides a powerful and easy to use dependency injection system.

Parameterized dependencies with callable instances

In Python, a class instance can be made callable by declaring a `__call__` method. FastAPI uses `__call__` to search for additional parameters and sub-dependencies, and calls it later to pass a value to the parameter in the path operation function. The `__init__` method is used to declare parameters of the instance that parametrize the dependency, and FastAPI never touches or concerns itself with `__init__` — it is used directly in the code.

Using callable instances as dependencies

A callable class instance should be passed to `Depends(checker)` instead of `Depends(ClassName)`, where `checker` is an instance of the class. When resolving the dependency, FastAPI calls the instance like `checker(q="somequery")` and passes the returned value as the parameter in the path operation function.

Depends scope parameter for yield dependencies

FastAPI version 0.121.0 added support for `Depends(scope="function")` for yield dependencies. With `scope="function"`, the exit code after `yield` is executed directly after the path operation function ends, before the response is sent to the client. With `scope="request"` (the default), the exit code after `yield` is executed after the response is sent.

Yield dependencies before FastAPI 0.118.0 behavior

Before FastAPI 0.118.0, when using a dependency with `yield`, the exit code after the path operation function was executed, but immediately before the response was sent. This meant that if a `StreamingResponse` was returned, the exit code of the yield dependency would already have been executed, preventing a database session in the yield dependency from being used during response streaming.

Yield dependencies from FastAPI 0.118.0 onwards

Starting from FastAPI 0.118.0, the exit code after `yield` is executed after the response is sent. This allows yielded resources like database sessions to remain available during `StreamingResponse` streaming and provides similar behavior to pre-0.106.0 versions but with improvements and bugfixes for special cases.

Explicitly closing resources in yield dependencies

For yield dependencies with SQLModel or SQLAlchemy, you can explicitly close the session after it is no longer needed, even if the path operation function or response streaming continues. This releases the database connection so other requests can use it, addressing cases where a resource is used only in the dependency but the response takes a long time to send.

Exception handling in yield dependencies before FastAPI 0.110.0

Before FastAPI 0.110.0, if a yield dependency caught an exception with `except` and did not re-throw it, the exception would automatically be forwarded to any exception handlers or the internal server error handler. This behavior was changed in 0.110.0 to prevent unhandled memory consumption and to be consistent with normal Python code behavior.

Background tasks and yield dependencies before FastAPI 0.106.0

Before FastAPI 0.106.0, throwing exceptions after `yield` was not possible. The exit code in yield dependencies was executed after the response was sent, allowing yielded objects from dependencies to be used in background tasks. This design ensured the exit code was only executed after background tasks completed.

Give your agent this brain