1+ from _typeshed import Incomplete
2+ from collections import defaultdict
13from collections .abc import Callable
2- from enum import Enum
4+ from enum import Enum , unique
35from typing import Any , Final , Generic , NamedTuple , NewType , TypeVar , overload
6+ from typing_extensions import Self , deprecated
47
5- __version__ : str
8+ _T = TypeVar ("_T" , default = Any )
9+
10+ __version__ : Final [str ]
611
12+ @deprecated ("Deprecated alias for `MissingDependencyError`." )
713class MissingDependencyException (Exception ): ...
14+
815class MissingDependencyError (MissingDependencyException ): ...
16+
17+ @deprecated ("Deprecated alias for `InvalidRegistrationError`." )
918class InvalidRegistrationException (Exception ): ...
19+
1020class InvalidRegistrationError (InvalidRegistrationException ): ...
21+
22+ class InvalidFactoryError (InvalidRegistrationError ):
23+ def __init__ (self , service , factory ) -> None : ...
24+
25+ class InvalidSelfRegistrationError (InvalidRegistrationError ):
26+ def __init__ (self , service ) -> None : ...
27+
28+ @deprecated ("Deprecated alias for `InvalidForwardReferenceError`." )
1129class InvalidForwardReferenceException (Exception ): ...
30+
1231class InvalidForwardReferenceError (InvalidForwardReferenceException ): ...
1332
33+ # TODO: Make this class Generic
34+ class RegistrationScope :
35+ parent : RegistrationScope | None
36+ entries : defaultdict [Incomplete , list [Incomplete ]]
37+ def __init__ (self , parent : RegistrationScope | None = None ) -> None : ...
38+ def child (self ) -> Self : ...
39+ def append (self , key , value ) -> None : ...
40+ def get (self , key ) -> list [Incomplete ]: ...
41+
42+ @unique
1443class Scope (Enum ):
1544 transient = 0
1645 singleton = 1
1746
18- _T = TypeVar ("_T" , default = Any )
19-
2047class _Registration (NamedTuple , Generic [_T ]):
2148 service : type [_T ] | str
2249 scope : Scope
2350 builder : Callable [..., _T ]
2451 needs : dict [str , Any ] # the type hints of the builder's parameters
2552 args : dict [str , Any ] # passed to builder at instantiation time
53+ cache : bool
2654
2755_Empty = NewType ("_Empty" , object ) # a class at runtime
2856empty : Final [_Empty ]
2957
3058class _Registry :
59+ def __init__ (self , parent : _Registry | None = None ) -> None : ...
3160 def register_service_and_impl (
3261 self ,
3362 service : type [_T ] | str ,
3463 scope : Scope ,
3564 impl : type [_T ],
3665 resolve_args : dict [str , Any ], # forwarded to _Registration.builder
66+ cache : bool = True ,
3767 ) -> None : ...
3868 def register_service_and_instance (self , service : type [_T ] | str , instance : _T ) -> None : ...
39- def register_concrete_service (self , service : type | str , scope : Scope ) -> None : ...
69+ def register_concrete_service (
70+ self ,
71+ service : type | str ,
72+ scope : Scope ,
73+ resolve_args : dict [str , Any ] | None = None , # forwarded to _Registration.builder
74+ cache : bool = True ,
75+ ) -> None : ...
4076 def build_context (self , key : type | str , existing : _ResolutionContext | None = None ) -> _ResolutionContext : ...
4177 def register (
4278 self ,
4379 service : type [_T ] | str ,
4480 factory : Callable [..., _T ] | _Empty = ...,
4581 instance : _T | _Empty = ...,
4682 scope : Scope = Scope .transient ,
83+ cache : bool = True ,
4784 ** kwargs : Any , # forwarded to _Registration.builder
4885 ) -> None : ...
49- def __getitem__ (self , service : type [_T ] | str ) -> list [_Registration [_T ]]: ...
5086
5187class _ResolutionTarget (Generic [_T ]):
5288 service : type [_T ] | str
@@ -55,7 +91,7 @@ class _ResolutionTarget(Generic[_T]):
5591 def is_generic_list (self ) -> bool : ...
5692 @property
5793 def generic_parameter (self ) -> Any : ... # returns the first annotated generic parameter of the service
58- def next_impl (self ) -> _Registration [_T ]: ...
94+ def next_impl (self ) -> _Registration [_T ] | None : ...
5995
6096class _ResolutionContext :
6197 targets : dict [type | str , _ResolutionTarget [Any ]]
@@ -70,25 +106,33 @@ class _ResolutionContext:
70106
71107class Container :
72108 registrations : _Registry
73- def __init__ (self ) -> None : ...
109+ def __init__ (self , registrations : _Registry | None = None , auto_register : bool = False ) -> None : ...
74110
75111 # all kwargs are forwarded to _Registration.builder
76112 @overload
77- def register (self , service : type [_T ] | str , * , instance : _T , ** kwargs : Any ) -> Container : ...
113+ def register (self , service : type [_T ] | str , * , instance : _T , cache : bool = True , ** kwargs : Any ) -> Self : ...
78114 @overload
79115 def register (
80- self , service : type [_T ] | str , factory : Callable [..., _T ] | _Empty = ..., * , scope : Scope = Scope .transient , ** kwargs : Any
81- ) -> Container : ...
116+ self ,
117+ service : type [_T ] | str ,
118+ factory : Callable [..., _T ] | _Empty = ...,
119+ * ,
120+ scope : Scope = Scope .transient ,
121+ cache : bool = True ,
122+ ** kwargs : Any ,
123+ ) -> Self : ...
82124 @overload
83125 def register (
84126 self ,
85127 service : type [_T ] | str ,
86128 factory : Callable [..., _T ] | _Empty = ...,
87129 instance : _T | _Empty = ...,
88130 scope : Scope = Scope .transient ,
131+ cache : bool = True ,
89132 ** kwargs : Any ,
90- ) -> Container : ...
133+ ) -> Self : ...
91134
92135 def resolve_all (self , service : type [_T ] | str , ** kwargs : Any ) -> list [_T ]: ...
93136 def resolve (self , service_key : type [_T ] | str , ** kwargs : Any ) -> _T : ...
94137 def instantiate (self , service_key : type [_T ] | str , ** kwargs : Any ) -> _T : ...
138+ def child (self ) -> Self : ...
0 commit comments