Skip to content

Commit

Permalink
🐛 fix: not resolving aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
StummeJ committed Oct 18, 2024
1 parent f6d6fbc commit b75bfc7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion rodi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def _get_resolvers_for_parameters(
# but at least Optional could be supported in the future
raise UnsupportedUnionTypeException(param_name, concrete_type)

if param_type is _empty:
if param_type is _empty or param_type not in services._map:
if services.strict:
raise CannotResolveParameterException(param_name, concrete_type)

Expand All @@ -521,6 +521,14 @@ def _get_resolvers_for_parameters(
else:
aliases = services._aliases[param_name]

if not aliases:
cls_name = class_name(param_type)
aliases = (
services._aliases[cls_name]
or services._aliases[cls_name.lower()]
or services._aliases[to_standard_param_name(cls_name)]
)

if aliases:
assert (
len(aliases) == 1
Expand Down
25 changes: 25 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,31 @@ def __init__(self, cats_controller, service_settings):
assert isinstance(u.cats_controller.cat_request_handler, GetCatRequestHandler)


def test_alias_dep_resolving():
container = arrange_cats_example()

class BaseClass: ...

class DerivedClass(BaseClass): ...

class UsingAliasByType:
def __init__(self, example: BaseClass):
self.example = example

def resolve_derived_class(_) -> DerivedClass:
return DerivedClass()

container.add_scoped_by_factory(resolve_derived_class, DerivedClass)
container.add_alias("BaseClass", DerivedClass)
container.add_scoped(UsingAliasByType)

provider = container.build_provider()
u = provider.get(UsingAliasByType)

assert isinstance(u, UsingAliasByType)
assert isinstance(u.example, DerivedClass)


def test_get_service_by_name_or_alias():
container = arrange_cats_example()
container.add_alias("k", CatsController)
Expand Down

0 comments on commit b75bfc7

Please sign in to comment.