Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastAPI: AttributeError: 'Provide' object has no attribute 'test_message' #729

Closed
MaxiPigna opened this issue Aug 2, 2023 · 5 comments
Closed

Comments

@MaxiPigna
Copy link

Hello folks! I get the error AttributeError: 'Provide' object has no attribute 'test_message'
This is the code

containers.py

from dependency_injector import containers, providers
from services.conversationService import ConversationService

class Container(containers.DeclarativeContainer):
    conversationService = providers.Factory(
        ConversationService
    )

conversationBaseService.py

from abc import abstractmethod


class ConversationBaseService:
    @abstractmethod
    def test_message(self, message: str) -> str:
        pass


conversationService.py

from services.conversationBaseService import ConversationBaseService


class ConversationService(ConversationBaseService):
        def test_message(self, message: str) -> str:
            print("Not Implemented")
            return "Sorry"

main.py

from services.conversationBaseService import ConversationBaseService
from containers import Container
from fastapi import Depends, FastAPI
from dependency_injector.wiring import inject, Provide

container = Container()
app = FastAPI()

@app.post("/test")
@inject
def test_api(
    conversationService: ConversationBaseService = Depends(Provide[Container.conversationService])
    ):
    conversationService.test_message("Ciao")

Library versions:
Name: dependency-injector
Version: 4.41.0

Name: fastapi
Version: 0.100.1

Did I forget to configure something? I checked the documentation, but I didn't find a solution. Actually, the published example doesn't seems to be correct/updated.

@billsioros
Copy link

Have you wired the modules? FYI I'm a noob myself 😊

@martlaf
Copy link

martlaf commented Aug 7, 2023

Not the most familiar with injection with FastAPI, but you can try with dependency-injector 4.38.0, see here: #672

@MaxiPigna
Copy link
Author

Have you wired the modules? FYI I'm a noob myself 😊

Correct, I moved the APIs in another module and added
wiring_config = containers.WiringConfiguration(modules=["api.conversations"])

@GGJdevelope
Copy link

#folder structure

app
-llm
--container.py
--adapter
---input
----api
-----v1
------llm.py

    wiring_config = WiringConfiguration(packages=['app']) #not working
    wiring_config = WiringConfiguration(packages=['app.llm.adapter.input.api.v1]) #not working
    wiring_config = WiringConfiguration(packages=['app.llm.adapter.input.api.v1.llm']) #working ✅ path to file(this file is router which contain @injection decoration)

modules argument also working same

@romantolkachyov
Copy link

I've used modules to wire packages and found weird behaviour:

If I wire module app.routers and have module app.router.process with @inject statements inside it will work while handlers are async. But it doesn't work with sync handlers.

The correct solution is to use packages argument but I believe the behavior should be consistent for sync and async methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants