@@ -595,6 +595,8 @@ def _create_components(self) -> None:
595595 # .. this hack is necessary for pybind11 based modules
596596 sys .modules ["pybind11_builtins" ] = types .SimpleNamespace () # type: ignore
597597
598+ injectables = self ._collect_injectables ()
599+
598600 for m , ctyp in typing .get_type_hints (cls ).items ():
599601 # Ignore private variables
600602 if m .startswith ("_" ):
@@ -611,24 +613,23 @@ def _create_components(self) -> None:
611613 % (cls .__name__ , m , ctyp )
612614 )
613615
614- component = self ._create_component (m , ctyp )
616+ component = self ._create_component (m , ctyp , injectables )
615617
616618 # Store for later
617619 components .append ((m , component ))
618-
619- self ._injectables = self ._collect_injectables ()
620+ injectables [m ] = component
620621
621622 # For each new component, perform magic injection
622623 for cname , component in components :
623624 setup_tunables (component , cname , "components" )
624- self ._setup_vars (cname , component )
625+ self ._setup_vars (cname , component , injectables )
625626 self ._setup_reset_vars (component )
626627
627628 # Do it for autonomous modes too
628629 for mode in self ._automodes .modes .values ():
629630 mode .logger = logging .getLogger (mode .MODE_NAME )
630631 setup_tunables (mode , mode .MODE_NAME , "autonomous" )
631- self ._setup_vars (mode .MODE_NAME , mode )
632+ self ._setup_vars (mode .MODE_NAME , mode , injectables )
632633
633634 # And for self too
634635 setup_tunables (self , "robot" , None )
@@ -672,9 +673,18 @@ def _collect_injectables(self) -> Dict[str, Any]:
672673
673674 return injectables
674675
675- def _create_component (self , name : str , ctyp : type ):
676+ def _create_component (self , name : str , ctyp : type , injectables : Dict [str , Any ]):
677+ type_hints = typing .get_type_hints (ctyp .__init__ )
678+ NoneType = type (None )
679+ init_return_type = type_hints .pop ("return" , NoneType )
680+ assert (
681+ init_return_type is NoneType
682+ ), f"{ ctyp !r} __init__ had an unexpected non-None return type hint"
683+ requests = get_injection_requests (type_hints , name )
684+ injections = find_injections (requests , injectables , name )
685+
676686 # Create instance, set it on self
677- component = ctyp ()
687+ component = ctyp (** injections )
678688 setattr (self , name , component )
679689
680690 # Ensure that mandatory methods are there
@@ -691,12 +701,12 @@ def _create_component(self, name: str, ctyp: type):
691701
692702 return component
693703
694- def _setup_vars (self , cname : str , component ) -> None :
704+ def _setup_vars (self , cname : str , component , injectables : Dict [ str , Any ] ) -> None :
695705 self .logger .debug ("Injecting magic variables into %s" , cname )
696706
697707 type_hints = typing .get_type_hints (type (component ))
698708 requests = get_injection_requests (type_hints , cname , component )
699- injections = find_injections (requests , self . _injectables , cname )
709+ injections = find_injections (requests , injectables , cname )
700710 component .__dict__ .update (injections )
701711
702712 def _setup_reset_vars (self , component ) -> None :
0 commit comments