11import inspect
2- from typing import Any , Dict , Type
2+
3+ from typing import Any , Dict , Optional , TypeVar , Type
34
45from dependency_injection .registration import Registration
56from dependency_injection .scope import DEFAULT_SCOPE_NAME , Scope
67from dependency_injection .utils .singleton_meta import SingletonMeta
78
9+ Self = TypeVar ('Self' , bound = 'DependencyContainer' )
10+
811
912DEFAULT_CONTAINER_NAME = "default_container"
1013
@@ -17,7 +20,7 @@ def __init__(self, name: str=None):
1720 self ._scoped_instances = {}
1821
1922 @classmethod
20- def get_instance (cls , name : str = None ):
23+ def get_instance (cls , name : str = None ) -> Self :
2124 if name is None :
2225 name = DEFAULT_CONTAINER_NAME
2326
@@ -26,22 +29,22 @@ def get_instance(cls, name: str=None):
2629
2730 return cls ._instances [(cls , name )]
2831
29- def register_transient (self , dependency : Type , implementation : Type , constructor_args = None ):
32+ def register_transient (self , dependency : Type , implementation : Type , constructor_args : Optional [ Dict [ str , Any ]] = None ) -> None :
3033 if dependency in self ._registrations :
3134 raise ValueError (f"Dependency { dependency } is already registered." )
3235 self ._registrations [dependency ] = Registration (dependency , implementation , Scope .TRANSIENT , constructor_args )
3336
34- def register_scoped (self , dependency : Type , implementation : Type , constructor_args = None ):
37+ def register_scoped (self , dependency : Type , implementation : Type , constructor_args : Optional [ Dict [ str , Any ]] = None ) -> None :
3538 if dependency in self ._registrations :
3639 raise ValueError (f"Dependency { dependency } is already registered." )
3740 self ._registrations [dependency ] = Registration (dependency , implementation , Scope .SCOPED , constructor_args )
3841
39- def register_singleton (self , dependency : Type , implementation : Type , constructor_args = None ):
42+ def register_singleton (self , dependency : Type , implementation : Type , constructor_args : Optional [ Dict [ str , Any ]] = None ) -> None :
4043 if dependency in self ._registrations :
4144 raise ValueError (f"Dependency { dependency } is already registered." )
4245 self ._registrations [dependency ] = Registration (dependency , implementation , Scope .SINGLETON , constructor_args )
4346
44- def resolve (self , dependency : Type , scope_name = DEFAULT_SCOPE_NAME ):
47+ def resolve (self , dependency : Type , scope_name : str = DEFAULT_SCOPE_NAME ) -> Type :
4548 if scope_name not in self ._scoped_instances :
4649 self ._scoped_instances [scope_name ] = {}
4750
@@ -106,7 +109,7 @@ def _validate_constructor_args(self, constructor_args: Dict[str, Any], implement
106109 f"Constructor argument '{ arg_name } ' has an incompatible type. "
107110 f"Expected type: { expected_type } , provided type: { type (arg_value )} ." )
108111
109- def _inject_dependencies (self , implementation , scope_name = None , constructor_args = None ):
112+ def _inject_dependencies (self , implementation : Type , scope_name : str = None , constructor_args : Optional [ Dict [ str , Any ]] = None ) -> Type :
110113 constructor = inspect .signature (implementation .__init__ )
111114 params = constructor .parameters
112115
0 commit comments