Skip to content
Merged
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
3 changes: 1 addition & 2 deletions documentation/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ from injection import set_constant
class ServiceC:
""" class implementation """

service_c = ServiceC()
set_constant(service_c)
service_c = set_constant(ServiceC())
```

Or here is the decorator `constant` which is equivalent:
Expand Down
2 changes: 1 addition & 1 deletion documentation/entrypoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def entrypoint[**P, T](self: AsyncEntrypoint[P, T], conf: Conf) -> Entrypoint[P,
f"{keyword}: {name}"
for name in self.profile_loader.required_module_names(profile)
}
module_loader = PythonModuleLoader.from_keywords(keyword, *keywords)
module_loader = PythonModuleLoader.from_keywords(*keywords)
return (
self.inject()
.decorate(adefine_scope(Scope.LIFESPAN, kind="shared"))
Expand Down
2 changes: 1 addition & 1 deletion injection/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class Module:
*,
alias: bool = ...,
mode: Mode | ModeStr = ...,
) -> Self:
) -> T:
"""
Function for registering a specific instance to be injected. This is useful for
registering global variables. The difference with the singleton decorator is
Expand Down
13 changes: 9 additions & 4 deletions injection/_core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,16 +532,21 @@ def set_constant[T](
*,
alias: bool = False,
mode: Mode | ModeStr = Mode.get_default(),
) -> Self:
hints = on if alias else (type(instance), on)
) -> T:
if not alias:
on = (type(instance), on)

elif not on:
raise ValueError("`on` must be provided when `alias` is `True`.")

self.injectable(
lambda: instance,
ignore_type_hint=True,
inject=False,
on=hints,
on=on,
mode=mode,
)
return self
return instance

def reserve_scoped_slot[T](
self,
Expand Down
8 changes: 7 additions & 1 deletion tests/core/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class C(B): ...
is module.get_instance(C)
)

def test_set_constant_with_success_with_type_alias(self, module):
def test_set_constant_with_type_alias(self, module):
type HelloWorld = str
value = "Hello world!"

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

def test_set_constant_with_alias_and_no_on_raise_value_error(self, module):
value = "Hello world!"

with pytest.raises(ValueError):
module.set_constant(value, alias=True)

"""
reserve_scoped_slot
"""
Expand Down
Loading