Skip to content

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

Open
@SpyrosRoum

Description

@SpyrosRoum

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions