Skip to content

Commit 53f7e47

Browse files
authored
feat: ✨ set_constant can raise ValueError (#269)
1 parent 32729ab commit 53f7e47

File tree

6 files changed

+216
-199
lines changed

6 files changed

+216
-199
lines changed

documentation/basic-usage.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ from injection import set_constant
3434
class ServiceC:
3535
""" class implementation """
3636

37-
service_c = ServiceC()
38-
set_constant(service_c)
37+
service_c = set_constant(ServiceC())
3938
```
4039

4140
Or here is the decorator `constant` which is equivalent:

documentation/entrypoint.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def entrypoint[**P, T](self: AsyncEntrypoint[P, T], conf: Conf) -> Entrypoint[P,
5959
f"{keyword}: {name}"
6060
for name in self.profile_loader.required_module_names(profile)
6161
}
62-
module_loader = PythonModuleLoader.from_keywords(keyword, *keywords)
62+
module_loader = PythonModuleLoader.from_keywords(*keywords)
6363
return (
6464
self.inject()
6565
.decorate(adefine_scope(Scope.LIFESPAN, kind="shared"))

injection/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Module:
374374
*,
375375
alias: bool = ...,
376376
mode: Mode | ModeStr = ...,
377-
) -> Self:
377+
) -> T:
378378
"""
379379
Function for registering a specific instance to be injected. This is useful for
380380
registering global variables. The difference with the singleton decorator is

injection/_core/module.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,21 @@ def set_constant[T](
532532
*,
533533
alias: bool = False,
534534
mode: Mode | ModeStr = Mode.get_default(),
535-
) -> Self:
536-
hints = on if alias else (type(instance), on)
535+
) -> T:
536+
if not alias:
537+
on = (type(instance), on)
538+
539+
elif not on:
540+
raise ValueError("`on` must be provided when `alias` is `True`.")
541+
537542
self.injectable(
538543
lambda: instance,
539544
ignore_type_hint=True,
540545
inject=False,
541-
on=hints,
546+
on=on,
542547
mode=mode,
543548
)
544-
return self
549+
return instance
545550

546551
def reserve_scoped_slot[T](
547552
self,

tests/core/test_module.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class C(B): ...
188188
is module.get_instance(C)
189189
)
190190

191-
def test_set_constant_with_success_with_type_alias(self, module):
191+
def test_set_constant_with_type_alias(self, module):
192192
type HelloWorld = str
193193
value = "Hello world!"
194194

@@ -197,6 +197,12 @@ def test_set_constant_with_success_with_type_alias(self, module):
197197
assert module.get_instance(str) is NotImplemented
198198
assert module.get_instance(HelloWorld) is value
199199

200+
def test_set_constant_with_alias_and_no_on_raise_value_error(self, module):
201+
value = "Hello world!"
202+
203+
with pytest.raises(ValueError):
204+
module.set_constant(value, alias=True)
205+
200206
"""
201207
reserve_scoped_slot
202208
"""

0 commit comments

Comments
 (0)