Skip to content

Commit d9d33dc

Browse files
Make providers work with type annotations (#286)
1 parent 5a30e9a commit d9d33dc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def provide_strs_also(self) -> List[str]:
13901390
def _mark_provider_function(function: Callable, *, allow_multi: bool) -> None:
13911391
scope_ = getattr(function, '__scope__', None)
13921392
try:
1393-
annotations = get_type_hints(function)
1393+
annotations = get_type_hints(function, include_extras=True)
13941394
except NameError:
13951395
return_type = '__deferred__'
13961396
else:

injector_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,3 +2032,19 @@ class MyClass:
20322032
injector = Injector([configure])
20332033
instance = injector.get(MyClass)
20342034
assert instance.foo == 123
2035+
2036+
2037+
def test_module_provider_with_annotated():
2038+
class MyModule(Module):
2039+
@provider
2040+
def provide_first(self) -> Annotated[str, 'first']:
2041+
return 'Bob'
2042+
2043+
@provider
2044+
def provide_second(self) -> Annotated[str, 'second']:
2045+
return 'Iger'
2046+
2047+
module = MyModule()
2048+
injector = Injector(module)
2049+
assert injector.get(Annotated[str, 'first']) == 'Bob'
2050+
assert injector.get(Annotated[str, 'second']) == 'Iger'

0 commit comments

Comments
 (0)