Skip to content

Fix for annotated injection with attribute. Replacing with origins on… #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,8 +1239,11 @@ def _is_new_union_type(instance: Any) -> bool:

for k, v in list(bindings.items()):
if _is_specialization(v, Annotated):
v, metadata = v.__origin__, v.__metadata__
bindings[k] = v
origin, metadata = v.__origin__, v.__metadata__
if (
_inject_marker in metadata or _noinject_marker in metadata
): # replace original annotated type with its origin if annotation is injection marker
bindings[k] = origin
else:
metadata = tuple()

Expand Down Expand Up @@ -1324,7 +1327,7 @@ def provide_strs_also(self) -> List[str]:
def _mark_provider_function(function: Callable, *, allow_multi: bool) -> None:
scope_ = getattr(function, '__scope__', None)
try:
annotations = get_type_hints(function)
annotations = get_type_hints(function, include_extras=True)
except NameError:
return_type = '__deferred__'
else:
Expand Down
37 changes: 33 additions & 4 deletions injector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

"""Functional tests for the "Injector" dependency injection framework."""

from contextlib import contextmanager
from typing import Any, NewType, Optional, Union
import abc
import sys
import threading
import traceback
import warnings
from contextlib import contextmanager
from typing import Any, Optional, Union

if sys.version_info >= (3, 9):
from typing import Annotated
Expand Down Expand Up @@ -1754,3 +1752,34 @@ def configure(binder):
injector = Injector([configure])
assert injector.get(foo) == 123
assert injector.get(bar) == 456


def test_annotated_injection_with_attribute():

foo = Annotated[str, "foo"]
bar = Annotated[str, "bar"]

# noinspection PyUnusedLocal
@inject
def target(val_foo: foo, val_bar: bar):
pass

assert get_bindings(target) == {'val_foo': foo, 'val_bar': bar}


def test_annotated_injection_from_provider_to_attribute():
foo = Annotated[str, "foo"]
bar = Annotated[str, "bar"]

class TestModule(Module):
@provider
def provide_foo(self) -> foo:
return "foo"

@multiprovider
def provide_bars(self) -> List[bar]:
return ["bar"]

injector = Injector([TestModule])
assert injector.binder.has_explicit_binding_for(foo)
assert injector.binder.has_explicit_binding_for(List[bar])