Name | fastapi-state JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | Simple state management utilities for FastAPI applications. |
upload_time | 2025-07-16 00:25:29 |
maintainer | None |
docs_url | None |
author | Petr Tsymbarovich |
requires_python | >=3.12 |
license | None |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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
```sh
# Install with pip
pip install fastapi-state
# Or add to your project
uv add fastapi-state
```
## Example
See more usage [examples](./examples).
```python
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
await database.inject(app)
yield
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](./LICENSE).
Raw data
{
"_id": null,
"home_page": null,
"name": "fastapi-state",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": null,
"author": "Petr Tsymbarovich",
"author_email": "Petr Tsymbarovich <petr@tsymbarovich.ru>",
"download_url": "https://files.pythonhosted.org/packages/56/3c/97b4dea43d72a468101828df9f42f30dd39b19d562e8e13cc2602f40e261/fastapi_state-0.1.0.tar.gz",
"platform": null,
"description": "# FastAPI State\r\n\r\nSimple state management utilities for FastAPI applications.\r\n\r\nThis package provides a decorator-based approach to define and inject\r\ncustom application state objects. It supports both synchronous and\r\nasynchronous state initializers and ensures that state keys are unique\r\nwithin a FastAPI application's lifecycle.\r\n\r\n## Features\r\n\r\n- Define application-wide state using decorators\r\n- Support for both sync and async state initializers\r\n- Works with both HTTP and WebSocket routes.\r\n\r\n## Limitations\r\n\r\n- When using a custom name with `@state('...')`, mypy may require a\r\n `# type: ignore[arg-type]` comment due to type narrowing limitations.\r\n\r\n## Installation\r\n\r\n```sh\r\n# Install with pip\r\npip install fastapi-state\r\n# Or add to your project\r\nuv add fastapi-state\r\n```\r\n\r\n## Example\r\n\r\nSee more usage [examples](./examples).\r\n\r\n```python\r\nfrom contextlib import asynccontextmanager\r\nfrom typing import Annotated, Any\r\n\r\nimport uvicorn\r\nfrom fastapi import Body, Depends, FastAPI, HTTPException, status\r\n\r\nfrom fastapi_state import state\r\n\r\n\r\ndef main() -> None:\r\n uvicorn.run(f'{__name__}:app')\r\n\r\n\r\ntype Database = dict[str, Any]\r\n\r\n\r\n@state\r\ndef database() -> Database:\r\n return {}\r\n\r\n\r\n@asynccontextmanager\r\nasync def lifespan(app: FastAPI): # noqa: ANN201\r\n await database.inject(app)\r\n yield\r\n\r\n\r\nDatabaseDep = Annotated[Database, Depends(database.extract)]\r\n\r\napp = FastAPI(lifespan=lifespan)\r\n\r\n\r\n@app.put('/{key}')\r\nasync def put_item(key: str, value: Annotated[Any, Body()], db: DatabaseDep) -> Any: # noqa: ANN401\r\n db[key] = value\r\n return value\r\n\r\n\r\n@app.get('/{key}')\r\nasync def get_item(key: str, db: DatabaseDep) -> Any: # noqa: ANN401\r\n if key in db:\r\n return db[key]\r\n\r\n raise HTTPException(status.HTTP_404_NOT_FOUND)\r\n```\r\n\r\n## License\r\n\r\nThis project is licensed under the [MIT License](./LICENSE).\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Simple state management utilities for FastAPI applications.",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/AgroDT/fastapi-state",
"Issues": "https://github.com/AgroDT/fastapi-state/issues",
"Repository": "https://github.com/AgroDT/fastapi-state"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d7ec5e81a587a4571fd065eaa3fb66cf78867305821bc09621fab6125dd4c70f",
"md5": "27beb991750a34cbc5ddd86ccf09a2c9",
"sha256": "72fd511995dcbaa8626965320a524e543a7c50a836e0bcad7aacc4e49deb16e0"
},
"downloads": -1,
"filename": "fastapi_state-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "27beb991750a34cbc5ddd86ccf09a2c9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 5161,
"upload_time": "2025-07-16T00:25:28",
"upload_time_iso_8601": "2025-07-16T00:25:28.374558Z",
"url": "https://files.pythonhosted.org/packages/d7/ec/5e81a587a4571fd065eaa3fb66cf78867305821bc09621fab6125dd4c70f/fastapi_state-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "563c97b4dea43d72a468101828df9f42f30dd39b19d562e8e13cc2602f40e261",
"md5": "d879d3e3e92c3c93648873326ff2c728",
"sha256": "0ea81bfb72ac6016407c01751e34931c6e36da069f5d9a9863734d731f05768b"
},
"downloads": -1,
"filename": "fastapi_state-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "d879d3e3e92c3c93648873326ff2c728",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 4215,
"upload_time": "2025-07-16T00:25:29",
"upload_time_iso_8601": "2025-07-16T00:25:29.758743Z",
"url": "https://files.pythonhosted.org/packages/56/3c/97b4dea43d72a468101828df9f42f30dd39b19d562e8e13cc2602f40e261/fastapi_state-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-16 00:25:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AgroDT",
"github_project": "fastapi-state",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-state"
}