Metadata-Version: 2.4
Name: di-flask
Version: 0.1.4
Summary: FastAPI-style dependency injection for Flask using Annotated and Python 3.12 features.
Project-URL: Homepage, https://github.com/razorblade23/di-flask
Project-URL: Issues, https://github.com/razorblade23/di-flask/issues
Author-email: razorblade23 <contact@razorblade23.dev>
License: MIT License
        
        Copyright (c) 2025 razorblade23
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: annotated,dependency-injection,di,fastapi,flask
Requires-Python: >=3.12
Requires-Dist: flask>=2.3
Description-Content-Type: text/markdown

# Flask-DI

A minimal and clean FastAPI-style dependency injection system for Flask.

## Features

- FastAPI-style `Depends()`
- Automatic injection (no decorators required)
- Nested dependencies
- Override system for testing
- Per-request caching
- Pure Python and lightweight

## Installation

```bash
pip install flask-di
```

## Usage
> [!NOTE]
> For automatic injection of dependecies we need to wrap Flask class

```python
from flask_di import DIFlask, Depends

app = DIFlask(__name__)

def get_db():
    return {"session": "db-session"}

def get_user(db=Depends(get_db)):
    return {"username": "alice", "db": db}

@app.route("/info")
def info(user=Depends(get_user)):
    return user
```
> [!TIP]
> Flask-DI also supports `Annotated` type alias for declaring dependecies just like FastAPI does.
> 
> ```python
> from flask_di import DIFlask, Depends
> from typing import Annotated
> 
> app = DIFlask(__name__)
> 
> def get_db():
>     return {"session": "db-session"}
> 
> SessionDep = Annotated(dict, Depends(get_db))
> 
> def get_user(session: SessionDep):
>     return {"username": "alice", "session": session}
> 
> UserDep = Annotated(dict, Depends(get_user))
> 
> @app.route("/info")
> def info(user: UserDep):
>     return user
> ```

## Overrides
Flask-DI supports overrides for easy mocking and testing of dependacies.

```python
def override_db():
    return {"session": "test-db"}

app.dependency_overrides[get_db] = override_db
```

## Disclaimer
This is my snippet of code that I use for my Flask project. 

Just wanted to share as its dead simple, lightweight and pure python.

## Contribution
You are free to contribute to the project in any way you want.

The goal of the project is to be on-par with FastAPI DI style. Please keep it simple, concise and with clear code comments.