From 3092b9d8f5e047cfd875295bb7554a1fab2e54e7 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Wed, 26 Aug 2020 17:41:37 -0400 Subject: [PATCH] Add stubs for singletons --- src/dependency_injector/providers.pyi | 20 +++++++++++++ tests/typing/singleton.py | 41 ++++++++++++++++----------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index c4eac7b8..57afabc3 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -227,6 +227,26 @@ class Singleton(BaseSingleton): ... class DelegatedSingleton(Singleton): ... +class ThreadSafeSingleton(Singleton): ... + + +class DelegatedThreadSafeSingleton(ThreadSafeSingleton): ... + + +class ThreadLocalSingleton(BaseSingleton): ... + + +class DelegatedThreadLocalSingleton(ThreadLocalSingleton): ... + + +class AbstractSingleton(BaseSingleton): + def override(self, provider: BaseSingleton) -> OverridingContext: ... + + +class SingletonDelegate(Delegate): + def __init__(self, factory: BaseSingleton): ... + + class ProvidedInstanceFluentInterface: def __getattr__(self, item: str) -> AttributeGetter: ... def __getitem__(self, item: str) -> ItemGetter: ... diff --git a/tests/typing/singleton.py b/tests/typing/singleton.py index 51c41b93..a31e20fd 100644 --- a/tests/typing/singleton.py +++ b/tests/typing/singleton.py @@ -42,23 +42,30 @@ attr_getter5: providers.AttributeGetter = provider5.provided.attr item_getter5: providers.ItemGetter = provider5.provided['item'] method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324) -# Test 6: to check the DelegatedFactory +# Test 6: to check the DelegatedSingleton provider6 = providers.DelegatedSingleton(Cat) animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0) -# # Test 7: to check the AbstractFactory -# provider7 = providers.AbstractFactory(Animal) -# provider7.override(providers.Factory(Cat)) -# animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0) -# -# # Test 8: to check the FactoryDelegate __init__ -# provider8 = providers.FactoryDelegate(providers.Factory(object)) -# -# # Test 9: to check FactoryAggregate provider -# provider9 = providers.FactoryAggregate( -# a=providers.Factory(object), -# b=providers.Factory(object), -# ) -# factory_a_9: providers.Factory = provider9.a -# factory_b_9: providers.Factory = provider9.b -# val9: Any = provider9('a') +# Test 7: to check the ThreadSafeSingleton +provider7: providers.BaseSingleton[Animal] = providers.ThreadSafeSingleton(Cat) +animal7: Animal = provider7() + +# Test 8: to check the DelegatedThreadSafeSingleton +provider8 = providers.DelegatedThreadSafeSingleton(Cat) +animal8: Animal = provider8(1, 2, 3, b='1', c=2, e=0.0) + +# Test 9: to check the ThreadLocalSingleton +provider9 = providers.ThreadLocalSingleton(Cat) +animal9: Animal = provider9(1, 2, 3, b='1', c=2, e=0.0) + +# Test 10: to check the DelegatedThreadLocalSingleton +provider10 = providers.DelegatedThreadLocalSingleton(Cat) +animal10: Animal = provider10(1, 2, 3, b='1', c=2, e=0.0) + +# Test 11: to check the AbstractSingleton +provider11 = providers.AbstractSingleton(Animal) +provider11.override(providers.Singleton(Cat)) +animal11: Animal = provider11(1, 2, 3, b='1', c=2, e=0.0) + +# Test 12: to check the SingletonDelegate __init__ +provider12 = providers.SingletonDelegate(providers.Singleton(object))