diff --git a/CHANGELOG.md b/CHANGELOG.md index ec77ab3..7ee4b77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.1.0] - 2025-11-24 :notes: +- Improve `resolve()` typing, by @sobolevn. +- Use `Self` type for Container, by @sobolevn. +- Improve typing of `inject`, by @sobolevn. - Drop support for Python <= 3.10. - Add Python 3.14 to the build matrix and to classifiers. - Remove Codecov from GitHub Workflow and from README. diff --git a/rodi/__about__.py b/rodi/__about__.py index 8cb37b5..9aa3f90 100644 --- a/rodi/__about__.py +++ b/rodi/__about__.py @@ -1 +1 @@ -__version__ = "2.0.8" +__version__ = "2.1.0" diff --git a/rodi/__init__.py b/rodi/__init__.py index 622b48e..69ad324 100644 --- a/rodi/__init__.py +++ b/rodi/__init__.py @@ -20,6 +20,7 @@ from typing import ( cast, get_type_hints, + overload, ) T = TypeVar("T") @@ -31,10 +32,16 @@ class ContainerProtocol(Protocol): and tell if a type is configured. """ - def register(self, obj_type: Type | str, *args, **kwargs): + def register(self, obj_type: Type | str, *args, **kwargs) -> None: """Registers a type in the container, with optional arguments.""" - def resolve(self, obj_type: Type[T] | str, *args, **kwargs) -> T: + @overload + def resolve(self, obj_type: Type[T], *args, **kwargs) -> T: ... + + @overload + def resolve(self, obj_type: str, *args, **kwargs) -> Any: ... + + def resolve(self, obj_type: Type[T] | str, *args, **kwargs) -> Any: """Activates an instance of the given type, with optional arguments.""" def __contains__(self, item) -> bool: @@ -934,13 +941,31 @@ def register( self.add_transient(obj_type, sub_type) return self + @overload + def resolve( + self, + obj_type: Type[T], + scope: Any = None, + *args, + **kwargs, + ) -> T: ... + + @overload + def resolve( + self, + obj_type: str, + scope: Any = None, + *args, + **kwargs, + ) -> Any: ... + def resolve( self, obj_type: Type[T] | str, scope: Any = None, *args, **kwargs, - ) -> T: + ) -> Any: """ Resolves a service by type, obtaining an instance of that type. """