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 1 commit
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
7 changes: 4 additions & 3 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,10 @@ def _is_new_union_type(instance: Any) -> bool:
bindings.pop(spec.varkw, None)

for k, v in list(bindings.items()):
if _is_specialization(v, Annotated):
v, metadata = v.__origin__, v.__metadata__
bindings[k] = v
if _is_specialization(v, Annotated) :
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
13 changes: 13 additions & 0 deletions injector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,3 +1754,16 @@ 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}
Loading