Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitccf7e85

Browse files
authored
refactor: Now analyze the types at the module level
1 parent3218cc5 commitccf7e85

File tree

2 files changed

+52
-55
lines changed

2 files changed

+52
-55
lines changed

‎injection/_core/common/type.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
get_type_hints,
2020
)
2121

22-
typeTypeDef[T]=type[T]|TypeAliasType|GenericAlias
23-
typeInputType[T]=TypeDef[T]|UnionType
22+
typeInputType[T]=type[T]|TypeAliasType|GenericAlias|UnionType
2423
typeTypeInfo[T]= (
2524
InputType[T]
2625
|Callable[...,T]

‎injection/_core/module.py‎

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
runtime_checkable,
4444
)
4545

46-
fromtype_analyzerimportMatchingTypesConfig,iter_matching_types
46+
fromtype_analyzerimportMatchingTypesConfig,iter_matching_types,matching_types
4747

4848
frominjection._core.common.asynchronousimport (
4949
AsyncCaller,
@@ -58,7 +58,6 @@
5858
frominjection._core.common.threadingimportget_lock
5959
frominjection._core.common.typeimport (
6060
InputType,
61-
TypeDef,
6261
TypeInfo,
6362
get_return_types,
6463
get_yield_hint,
@@ -247,20 +246,6 @@ class Updater[T]:
247246
defmake_record(self)->Record[T]:
248247
returnRecord(self.injectable,self.mode)
249248

250-
@classmethod
251-
defwith_basics(
252-
cls,
253-
on:TypeInfo[T],
254-
/,
255-
injectable:Injectable[T],
256-
mode:Mode|ModeStr,
257-
)->Self:
258-
returncls(
259-
classes=get_return_types(on),
260-
injectable=injectable,
261-
mode=Mode(mode),
262-
)
263-
264249

265250
@dataclass(repr=False,frozen=True,slots=True)
266251
classLocator(Broker):
@@ -274,20 +259,15 @@ class Locator(Broker):
274259
)
275260

276261
def__getitem__[T](self,cls:InputType[T],/)->Injectable[T]:
277-
forkey_typeinself.__iter_key_types((cls,)):
278-
try:
279-
record=self.__records[key_type]
280-
exceptKeyError:
281-
continue
282-
262+
try:
263+
record=self.__records[cls]
264+
exceptKeyErrorasexc:
265+
raiseNoInjectable(cls)fromexc
266+
else:
283267
returnrecord.injectable
284268

285-
raiseNoInjectable(cls)
286-
287269
def__contains__(self,cls:InputType[Any],/)->bool:
288-
returnany(
289-
key_typeinself.__recordsforkey_typeinself.__iter_key_types((cls,))
290-
)
270+
returnclsinself.__records
291271

292272
@property
293273
defis_locked(self)->bool:
@@ -299,8 +279,7 @@ def __injectables(self) -> frozenset[Injectable[Any]]:
299279

300280
defupdate[T](self,updater:Updater[T])->Self:
301281
record=updater.make_record()
302-
key_types=self.__build_key_types(updater.classes)
303-
records=dict(self.__prepare_for_updating(key_types,record))
282+
records=dict(self.__prepare_for_updating(updater.classes,record))
304283

305284
ifrecords:
306285
event=LocatorDependenciesUpdated(self,records.keys(),record.mode)
@@ -345,21 +324,6 @@ def __prepare_for_updating[T](
345324

346325
yieldcls,record
347326

348-
@staticmethod
349-
def__build_key_types[T](classes:Iterable[InputType[T]])->frozenset[TypeDef[T]]:
350-
config=MatchingTypesConfig(ignore_none=True)
351-
returnfrozenset(
352-
itertools.chain.from_iterable(
353-
iter_matching_types(cls,config)forclsinclasses
354-
)
355-
)
356-
357-
@staticmethod
358-
def__iter_key_types[T](classes:Iterable[InputType[T]])->Iterator[InputType[T]]:
359-
config=MatchingTypesConfig(with_origin=True,with_type_alias_value=True)
360-
forclsinclasses:
361-
yieldfromiter_matching_types(cls,config)
362-
363327
@staticmethod
364328
def__keep_new_record[T](
365329
new:Record[T],
@@ -432,14 +396,22 @@ def __post_init__(self) -> None:
432396
self.__locator.add_listener(self)
433397

434398
def__getitem__[T](self,cls:InputType[T],/)->Injectable[T]:
399+
key_types=self.__matching_key_types(cls)
400+
435401
forbrokerinself._iter_brokers():
436-
withsuppress(KeyError):
437-
returnbroker[cls]
402+
forkey_typeinkey_types:
403+
withsuppress(KeyError):
404+
returnbroker[key_type]
438405

439406
raiseNoInjectable(cls)
440407

441408
def__contains__(self,cls:InputType[Any],/)->bool:
442-
returnany(clsinbrokerforbrokerinself._iter_brokers())
409+
key_types=self.__matching_key_types(cls)
410+
returnany(
411+
key_typeinbroker
412+
forbrokerinself._iter_brokers()
413+
forkey_typeinkey_types
414+
)
443415

444416
@property
445417
defis_locked(self)->bool:
@@ -460,8 +432,7 @@ def decorator(wp: Recipe[P, T]) -> Recipe[P, T]:
460432
factory=extract_caller(self.make_injected_function(wp)ifinjectelsewp)
461433
injectable=cls(factory)# type: ignore[arg-type]
462434
hints=onifignore_type_hintelse (wp,on)
463-
updater=Updater.with_basics(hints,injectable,mode)
464-
self.update(updater)
435+
self.update_from(hints,injectable,mode)
465436
returnwp
466437

467438
returndecorator(wrapped)ifwrappedelsedecorator
@@ -512,8 +483,7 @@ def decorator(
512483
defshould_be_injectable[T](self,wrapped:type[T]|None=None,/)->Any:
513484
defdecorator(wp:type[T])->type[T]:
514485
injectable=ShouldBeInjectable(wp)
515-
updater=Updater.with_basics(wp,injectable,Mode.FALLBACK)
516-
self.update(updater)
486+
self.update_from(wp,injectable,Mode.FALLBACK)
517487
returnwp
518488

519489
returndecorator(wrapped)ifwrappedelsedecorator
@@ -571,8 +541,7 @@ def reserve_scoped_slot[T](
571541
mode:Mode|ModeStr=Mode.get_default(),
572542
)->SlotKey[T]:
573543
injectable=ScopedSlotInjectable(cls,scope_name)
574-
updater=Updater.with_basics(cls,injectable,mode)
575-
self.update(updater)
544+
self.update_from(cls,injectable,mode)
576545
returninjectable.key
577546

578547
definject[**P,T](
@@ -795,6 +764,21 @@ def update[T](self, updater: Updater[T]) -> Self:
795764
self.__locator.update(updater)
796765
returnself
797766

767+
defupdate_from[T](
768+
self,
769+
on:TypeInfo[T],
770+
/,
771+
injectable:Injectable[T],
772+
mode:Mode|ModeStr,
773+
)->Self:
774+
updater=Updater(
775+
classes=self.__build_key_types(on),
776+
injectable=injectable,
777+
mode=Mode(mode),
778+
)
779+
self.update(updater)
780+
returnself
781+
798782
definit_modules(self,*modules:Module)->Self:
799783
formoduleintuple(self.__modules):
800784
self.stop_using(module)
@@ -955,6 +939,20 @@ def from_name(cls, name: str) -> Module:
955939
defdefault(cls)->Module:
956940
returncls.from_name("__default__")
957941

942+
@staticmethod
943+
def__build_key_types(input_cls:Any)->frozenset[Any]:
944+
config=MatchingTypesConfig(ignore_none=True)
945+
returnfrozenset(
946+
itertools.chain.from_iterable(
947+
iter_matching_types(cls,config)forclsinget_return_types(input_cls)
948+
)
949+
)
950+
951+
@staticmethod
952+
def__matching_key_types(input_cls:Any)->tuple[Any, ...]:
953+
config=MatchingTypesConfig(with_origin=True,with_type_alias_value=True)
954+
returnmatching_types(input_cls,config)
955+
958956

959957
defmod(name:str|None=None,/)->Module:
960958
ifnameisNone:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp