python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

mypy error: "FastAPI" has no attribute "container"

Open SpyrosRoum opened this issue 3 years ago • 5 comments
trafficstars

dependecy-injector version: "^4.40.0" FastAPI version: "^0.79.1"

Hello, following the example for FastAPI, mypy complains with the error on the title when I do app.container = container.

The only way I found to work around it (other than type: ignore) was to create a custom class that inherits from FastAPI:

class MyFastAPI(FastAPI):
    container: Container


def create_app() -> MyFastAPI:
    app = MyFastAPI()

    container = Container()
    app.container = container

    @app.on_event('startup')
    async def startup_event() -> None:
        app.container.wire(modules=[__name__])
        await initialize(app.container)

    return app

Is there any other way to do this? I'm a little surprised that I couldn't find any other issue about this so I'm wondering if I missed something.

SpyrosRoum avatar Sep 30 '22 15:09 SpyrosRoum