25
25
wraps ,
26
26
)
27
27
from inspect import Signature , isclass
28
- from types import MappingProxyType , UnionType
28
+ from types import UnionType
29
29
from typing import (
30
30
Any ,
31
31
ClassVar ,
@@ -421,7 +421,7 @@ def decorator(wp):
421
421
wp .__init__ = self .inject (wp .__init__ , force = force )
422
422
return wp
423
423
424
- wrapper = Injected (wp , force = force ).update (self )
424
+ wrapper = InjectionFunction (wp , force = force ).update (self )
425
425
self .add_listener (wrapper )
426
426
return wrapper
427
427
@@ -540,45 +540,35 @@ def __move_module(self, module: Module, priority: ModulePriority):
540
540
541
541
542
542
"""
543
- Injected
543
+ InjectionFunction
544
544
"""
545
545
546
546
547
547
@dataclass (repr = False , frozen = True , slots = True )
548
548
class Dependencies :
549
- mapping : MappingProxyType [str , Injectable ]
550
-
551
- __SELF_KEY : ClassVar [str ] = "$self"
549
+ mapping : Mapping [str , Injectable ]
552
550
553
551
def __bool__ (self ) -> bool :
554
552
return bool (self .mapping )
555
553
556
- @property
557
- def arguments (self ) -> OrderedDict [str , Any ]:
558
- return OrderedDict (self .items (exclude = {self .__SELF_KEY }))
559
-
560
- @property
561
- def self (self ) -> Any | None :
562
- return self .get (self .__SELF_KEY )
563
-
564
- def items (self , / , * , exclude : Set [str ] = frozenset ()) -> Iterator [tuple [str , Any ]]:
554
+ def __iter__ (self ) -> Iterator [tuple [str , Any ]]:
565
555
for name , injectable in self .mapping .items ():
566
- if name in exclude :
567
- continue
568
-
569
556
yield name , injectable .get_instance ()
570
557
571
- def get (self , key : str ) -> Any | None :
572
- injectable = self .mapping .get (key )
558
+ @property
559
+ def are_resolved (self ) -> bool :
560
+ if isinstance (self .mapping , LazyMapping ) and not self .mapping .is_set :
561
+ return False
573
562
574
- if injectable is None :
575
- return None
563
+ return bool (self )
576
564
577
- return injectable .get_instance ()
565
+ @property
566
+ def arguments (self ) -> OrderedDict [str , Any ]:
567
+ return OrderedDict (self )
578
568
579
569
@classmethod
580
570
def from_mapping (cls , mapping : Mapping [str , Injectable ]):
581
- return cls (mapping = MappingProxyType ( mapping ) )
571
+ return cls (mapping = mapping )
582
572
583
573
@classmethod
584
574
def empty (cls ):
@@ -602,8 +592,9 @@ def __resolver(
602
592
)
603
593
604
594
if owner :
595
+ name , _ = next (hints )
605
596
hints = itertools .chain (
606
- ((cls . __SELF_KEY , owner ),),
597
+ ((name , owner ),),
607
598
hints ,
608
599
)
609
600
@@ -621,48 +612,47 @@ class Arguments(NamedTuple):
621
612
kwargs : Mapping [str , Any ]
622
613
623
614
624
- class Injected (EventListener ):
625
- __slots__ = ("__dict__" , "__function " , "__dependencies" , "__owner" )
615
+ class InjectionFunction (EventListener ):
616
+ __slots__ = ("__dict__" , "__wrapped__" , "__wrapper " , "__dependencies" , "__owner" )
626
617
627
618
def __init__ (self , wrapped : Callable [..., Any ], * , force : bool = False ):
628
- update_wrapper (self , wrapped )
619
+ update_wrapper (self , wrapped , updated = () )
629
620
630
621
@wraps (wrapped )
631
622
def wrapper (* args , ** kwargs ):
632
623
args , kwargs = self .bind (args , kwargs , force )
633
624
return wrapped (* args , ** kwargs )
634
625
635
- self .__function = wrapper
626
+ self .__wrapper = wrapper
636
627
self .__dependencies = Dependencies .empty ()
637
628
self .__owner = None
638
629
639
630
def __repr__ (self ) -> str :
640
- return repr (self .__function )
631
+ return repr (self .__wrapped__ )
641
632
642
633
def __str__ (self ) -> str :
643
- return str (self .__function )
634
+ return str (self .__wrapped__ )
644
635
645
636
def __call__ (self , / , * args , ** kwargs ) -> Any :
646
- return self .__function (* args , ** kwargs )
637
+ return self .__wrapper (* args , ** kwargs )
647
638
648
639
def __get__ (self , instance : object | None , owner : type ):
649
640
if instance is None :
650
- instance = self . __dependencies . self
641
+ return self
651
642
652
- return self .__function .__get__ (instance , owner )
643
+ return self .__wrapper .__get__ (instance , owner )
653
644
654
645
def __set_name__ (self , owner : type , name : str ):
655
- if self .__dependencies :
656
- raise TypeError
646
+ if self .__dependencies . are_resolved :
647
+ raise TypeError ( "TODO" )
657
648
658
649
if self .__owner :
659
- raise TypeError
650
+ raise TypeError ( "TODO" )
660
651
661
- with thread_lock :
662
- self .__owner = owner
652
+ self .__owner = owner
663
653
664
654
@cached_property
665
- def __signature (self ) -> Signature :
655
+ def __signature__ (self ) -> Signature :
666
656
return inspect .signature (self .__wrapped__ , eval_str = True )
667
657
668
658
def bind (
@@ -674,22 +664,23 @@ def bind(
674
664
if kwargs is None :
675
665
kwargs = {}
676
666
677
- if not ( arguments := self .__dependencies . arguments ) :
667
+ if not self .__dependencies :
678
668
return Arguments (args , kwargs )
679
669
680
- bound = self .__signature .bind_partial (* args , ** kwargs )
670
+ bound = self .__signature__ .bind_partial (* args , ** kwargs )
671
+ dependencies = self .__dependencies .arguments
681
672
682
673
if force :
683
- bound .arguments |= arguments
674
+ bound .arguments |= dependencies
684
675
else :
685
- bound .arguments = arguments | bound .arguments
676
+ bound .arguments = dependencies | bound .arguments
686
677
687
678
return Arguments (bound .args , bound .kwargs )
688
679
689
680
def update (self , module : Module ):
690
681
with thread_lock :
691
682
self .__dependencies = Dependencies .resolve (
692
- self .__signature ,
683
+ self .__signature__ ,
693
684
module ,
694
685
self .__owner ,
695
686
)
0 commit comments