Simple state management utilities for FastAPI applications.
Project description
FastAPI State
Simple state management utilities for FastAPI applications.
This package provides a decorator-based approach to define and inject custom application state objects. It supports both synchronous and asynchronous state initializers and ensures that state keys are unique within a FastAPI application's lifecycle.
Features
- Define application-wide state using decorators
- Support for both sync and async state initializers
- Works with both HTTP and WebSocket routes.
Limitations
- When using a custom name with
@state('...'), mypy may require a# type: ignore[arg-type]comment due to type narrowing limitations.
Installation
# Install with pip
pip install fastapi-state
# Or add to your project
uv add fastapi-state
Example
See more usage examples.
from contextlib import asynccontextmanager
from typing import Annotated, Any
import uvicorn
from fastapi import Body, Depends, FastAPI, HTTPException, status
from fastapi_state import state
def main() -> None:
uvicorn.run(f'{__name__}:app')
type Database = dict[str, Any]
@state
def database() -> Database:
return {}
@asynccontextmanager
async def lifespan(_app: FastAPI): # noqa: ANN201
state: dict = {}
await database.inject(state)
yield state
DatabaseDep = Annotated[Database, Depends(database.extract)]
app = FastAPI(lifespan=lifespan)
@app.put('/{key}')
async def put_item(key: str, value: Annotated[Any, Body()], db: DatabaseDep) -> Any: # noqa: ANN401
db[key] = value
return value
@app.get('/{key}')
async def get_item(key: str, db: DatabaseDep) -> Any: # noqa: ANN401
if key in db:
return db[key]
raise HTTPException(status.HTTP_404_NOT_FOUND)
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_state-0.2.0.tar.gz.
File metadata
- Download URL: fastapi_state-0.2.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6231b00a002b3e400b9e73beea73ebf2c11a461537b8ad01235fc0d758a71ae
|
|
| MD5 |
33fcc9cc3098e59b2a79a78180bb1172
|
|
| BLAKE2b-256 |
49d133b8be8a659a4148c4559dc09067f0450e599ca040db780fbf57c007073a
|
Provenance
The following attestation bundles were made for fastapi_state-0.2.0.tar.gz:
Publisher:
publish.yml on AgroDT/fastapi-state
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_state-0.2.0.tar.gz -
Subject digest:
c6231b00a002b3e400b9e73beea73ebf2c11a461537b8ad01235fc0d758a71ae - Sigstore transparency entry: 646932704
- Sigstore integration time:
-
Permalink:
AgroDT/fastapi-state@fab533900676989b926c33ce93d1b78ae1c95a15 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/AgroDT
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fab533900676989b926c33ce93d1b78ae1c95a15 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_state-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_state-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e96e17cdfc4d2ee6d8455ed0eb1815d1e47b27118dc6f361709dbbc770f13111
|
|
| MD5 |
61b7587491fe99713f565f291eb2edc1
|
|
| BLAKE2b-256 |
3e8776ca0d601e355df19e1196510872b973e20a2212140852b912f56c07dd14
|
Provenance
The following attestation bundles were made for fastapi_state-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on AgroDT/fastapi-state
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_state-0.2.0-py3-none-any.whl -
Subject digest:
e96e17cdfc4d2ee6d8455ed0eb1815d1e47b27118dc6f361709dbbc770f13111 - Sigstore transparency entry: 646932729
- Sigstore integration time:
-
Permalink:
AgroDT/fastapi-state@fab533900676989b926c33ce93d1b78ae1c95a15 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/AgroDT
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fab533900676989b926c33ce93d1b78ae1c95a15 -
Trigger Event:
push
-
Statement type: