-
-
Notifications
You must be signed in to change notification settings - Fork 339
AttributeError: 'Provide' object has no attribute 'get_by_id' #782
-
I am constantly getting the following error, despite that I have declared a valid WiringConfiguration for the container.
AttributeError: 'Provide' object has no attribute 'get_by_id'
This is the structure of my project,
├── myapp
│ ├── __init__.py
│ ├── actions
│ │ ├── __init__.py
│ │ └── events.py
│ ├── container.py
│ ├── controllers
│ │ ├── __init__.py
│ │ └── doc.py
│ ├── repos
│ │ ├── __init__.py
│ │ ├── entities.py
│ ├── server
│ │ ├── __init__.py
│ │ ├── main.py
│ │ ├── routes
│ │ │ ├── __init__.py
│ │ │ └── process.py
│ └── workers
│ ├── __init__.py
│ ├── message_handlers.py
and here are the relevant files.
container.py
from dependency_injector import containers
from dependency_injector import providers
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
wiring_config = containers.WiringConfiguration(
packages=[
'.server.routes',
'.controllers',
'.actions',
'.workers.message_handlers',
],
)
session = providers.Factory(db_session)
repo = providers.Factory(Repo, session=session)
async def initialize(container: Container) -> Container:
container.init_resources()
return container
main.py
app = FastAPI()
app.include_router(process.router)
container = Container()
app.container = container
@app.on_event('startup')
async def startup_event() -> None:
app.container.wire(modules=[__name__]) # type: ignore[attr-defined]
await initialize(app.container) # type: ignore[attr-defined]
server/routes.py
@router.post(
path='/v1/process',
status_code=status.HTTP_201_CREATED,
response_class=Response,
)
@inject
async def create(
doc: DocRequestSchema,
repo: Repo = Depends(Provide[Container.repo]),
) -> None:
controller = Controller(repo=repo)
await controller.create(id=doc.id)
repo/entities.py
class Repo(AbstractRepository):
model = DocumentModel
async def get_by_id(self, id: UUID, /) -> Document | None:
query = select(self.model).filter(self.model.id == id)
async with self.session as s:
obj: DocumentModel | None = (await s.execute(query)).scalars().one_or_none()
if not obj:
return None
return Document.model_validate(obj, from_attributes=True)
controllers/doc.py
class DocumentController:
def __init__(
self,
repo: Repo = Provide[Container.repo],
):
self.repo = repo
async def create(
self,
id: UUID,
) -> Document:
doc = Document(
id=id,
)
return await self.repo.create(doc=doc)
actions/events.py
@inject
async def handle_doc(
id: UUID,
/,
repo: Repo = Provide[Container.repo],
) -> None:
doc = await doc.get_by_id(id)
if not doc:
logger.warning('warn')
workers/message_handlers.py
@inject
async def doc_handler(
message: aio_pika.abc.AbstractIncomingMessage,
/,
repo: Repo = Provide[Container.repo],
) -> None:
try:
payload = await decode_incoming_message(message=message)
await handle_doc(
payload['id'],
repo=repo,
)
await message.ack()
except Exception as e:
await message.reject()
The full error trace is:
2024年02月08日 18:31:16 File "/home/worker/app/myapp/workers/message_handlers.py", line 52, in doc_handler
2024年02月08日 18:31:16 await handle_doc(
2024年02月08日 18:31:16 File "/usr/local/lib/python3.10/site-packages/dependency_injector/wiring.py", line 994, in _patched
2024年02月08日 18:31:16 return await _async_inject(
2024年02月08日 18:31:16 File "src/dependency_injector/_cwiring.pyx", line 66, in _async_inject
2024年02月08日 18:31:16 File "/home/worker/app/myapp/actions/events.py", line 42, in handle_doc
2024年02月08日 18:31:16 doc = await repo.get_by_id(id)
2024年02月08日 18:31:16 AttributeError: 'Provide' object has no attribute 'get_by_id'
I am pretty confident the wiring is OK so I am not too sure what is going on here. Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment