Skip to content

Commit

Permalink
service locator type hints (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
keotl committed May 29, 2021
1 parent b8c55d5 commit a9136ac
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions jivago/inject/service_locator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Optional
from typing import Callable, Optional, TypeVar, Type, Union, List

from jivago.inject import typing_meta_helper
from jivago.inject.exception.instantiation_exception import InstantiationException
Expand All @@ -8,6 +8,9 @@
from jivago.lang.registry import Registry
from jivago.lang.stream import Stream

T = TypeVar("T")
S = TypeVar("S")


class ServiceLocator(object):
def __init__(self, registry=Registry()):
Expand All @@ -17,7 +20,7 @@ def __init__(self, registry=Registry()):
self.providers = {}
self.scopeCaches = []

def bind(self, interface, implementation):
def bind(self, interface: Type[T], implementation: Union[Type[T], Type[S], T, Callable[..., T]]):
if self.__is_provider_function(implementation):
self.providers[interface] = implementation
elif isinstance(implementation, type):
Expand All @@ -28,7 +31,7 @@ def bind(self, interface, implementation):
def register_scope(self, scope_cache: ScopeCache):
self.scopeCaches.append(scope_cache)

def get(self, interface: type):
def get(self, interface: Type[T]) -> T:
if interface in self.literals.keys():
return self.literals[interface]
if typing_meta_helper.is_typing_meta_collection(interface):
Expand All @@ -54,7 +57,7 @@ def get(self, interface: type):
scope.store(stored_component, instance)
return instance

def get_all(self, clazz: type):
def get_all(self, clazz: Type[T]) -> List[T]:
return Stream(self.literals.keys(), self.components.keys(), self.providers.keys()).filter(
lambda k: issubclass(k, clazz)).map(lambda k: self.get(k)).toList()

Expand Down

0 comments on commit a9136ac

Please sign in to comment.