Skip to content

Commit

Permalink
Make AnnotatedParameter reader work with multiple annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Dec 2, 2023
1 parent eed94a9 commit 4f29cac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from typing import Dict, List, Tuple, Union

from typing_extensions import Annotated
from wireup.ioc.types import AnnotatedParameter, ContainerProxyQualifier
from wireup.ioc.types import AnnotatedParameter, ContainerProxyQualifier, InjectableType
from wireup.ioc.util import (
is_type_autowireable,
parameter_get_type_and_annotation,
)


class TestUtilityFunctions(unittest.TestCase):
def test_get_annotated_parameter(self):
class Dummy:
def test_get_annotated_parameter_retrieves_first_injectable_type(self):
class Dummy(InjectableType):
...

d1 = Dummy()
d2 = Dummy()

def inner(_a: Annotated[str, d1], _b, _c: str, _d=d2):
def inner(_a: Annotated[str, "ignored", unittest.TestCase, d1], _b, _c: str, _d=d2):
...

params = inspect.signature(inner)
Expand Down
7 changes: 5 additions & 2 deletions wireup/ioc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from inspect import Parameter
from typing import Any

from wireup.ioc.types import AnnotatedParameter
from wireup.ioc.types import AnnotatedParameter, InjectableType


def parameter_get_type_and_annotation(parameter: Parameter) -> AnnotatedParameter:
Expand All @@ -14,7 +14,10 @@ def parameter_get_type_and_annotation(parameter: Parameter) -> AnnotatedParamete
"""
if hasattr(parameter.annotation, "__metadata__") and hasattr(parameter.annotation, "__args__"):
klass = parameter.annotation.__args__[0]
annotation = parameter.annotation.__metadata__[0]
annotation = next(
(ann for ann in parameter.annotation.__metadata__ if isinstance(ann, InjectableType)),
None,
)
else:
klass = None if parameter.annotation is Parameter.empty else parameter.annotation
annotation = None if parameter.default is Parameter.empty else parameter.default
Expand Down

0 comments on commit 4f29cac

Please sign in to comment.