python-dependency-injector/examples/providers/singleton_full_resetting.py
Roman Mogylatov 2fe0e00cef
Singleton.full_reset() (#391)
* Improve .traverse() typing stubs

* Fix container.reset_singletons()

* Add implementation, tests, and typing stubs

* Add docs and example

* Update changelog
2021-02-05 18:17:44 -05:00

32 lines
664 B
Python

"""`Singleton` provider full resetting example."""
from dependency_injector import containers, providers
class Database:
...
class UserService:
def __init__(self, db: Database):
self.db = db
class Container(containers.DeclarativeContainer):
database = providers.Singleton(Database)
user_service = providers.Singleton(UserService, db=database)
if __name__ == '__main__':
container = Container()
user_service1 = container.user_service()
container.user_service.full_reset()
user_service2 = container.user_service()
assert user_service2 is not user_service1
assert user_service2.db is not user_service1.db