Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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. 🚧
2 changes: 1 addition & 1 deletion rodi/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.7"
__version__ = "2.0.8"
18 changes: 0 additions & 18 deletions rodi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
Type,
Expand Down Expand Up @@ -38,7 +39,6 @@
ServiceLifeStyle,
Services,
TrackingActivationScope,
UnsupportedUnionTypeException,
_get_factory_annotations_or_throw,
inject,
to_standard_param_name,
Expand Down Expand Up @@ -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():
Expand Down