Skip to content

Commit 9dfea98

Browse files
authored
refactoring: ♻️ Rename constant to set_constant
1 parent 32f50b2 commit 9dfea98

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

documentation/basic-usage.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class Injectable:
2424
""" class implementation """
2525
```
2626

27-
If you have a constant (such as a global variable) and wish to register it as an injectable, use `constant` function.
27+
If you have a constant (such as a global variable) and wish to register it as an injectable, use `set_constant`
28+
function.
2829

2930
```python
30-
from injection import constant
31+
from injection import set_constant
3132

32-
app = constant(Application())
33+
app = set_constant(Application())
3334
```
3435

3536
## Inject an instance

injection/_pkg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
__all__ = (
44
"Module",
55
"ModulePriorities",
6-
"constant",
76
"default_module",
87
"get_instance",
98
"get_lazy_instance",
109
"inject",
1110
"injectable",
11+
"set_constant",
1212
"singleton",
1313
)
1414

1515
default_module = Module(f"{__name__}:default_module")
1616

17-
constant = default_module.constant
1817
get_instance = default_module.get_instance
1918
get_lazy_instance = default_module.get_lazy_instance
2019

2120
inject = default_module.inject
2221
injectable = default_module.injectable
2322
singleton = default_module.singleton
23+
24+
set_constant = default_module.set_constant

injection/_pkg.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ _T = TypeVar("_T")
1010

1111
default_module: Final[Module] = ...
1212

13-
constant = default_module.constant
1413
get_instance = default_module.get_instance
1514
get_lazy_instance = default_module.get_lazy_instance
1615

1716
inject = default_module.inject
1817
injectable = default_module.injectable
1918
singleton = default_module.singleton
2019

20+
set_constant = default_module.set_constant
21+
2122
@final
2223
class Module:
2324
"""
@@ -60,7 +61,7 @@ class Module:
6061
singleton will be constructed. At injection time, the injected instance will
6162
always be the same.
6263
"""
63-
def constant(
64+
def set_constant(
6465
self,
6566
instance: _T,
6667
on: type | Iterable[type] | UnionType = ...,

injection/core/module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def __brokers(self) -> Iterator[Container | Module]:
309309
yield from tuple(self.__modules)
310310
yield self.__container
311311

312-
def constant(self, instance: _T, on: type | Types = None) -> _T:
312+
def set_constant(self, instance: _T, on: type | Types = None) -> _T:
313313
cls = type(instance)
314314

315315
@self.injectable(on=(cls, on))

tests/core/test_module.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,32 @@ def test_get_lazy_instance_with_no_injectable_return_lazy_none(self, module):
114114
assert lazy_instance.is_set
115115

116116
"""
117-
constant
117+
set_constant
118118
"""
119119

120-
def test_constant_with_success_return_instance(self, module):
120+
def test_set_constant_with_success_return_instance(self, module):
121121
instance = SomeClass()
122-
return_value = module.constant(instance)
122+
return_value = module.set_constant(instance)
123123
assert instance is return_value is module.get_instance(SomeClass)
124124

125-
def test_constant_with_on_return_instance(self, module):
126-
class Class(SomeClass):
125+
def test_set_constant_with_on_return_instance(self, module):
126+
class A:
127+
...
128+
129+
class B(A):
130+
...
131+
132+
class C(B):
127133
...
128134

129-
instance = Class()
130-
module.constant(instance, on=SomeClass)
131-
assert instance is module.get_instance(Class) is module.get_instance(SomeClass)
135+
instance = C()
136+
module.set_constant(instance, on=(A, B))
137+
assert (
138+
instance
139+
is module.get_instance(A)
140+
is module.get_instance(B)
141+
is module.get_instance(C)
142+
)
132143

133144
"""
134145
use

0 commit comments

Comments
 (0)