Dependency in Base class doesn't appear to materialize
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from abc import ABC
from fastapi import Request
from core.repos import AuditRepo
class BaseUseCase(ABC):
def __init__(self, req: Request, audit : AuditRepo = Depends()
self.audit = audit
self.req = req
def handle(self):
self.audit.record(self.req) # This line throws AttributeError
self.execute()
class CancelOrder(BaseUseCase):
def __init__(self, some_other_dependency: SomeRepo = Depends()):
super().__init__()
self.some_other_dependency = some_other_dependency
def execute(self):
print("Cancelling Order")
self.some_other_dependency.do_something() # This works
Description
I’m stuck at a point where I need to use a common Dependency in the base class of all my usecases. For example, AuditTrail which needs to happen on all transactions.
To fulfil this requirement I set the audit as a dependency in the BaseUseCase class. However, when I call it on “self.audit.record()” it throws error: “AttributeError: ‘Depends’ object has no attribute ‘record’”
All other dependencies in CancelOrder work fine.
Am I doing something wrong here?
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
3.10.0
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Implicitly injecting dependency in Base class while derived ...
I thought that Dependency dep should get injected automatically(through Constructor Injection) when derived class is resolved. But this doesnt ...
Read more >Materialize: Documentation
Materialize is a modern responsive CSS framework based on Material Design by Google.
Read more >Visibility Modes in C++ with Examples - GeeksforGeeks
Protected Visibility mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base...
Read more >Refreshing a materialized view - Amazon Redshift
Amazon Redshift doesn't support incremental refresh for materialized views ... refresh materialized views with up-to-date data from its base tables when ...
Read more >3 Materialized View Concepts and Architecture
The following sections describe each type of materialized view and also describe some ... Advanced Replication does not support type inheritance.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tiangolo Do you have any insights into this please. There is another issue somebody opened and it appears either we are missing the knowledge to use dependency in base class.
Another developer opened an issue: #4681
I saw another issue https://github.com/tiangolo/fastapi/issues/1105 which appeared to be similar but I can understand that the target function was not in the request path.