Fast API with Dependency-injector Python getting strategy_service.test(Test(name, id)) AttributeError: 'Provide' object has no attribute 'test'

Viewed 1677

I am using FastApI and dependency-injetor below is the project structure

-app 
    - api/v1/endpoints
    - repository
    - service
    main.py

When I am injecting the dependencies to the endpoints I always gets Provide object not actual class object due to this code always throws

AttributeError: 'Provide' object has no attribute 'test' However if I inject the dependencies in normal function it works well.

Here is below code snippet that I am using -

users.py as endpoint

@router.get('/u/{name}/{id}')
def add_user(name:str, id:int, strategy_service:StrategyService = Depends(Provide[Container.strategy_service])):
    strategy_service.test(Test(name, id))

Container - class

class Container(DeclarativeContainer):
    #Define configuration
    config = providers.Configuration()

    #Database
    db = providers.Singleton(Database, db_url=config.db_url)
    
    #All the repositories Configuration
    strategy_repository = providers.Factory(
        StrategyRepository,
        session_factory = db.provided.session
    )

#All the services configured
    strategy_service = providers.Factory(
        StrategyService,
        strategy_repository = strategy_repository
    )

main.py

    def create_app() -> FastAPI:
        container = Container()
        container.config.from_yaml('../config.yml')
         container.wire(packages=[api])
   
    
        app = FastAPI()
    
        app.container = container
    
        #Here configure all your routes
        app.include_router(api_v1.router, prefix='/api/v1')
    
        #Return the final app
        return app
    
    app = create_app()

if __name__ == '__main__':
    #We can get this host and port number from config files
    uvicorn.run(app, host = '127.0.0.1', port = 8080)

Getting this exception -

result = self.fn(*self.args, **self.kwargs) File "/Users/manpr06/driveE/AlgoBot/algotradersbot-service/app/api/v1/endpoints/users.py", line 21, in add_user strategy_service.test(Test(name, id)) AttributeError: 'Provide' object has no attribute 'test'

Can someone please help one this?

2 Answers

I do not know if it is directly related but after I downgraded dependency-injector from 4.39.1 to 4.38.0 it solved this issue and started working. Hope it helps!

You need setup wiring

class Container(DeclarativeContainer):
    wiring_config = containers.WiringConfiguration(
        modules=["src.routes.v1.users"] # or "users" in your case
    )

    # Define configuration
    config = providers.Configuration()
Related