Skip to content

Commit 4f197ca

Browse files
authored
Improve resolve() typing
1 parent f6d6fbc commit 4f197ca

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

rodi/__init__.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Union,
1919
cast,
2020
get_type_hints,
21+
overload,
2122
)
2223

2324
if sys.version_info >= (3, 8): # pragma: no cover
@@ -41,10 +42,17 @@ class ContainerProtocol(Protocol):
4142
and tell if a type is configured.
4243
"""
4344

44-
def register(self, obj_type: Union[Type, str], *args, **kwargs):
45+
def register(self, obj_type: Union[Type, str], *args, **kwargs) -> None:
4546
"""Registers a type in the container, with optional arguments."""
4647

47-
def resolve(self, obj_type: Union[Type[T], str], *args, **kwargs) -> T:
48+
@overload
49+
def resolve(self, obj_type: Type[T], *args, **kwargs) -> T: ...
50+
51+
@overload
52+
def resolve(self, obj_type: str, *args, **kwargs) -> Any: ...
53+
54+
@overload
55+
def resolve(self, obj_type: Union[Type[T], str], *args, **kwargs) -> Any:
4856
"""Activates an instance of the given type, with optional arguments."""
4957

5058
def __contains__(self, item) -> bool:
@@ -901,13 +909,31 @@ def register(
901909
self.add_transient(obj_type, sub_type)
902910
return self
903911

912+
@overload
913+
def resolve(
914+
self,
915+
obj_type: Type[T],
916+
scope: Any = None,
917+
*args,
918+
**kwargs,
919+
) -> T: ...
920+
921+
@overload
922+
def resolve(
923+
self,
924+
obj_type: str,
925+
scope: Any = None,
926+
*args,
927+
**kwargs,
928+
) -> Any: ...
929+
904930
def resolve(
905931
self,
906932
obj_type: Union[Type[T], str],
907933
scope: Any = None,
908934
*args,
909935
**kwargs,
910-
) -> T:
936+
) -> Any:
911937
"""
912938
Resolves a service by type, obtaining an instance of that type.
913939
"""

0 commit comments

Comments
 (0)