22from glob import glob
33from inspect import Parameter , signature
44from pathlib import Path
5- from typing import Any , Dict , List , NamedTuple , Optional , Tuple , Type
5+ from typing import Any , Dict , List , NamedTuple , Optional , Tuple , Type , cast
66
77from ..helpers import (
88 import_module_and_get_attr ,
99 import_module_and_get_attrs ,
1010 is_abstract ,
1111 is_primitive ,
12- raise_ ,
1312 re_finditer ,
1413)
1514from . import Resolver , ValueNotFound , ValueResolutionPostponed
1615
1716_SERVICE_AUTOREGISTRATION_EXCLUDE_REGEX = r"^([.\w/]+)?({[\w/.*,]+})?$"
17+ _SVC_DEFAULTS = ...
1818
1919
2020class ServiceDefaults (NamedTuple ):
@@ -135,10 +135,10 @@ class ServiceMetadata(NamedTuple):
135135 type : Type [Any ]
136136 clazz : Type [Any ]
137137 arguments : Dict [str , Any ]
138- params : List ['ServiceMetadata.ParameterMetadata' ]
138+ params : List [Any ]
139139 defaults : ServiceDefaults
140140
141- class ParameterMetadata (NamedTuple ):
141+ class ParameterMetadata (NamedTuple ): # type: ignore
142142 name : str
143143 source_kind : str
144144 type : Type [Any ]
@@ -186,39 +186,39 @@ class ServiceResolutionPostponed(ValueResolutionPostponed[ServiceMetadata]):
186186class ServiceResolver (Resolver [ServiceMetadata , Any ]):
187187 @staticmethod
188188 def _define_service_type (name : str , typ : str , cls : str ) -> Tuple [Type [Any ], Type [Any ]]:
189- if typ is ... and cls is ...:
190- cls = typ = import_module_and_get_attr (name = name )
191- return typ , cls
189+ if typ is _SVC_DEFAULTS and cls is _SVC_DEFAULTS : # type: ignore
190+ cls = typ = import_module_and_get_attr (name = name ) # type: ignore
191+ return typ , cls # type: ignore
192192
193- if typ is not ...:
194- typ = import_module_and_get_attr (name = typ )
195- if cls is not ...:
196- cls = import_module_and_get_attr (name = cls )
193+ if typ is not _SVC_DEFAULTS : # type: ignore
194+ typ = import_module_and_get_attr (name = typ ) # type: ignore
195+ if cls is not _SVC_DEFAULTS : # type: ignore
196+ cls = import_module_and_get_attr (name = cls ) # type: ignore
197197
198- if typ is ...:
198+ if typ is _SVC_DEFAULTS : # type: ignore
199199 try :
200- typ = import_module_and_get_attr (name = name )
200+ typ = import_module_and_get_attr (name = name ) # type: ignore
201201 except Exception :
202202 typ = cls
203- if cls is ...:
203+ if cls is _SVC_DEFAULTS : # type: ignore
204204 cls = typ
205205
206- if cls is not typ and not issubclass (signature (cls ).return_annotation or cls , typ ):
206+ if cls is not typ and not issubclass (signature (cls ).return_annotation or cls , typ ): # type: ignore
207207 raise TypeError ('Class <{0}> return type must be <{1}>' .format (cls , typ ))
208208
209- return typ , cls
209+ return typ , cls # type: ignore
210210
211- def extract_metadata (self , data : Dict [ str , Any ], extra : Dict [ str , Any ] = {}) -> ServiceMetadata :
212- key : str = data . get ( 'key' ) or raise_ ( KeyError ( 'Missing key "key" to extract service metadata' ))
213- val : Any = data . get ( 'val' ) or raise_ ( KeyError ( 'Missing key "val" to extract service metadata' ))
214- defaults : ServiceDefaults = data .get ('defaults' ) or raise_ (
215- KeyError ( 'Missing key "defaults" to extract service metadata ' )
216- )
211+ def extract_metadata (
212+ self , data : Dict [ str , Any ], extra : Dict [ str , Any ] = {} # pylint: disable=W0613
213+ ) -> ServiceMetadata :
214+ key = cast ( str , data .get ('key' ))
215+ val = data . get ( 'val ' )
216+ defaults = cast ( ServiceDefaults , data . get ( 'defaults' ) )
217217
218218 typ , clazz = self ._define_service_type (
219219 name = key ,
220- typ = val ['type' ] if isinstance (val , dict ) and 'type' in val else ... ,
221- cls = val ['class' ] if isinstance (val , dict ) and 'class' in val else ... ,
220+ typ = val ['type' ] if isinstance (val , dict ) and 'type' in val else _SVC_DEFAULTS ,
221+ cls = val ['class' ] if isinstance (val , dict ) and 'class' in val else _SVC_DEFAULTS ,
222222 )
223223 kwargs = val ['arguments' ] if isinstance (val , dict ) and 'arguments' in val else {}
224224 return ServiceMetadata (
@@ -234,15 +234,9 @@ def extract_metadata(self, data: Dict[str, Any], extra: Dict[str, Any] = {}) ->
234234 )
235235
236236 def parse_value (self , metadata : ServiceMetadata , retries : int = - 1 , extra : Dict [str , Any ] = {}) -> Any :
237- _variables : Dict [str , Any ] = extra .get ('variables' )
238- if _variables is None :
239- raise KeyError ('Missing key "variables" to parse service value' )
240- _services : Dict [str , Any ] = extra .get ('services' )
241- if _services is None :
242- raise KeyError ('Missing key "services" to parse service value' )
243- variable_resolver : Resolver = extra .get ('resolvers' , {}).get ('variable' ) or raise_ (
244- KeyError ('Missing key "resolvers.variable"' )
245- )
237+ _variables = cast (Dict [str , Any ], extra .get ('variables' ))
238+ _services = cast (Dict [str , Any ], extra .get ('services' ))
239+ variable_resolver = cast (Resolver , extra .get ('resolvers' , {}).get ('variable' ))
246240
247241 parameters : Dict [str , Any ] = {}
248242 for param in metadata .params :
@@ -285,9 +279,7 @@ def parse_value(self, metadata: ServiceMetadata, retries: int = -1, extra: Dict[
285279def prepare_services_to_parse (
286280 resolver : Resolver [Any , Any ], items : Dict [str , Any ], extra : Dict [str , Any ]
287281) -> Dict [str , Tuple ['ServiceMetadata' , int ]]:
288- _service_defaults : ServiceDefaults = extra .get ('_service_defaults' ) or raise_ (
289- KeyError ('Missing key "_service_defaults"' )
290- )
282+ _service_defaults = cast (ServiceDefaults , extra .get ('_service_defaults' ))
291283
292284 services : Dict [str , Tuple ['ServiceMetadata' , int ]] = {}
293285 for key , val in items .items ():
0 commit comments