Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_state-0.2.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_state-0.2.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

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

Hashes for fastapi_state-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c6231b00a002b3e400b9e73beea73ebf2c11a461537b8ad01235fc0d758a71ae
MD5 33fcc9cc3098e59b2a79a78180bb1172
BLAKE2b-256 49d133b8be8a659a4148c4559dc09067f0450e599ca040db780fbf57c007073a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_state-0.2.0.tar.gz:

Publisher: publish.yml on AgroDT/fastapi-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for fastapi_state-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e96e17cdfc4d2ee6d8455ed0eb1815d1e47b27118dc6f361709dbbc770f13111
MD5 61b7587491fe99713f565f291eb2edc1
BLAKE2b-256 3e8776ca0d601e355df19e1196510872b973e20a2212140852b912f56c07dd14

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_state-0.2.0-py3-none-any.whl:

Publisher: publish.yml on AgroDT/fastapi-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page