From 02486bcb45ef80ee01ece8f60b2eb4a91099c5c7 Mon Sep 17 00:00:00 2001 From: Roberto Prevato Date: Sat, 12 Apr 2025 11:06:21 +0200 Subject: [PATCH] Prepare for 2.0.8 --- CHANGELOG.md | 6 ++++++ README.md | 9 +++++---- rodi/__about__.py | 2 +- rodi/__init__.py | 18 ------------------ tests/test_services.py | 14 ++++++-------- 5 files changed, 18 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32316e2..4f46b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.8] - 2025-04-12 + +- Add the link to the [documentation](https://www.neoteroi.dev/rodi/). +- Remove the `UnsupportedUnionTypeException` as `Rodi` supports union types, + they only require proper handling. + ## [2.0.7] - 2025-03-28 - Add the possibility to specify the `ActivationScope` class when instantiating diff --git a/README.md b/README.md index 119a4a9..c2006fd 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![versions](https://img.shields.io/pypi/pyversions/rodi.svg)](https://github.com/Neoteroi/rodi) [![codecov](https://codecov.io/gh/Neoteroi/rodi/branch/main/graph/badge.svg?token=VzAnusWIZt)](https://codecov.io/gh/Neoteroi/rodi) [![license](https://img.shields.io/github/license/Neoteroi/rodi.svg)](https://github.com/Neoteroi/rodi/blob/main/LICENSE) +[![documentation](https://img.shields.io/badge/📖-docs-purple)](https://www.neoteroi.dev/rodi/) # Implementation of dependency injection for Python 3 @@ -24,6 +25,10 @@ Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injec application](https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/)_). The `ContainerProtocol` for v2 is inspired by [punq](https://github.com/bobthemighty/punq). +## Documentation + +Rodi is documented here: [https://www.neoteroi.dev/rodi/](https://www.neoteroi.dev/rodi/). + ## Installation ```bash @@ -86,7 +91,3 @@ relying on the HTTP Request object being a service registered in your container. `rodi` is used in the [BlackSheep](https://www.neoteroi.dev/blacksheep/) web framework to implement [dependency injection](https://www.neoteroi.dev/blacksheep/dependency-injection/) for request handlers. - -# Documentation - -Under construction. 🚧 diff --git a/rodi/__about__.py b/rodi/__about__.py index 962c851..8cb37b5 100644 --- a/rodi/__about__.py +++ b/rodi/__about__.py @@ -1 +1 @@ -__version__ = "2.0.7" +__version__ = "2.0.8" diff --git a/rodi/__init__.py b/rodi/__init__.py index e1fa0a2..1262a00 100644 --- a/rodi/__init__.py +++ b/rodi/__init__.py @@ -131,18 +131,6 @@ def __init__(self, param_name, desired_type): ) -class UnsupportedUnionTypeException(DIException): - """Exception risen when a parameter type is defined - as Optional or Union of several types.""" - - def __init__(self, param_name, desired_type): - super().__init__( - f"Union or Optional type declaration is not supported. " - f"Cannot resolve parameter '{param_name}' " - f"when resolving '{class_name(desired_type)}'" - ) - - class OverridingServiceException(DIException): """ Exception risen when registering a service @@ -548,12 +536,6 @@ def _get_resolvers_for_parameters( param_type = param.annotation - if hasattr(param_type, "__origin__") and param_type.__origin__ is Union: - # NB: we could cycle through possible types using: param_type.__args__ - # Right now Union and Optional types resolution is not implemented, - # but at least Optional could be supported in the future - raise UnsupportedUnionTypeException(param_name, concrete_type) - if param_type is _empty: if services.strict: raise CannotResolveParameterException(param_name, concrete_type) diff --git a/tests/test_services.py b/tests/test_services.py index a7523fc..a55c529 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -10,6 +10,7 @@ Iterable, List, Mapping, + Optional, Sequence, Tuple, Type, @@ -38,7 +39,6 @@ ServiceLifeStyle, Services, TrackingActivationScope, - UnsupportedUnionTypeException, _get_factory_annotations_or_throw, inject, to_standard_param_name, @@ -321,15 +321,13 @@ def test_add_instance_with_declared_type(): assert isinstance(icircle, Circle) -def test_raises_for_optional_parameter(): +def test_optional_parameter(): container = Container() - container._add_exact_transient(Foo) - container._add_exact_transient(TypeWithOptional) - - with pytest.raises(UnsupportedUnionTypeException) as context: - container.build_provider() + container.add_transient(Optional[Foo], Foo) # type: ignore + container.add_transient(TypeWithOptional) - assert "foo" in str(context.value) + a = container.resolve(TypeWithOptional) + assert isinstance(a.foo, Foo) def test_raises_for_nested_circular_dependency():