Skip to content

Commit a6a5fde

Browse files
changed behaviour of provider decorator to enable annotated types providing
1 parent b436c63 commit a6a5fde

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ def provide_strs_also(self) -> List[str]:
13251325
def _mark_provider_function(function: Callable, *, allow_multi: bool) -> None:
13261326
scope_ = getattr(function, '__scope__', None)
13271327
try:
1328-
annotations = get_type_hints(function)
1328+
annotations = get_type_hints(function, include_extras=True)
13291329
except NameError:
13301330
return_type = '__deferred__'
13311331
else:

injector_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,3 +1767,21 @@ def target(val_foo: foo, val_bar: bar):
17671767
pass
17681768

17691769
assert get_bindings(target) == {'val_foo': foo, 'val_bar': bar}
1770+
1771+
1772+
def test_annotated_injection_from_provider_to_attribute():
1773+
foo = Annotated[str, "foo"]
1774+
bar = Annotated[str, "bar"]
1775+
1776+
class TestModule(Module):
1777+
@provider
1778+
def provide_foo(self) -> foo:
1779+
return "foo"
1780+
1781+
@multiprovider
1782+
def provide_bars(self) -> list[bar]:
1783+
return ["bar"]
1784+
1785+
injector = Injector([TestModule])
1786+
assert injector.binder.has_explicit_binding_for(foo)
1787+
assert injector.binder.has_explicit_binding_for(list[bar])

0 commit comments

Comments
 (0)