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

Override a dependency in a container subclass #778

Open
warvariuc opened this issue Jan 19, 2024 · 0 comments
Open

Override a dependency in a container subclass #778

warvariuc opened this issue Jan 19, 2024 · 0 comments

Comments

@warvariuc
Copy link

warvariuc commented Jan 19, 2024

import dependency_injector.containers as dic
import dependency_injector.providers as dip


class UnitOfWork:
    ...


class TestUnitOfWork(UnitOfWork):
    ...


class ApplicationContainer(dic.DeclarativeContainer):
    unit_of_work_class = dip.Callable(UnitOfWork)
    uow = dip.Factory(unit_of_work_class)


class TestApplicationContainer(ApplicationContainer):
    unit_of_work_class = dip.Callable(TestUnitOfWork)


container = TestApplicationContainer()
print(container.uow())
with container.unit_of_work_class.override(TestUnitOfWork):
    print(container.uow())

The output:

<__main__.UnitOfWork object at 0x110ae9ed0>
<__main__.UnitOfWork object at 0x110ae9ed0>

I would expect that container.uow() to return an instance of TestUnitOfWork, since container is an instance of TestApplicationContainer which has unit_of_work_class = dip.Callable(TestUnitOfWork).

container.unit_of_work_class.override(TestUnitOfWork) also doesn't work, as you can see...

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

1 participant