@@ -79,7 +79,7 @@ def _get_obj_locals(obj) -> Optional[Dict[str, Any]]:
7979
8080
8181def class_name (input_type ):
82- if input_type in {list , set } and str (
82+ if input_type in {list , set } and str ( # noqa: E721
8383 type (input_type ) == "<class 'types.GenericAlias'>"
8484 ):
8585 # for Python 3.9 list[T], set[T]
@@ -233,9 +233,9 @@ class ActivationScope:
233233 def __init__ (
234234 self ,
235235 provider : Optional ["Services" ] = None ,
236- scoped_services : Optional [Dict [Type [T ], T ]] = None ,
236+ scoped_services : Optional [Dict [Union [ Type [T ], str ], T ]] = None ,
237237 ):
238- self .provider = provider
238+ self .provider = provider or Services ()
239239 self .scoped_services = scoped_services or {}
240240
241241 def __enter__ (self ):
@@ -248,7 +248,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
248248
249249 def get (
250250 self ,
251- desired_type : Union [Type [T ], str ],
251+ desired_type : Union [Union [ Type [T ], str ], str ],
252252 scope : Optional ["ActivationScope" ] = None ,
253253 * ,
254254 default : Optional [Any ] = ...,
@@ -713,13 +713,14 @@ def get(
713713 scope = ActivationScope (self )
714714
715715 resolver = self ._map .get (desired_type )
716+ scoped_service = scope .scoped_services .get (desired_type ) if scope else None
716717
717- if not resolver :
718+ if not resolver and not scoped_service :
718719 if default is not ...:
719720 return cast (T , default )
720721 raise CannotResolveTypeException (desired_type )
721722
722- return cast (T , resolver (scope , desired_type ))
723+ return cast (T , scoped_service or resolver (scope , desired_type ))
723724
724725 def _get_getter (self , key , param ):
725726 if param .annotation is _empty :
@@ -756,13 +757,15 @@ def get_executor(self, method: Callable) -> Callable:
756757
757758 if iscoroutinefunction (method ):
758759
759- async def async_executor (scoped : Optional [Dict [Type , Any ]] = None ):
760+ async def async_executor (
761+ scoped : Optional [Dict [Union [Type , str ], Any ]] = None
762+ ):
760763 with ActivationScope (self , scoped ) as context :
761764 return await method (* [fn (context ) for fn in fns ])
762765
763766 return async_executor
764767
765- def executor (scoped : Optional [Dict [Type , Any ]] = None ):
768+ def executor (scoped : Optional [Dict [Union [ Type , str ] , Any ]] = None ):
766769 with ActivationScope (self , scoped ) as context :
767770 return method (* [fn (context ) for fn in fns ])
768771
0 commit comments