Skip to content

Commit 56affd9

Browse files
authored
feat: ✨ Add threadsafe parameter for LazyInstance
1 parent 6e66b75 commit 56affd9

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

documentation/basic-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class SomeDataClass:
8383

8484
### Threadsafe injection
8585

86-
With `threadsafe=True`, the injection logic is wrapped in a `threading.Lock`.
86+
With `threadsafe=True`, the injection logic is wrapped in a `threading.RLock`.
8787

8888
```python
8989
@inject(threadsafe=True)

injection/__init__.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ class LazyInstance[T]:
7878
cls: _InputType[T],
7979
/,
8080
default: T = ...,
81+
*,
8182
module: Module = ...,
83+
threadsafe: bool = ...,
8284
) -> None: ...
8385
@overload
8486
def __get__(self, instance: object, owner: type | None = ...) -> T: ...
@@ -292,8 +294,8 @@ class Module:
292294
) -> _Invertible[T | Default]:
293295
"""
294296
Function used to retrieve an instance associated with the type passed in
295-
parameter or `NotImplemented`. Return a `Invertible` object. To access the instance
296-
contained in an invertible object, simply use a wavy line (~).
297+
parameter or `NotImplemented`. Return an `Invertible` object. To access the
298+
instance contained in an invertible object, simply use a wavy line (~).
297299
298300
Example: instance = ~lazy_instance
299301
"""

injection/_core/descriptors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ def __init__(
1515
cls: InputType[T],
1616
/,
1717
default: T = NotImplemented,
18+
*,
1819
module: Module | None = None,
20+
threadsafe: bool = False,
1921
) -> None:
20-
self.__value = (module or mod()).get_lazy_instance(cls, default)
22+
module = module or mod()
23+
self.__value = module.get_lazy_instance(cls, default, threadsafe=threadsafe)
2124

2225
def __get__(
2326
self,

0 commit comments

Comments
 (0)