Skip to content

Commit 03da590

Browse files
authored
threadsafe is now None by default
1 parent aef964d commit 03da590

File tree

7 files changed

+60
-45
lines changed

7 files changed

+60
-45
lines changed

injection/__init__.pyi

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ def asfunction[**P, T](
3636
/,
3737
*,
3838
module: Module = ...,
39-
threadsafe: bool = ...,
39+
threadsafe: bool | None = ...,
4040
) -> Callable[P, T]: ...
4141
@overload
4242
def asfunction[**P, T](
4343
wrapped: None = ...,
4444
/,
4545
*,
4646
module: Module = ...,
47-
threadsafe: bool = ...,
47+
threadsafe: bool | None = ...,
4848
) -> Callable[[type[Callable[P, T]]], Callable[P, T]]: ...
4949
@asynccontextmanager
5050
def adefine_scope(
5151
name: str,
5252
/,
5353
kind: ScopeKind | ScopeKindStr = ...,
54-
threadsafe: bool = ...,
54+
threadsafe: bool | None = ...,
5555
) -> AsyncIterator[Scope]: ...
5656
@contextmanager
5757
def define_scope(
5858
name: str,
5959
/,
6060
kind: ScopeKind | ScopeKindStr = ...,
61-
threadsafe: bool = ...,
61+
threadsafe: bool | None = ...,
6262
) -> Iterator[Scope]: ...
6363
def mod(name: str = ..., /) -> Module:
6464
"""
@@ -96,7 +96,7 @@ class LazyInstance[T]:
9696
default: T = ...,
9797
*,
9898
module: Module = ...,
99-
threadsafe: bool = ...,
99+
threadsafe: bool | None = ...,
100100
) -> None: ...
101101
@overload
102102
def __get__(self, instance: object, owner: type | None = ...) -> T: ...
@@ -124,7 +124,7 @@ class Module:
124124
wrapped: Callable[P, T] = ...,
125125
/,
126126
*,
127-
threadsafe: bool = ...,
127+
threadsafe: bool | None = ...,
128128
) -> Any:
129129
"""
130130
Decorator applicable to a class or function. Inject function dependencies using
@@ -227,21 +227,26 @@ class Module:
227227
self,
228228
wrapped: Callable[P, T],
229229
/,
230-
threadsafe: bool = ...,
230+
threadsafe: bool | None = ...,
231231
) -> Callable[P, T]: ...
232232
def make_async_factory[T](
233233
self,
234234
wrapped: type[T],
235235
/,
236-
threadsafe: bool = ...,
236+
threadsafe: bool | None = ...,
237237
) -> Callable[..., Awaitable[T]]: ...
238238
async def afind_instance[T](
239239
self,
240240
cls: _InputType[T],
241241
*,
242-
threadsafe: bool = ...,
242+
threadsafe: bool | None = ...,
243243
) -> T: ...
244-
def find_instance[T](self, cls: _InputType[T], *, threadsafe: bool = ...) -> T:
244+
def find_instance[T](
245+
self,
246+
cls: _InputType[T],
247+
*,
248+
threadsafe: bool | None = ...,
249+
) -> T:
245250
"""
246251
Function used to retrieve an instance associated with the type passed in
247252
parameter or an exception will be raised.
@@ -253,23 +258,23 @@ class Module:
253258
cls: _InputType[T],
254259
default: Default,
255260
*,
256-
threadsafe: bool = ...,
261+
threadsafe: bool | None = ...,
257262
) -> T | Default: ...
258263
@overload
259264
async def aget_instance[T](
260265
self,
261266
cls: _InputType[T],
262267
default: T = ...,
263268
*,
264-
threadsafe: bool = ...,
269+
threadsafe: bool | None = ...,
265270
) -> T: ...
266271
@overload
267272
def get_instance[T, Default](
268273
self,
269274
cls: _InputType[T],
270275
default: Default,
271276
*,
272-
threadsafe: bool = ...,
277+
threadsafe: bool | None = ...,
273278
) -> T | Default:
274279
"""
275280
Function used to retrieve an instance associated with the type passed in
@@ -282,31 +287,31 @@ class Module:
282287
cls: _InputType[T],
283288
default: T = ...,
284289
*,
285-
threadsafe: bool = ...,
290+
threadsafe: bool | None = ...,
286291
) -> T: ...
287292
@overload
288293
def aget_lazy_instance[T, Default](
289294
self,
290295
cls: _InputType[T],
291296
default: Default,
292297
*,
293-
threadsafe: bool = ...,
298+
threadsafe: bool | None = ...,
294299
) -> Awaitable[T | Default]: ...
295300
@overload
296301
def aget_lazy_instance[T](
297302
self,
298303
cls: _InputType[T],
299304
default: T = ...,
300305
*,
301-
threadsafe: bool = ...,
306+
threadsafe: bool | None = ...,
302307
) -> Awaitable[T]: ...
303308
@overload
304309
def get_lazy_instance[T, Default](
305310
self,
306311
cls: _InputType[T],
307312
default: Default,
308313
*,
309-
threadsafe: bool = ...,
314+
threadsafe: bool | None = ...,
310315
) -> _Invertible[T | Default]:
311316
"""
312317
Function used to retrieve an instance associated with the type passed in
@@ -322,7 +327,7 @@ class Module:
322327
cls: _InputType[T],
323328
default: T = ...,
324329
*,
325-
threadsafe: bool = ...,
330+
threadsafe: bool | None = ...,
326331
) -> _Invertible[T]: ...
327332
def init_modules(self, *modules: Module) -> Self:
328333
"""

injection/_core/asfunction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def asfunction[**P, T](
1212
/,
1313
*,
1414
module: Module | None = None,
15-
threadsafe: bool = False,
15+
threadsafe: bool | None = None,
1616
) -> Any:
1717
module = module or mod()
1818

injection/_core/common/threading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
from typing import Any, ContextManager
44

55

6-
def get_lock(threadsafe: bool) -> ContextManager[Any]:
6+
def get_lock(threadsafe: bool | None = None) -> ContextManager[Any]:
77
return RLock() if threadsafe else nullcontext()

injection/_core/descriptors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(
1717
default: T = NotImplemented,
1818
*,
1919
module: Module | None = None,
20-
threadsafe: bool = False,
20+
threadsafe: bool | None = None,
2121
) -> None:
2222
module = module or mod()
2323
self.__value = module.get_lazy_instance(cls, default, threadsafe=threadsafe)

injection/_core/module.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def inject[**P, T](
560560
wrapped: Callable[P, T] | None = None,
561561
/,
562562
*,
563-
threadsafe: bool = False,
563+
threadsafe: bool | None = None,
564564
) -> Any:
565565
def decorator(wp: Callable[P, T]) -> Callable[P, T]:
566566
if isclass(wp):
@@ -576,22 +576,22 @@ def make_injected_function[**P, T](
576576
self,
577577
wrapped: Callable[P, T],
578578
/,
579-
threadsafe: bool = ...,
579+
threadsafe: bool | None = ...,
580580
) -> SyncInjectedFunction[P, T]: ...
581581

582582
@overload
583583
def make_injected_function[**P, T](
584584
self,
585585
wrapped: Callable[P, Awaitable[T]],
586586
/,
587-
threadsafe: bool = ...,
587+
threadsafe: bool | None = ...,
588588
) -> AsyncInjectedFunction[P, T]: ...
589589

590590
def make_injected_function[**P, T](
591591
self,
592592
wrapped: Callable[P, T],
593593
/,
594-
threadsafe: bool = False,
594+
threadsafe: bool | None = None,
595595
) -> InjectedFunction[P, T]:
596596
metadata = InjectMetadata(wrapped, threadsafe)
597597

@@ -609,7 +609,7 @@ def make_async_factory[T](
609609
self,
610610
wrapped: type[T],
611611
/,
612-
threadsafe: bool = False,
612+
threadsafe: bool | None = None,
613613
) -> Callable[..., Awaitable[T]]:
614614
factory: InjectedFunction[..., T] = self.make_injected_function(
615615
wrapped,
@@ -621,13 +621,18 @@ async def afind_instance[T](
621621
self,
622622
cls: InputType[T],
623623
*,
624-
threadsafe: bool = False,
624+
threadsafe: bool | None = None,
625625
) -> T:
626626
with get_lock(threadsafe):
627627
injectable = self[cls]
628628
return await injectable.aget_instance()
629629

630-
def find_instance[T](self, cls: InputType[T], *, threadsafe: bool = False) -> T:
630+
def find_instance[T](
631+
self,
632+
cls: InputType[T],
633+
*,
634+
threadsafe: bool | None = None,
635+
) -> T:
631636
with get_lock(threadsafe):
632637
injectable = self[cls]
633638
return injectable.get_instance()
@@ -638,7 +643,7 @@ async def aget_instance[T, Default](
638643
cls: InputType[T],
639644
default: Default,
640645
*,
641-
threadsafe: bool = ...,
646+
threadsafe: bool | None = ...,
642647
) -> T | Default: ...
643648

644649
@overload
@@ -647,15 +652,15 @@ async def aget_instance[T](
647652
cls: InputType[T],
648653
default: T = ...,
649654
*,
650-
threadsafe: bool = ...,
655+
threadsafe: bool | None = ...,
651656
) -> T: ...
652657

653658
async def aget_instance[T, Default](
654659
self,
655660
cls: InputType[T],
656661
default: Default = NotImplemented,
657662
*,
658-
threadsafe: bool = False,
663+
threadsafe: bool | None = None,
659664
) -> T | Default:
660665
try:
661666
return await self.afind_instance(cls, threadsafe=threadsafe)
@@ -668,7 +673,7 @@ def get_instance[T, Default](
668673
cls: InputType[T],
669674
default: Default,
670675
*,
671-
threadsafe: bool = ...,
676+
threadsafe: bool | None = ...,
672677
) -> T | Default: ...
673678

674679
@overload
@@ -677,15 +682,15 @@ def get_instance[T](
677682
cls: InputType[T],
678683
default: T = ...,
679684
*,
680-
threadsafe: bool = ...,
685+
threadsafe: bool | None = ...,
681686
) -> T: ...
682687

683688
def get_instance[T, Default](
684689
self,
685690
cls: InputType[T],
686691
default: Default = NotImplemented,
687692
*,
688-
threadsafe: bool = False,
693+
threadsafe: bool | None = None,
689694
) -> T | Default:
690695
try:
691696
return self.find_instance(cls, threadsafe=threadsafe)
@@ -698,7 +703,7 @@ def aget_lazy_instance[T, Default](
698703
cls: InputType[T],
699704
default: Default,
700705
*,
701-
threadsafe: bool = ...,
706+
threadsafe: bool | None = ...,
702707
) -> Awaitable[T | Default]: ...
703708

704709
@overload
@@ -707,15 +712,15 @@ def aget_lazy_instance[T](
707712
cls: InputType[T],
708713
default: T = ...,
709714
*,
710-
threadsafe: bool = ...,
715+
threadsafe: bool | None = ...,
711716
) -> Awaitable[T]: ...
712717

713718
def aget_lazy_instance[T, Default](
714719
self,
715720
cls: InputType[T],
716721
default: Default = NotImplemented,
717722
*,
718-
threadsafe: bool = False,
723+
threadsafe: bool | None = None,
719724
) -> Awaitable[T | Default]:
720725
function = self.make_injected_function(
721726
lambda instance=default: instance,
@@ -730,7 +735,7 @@ def get_lazy_instance[T, Default](
730735
cls: InputType[T],
731736
default: Default,
732737
*,
733-
threadsafe: bool = ...,
738+
threadsafe: bool | None = ...,
734739
) -> Invertible[T | Default]: ...
735740

736741
@overload
@@ -739,15 +744,15 @@ def get_lazy_instance[T](
739744
cls: InputType[T],
740745
default: T = ...,
741746
*,
742-
threadsafe: bool = ...,
747+
threadsafe: bool | None = ...,
743748
) -> Invertible[T]: ...
744749

745750
def get_lazy_instance[T, Default](
746751
self,
747752
cls: InputType[T],
748753
default: Default = NotImplemented,
749754
*,
750-
threadsafe: bool = False,
755+
threadsafe: bool | None = None,
751756
) -> Invertible[T | Default]:
752757
function = self.make_injected_function(
753758
lambda instance=default: instance,
@@ -1013,7 +1018,12 @@ class InjectMetadata[**P, T](Caller[P, T], EventListener):
10131018
__tasks: deque[Callable[..., Any]]
10141019
__wrapped: Callable[P, T]
10151020

1016-
def __init__(self, wrapped: Callable[P, T], /, threadsafe: bool) -> None:
1021+
def __init__(
1022+
self,
1023+
wrapped: Callable[P, T],
1024+
/,
1025+
threadsafe: bool | None = None,
1026+
) -> None:
10171027
self.__dependencies = Dependencies.empty()
10181028
self.__lock = get_lock(threadsafe)
10191029
self.__owner = None

injection/_core/scope.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async def adefine_scope(
130130
name: str,
131131
/,
132132
kind: ScopeKind | ScopeKindStr = ScopeKind.get_default(),
133-
threadsafe: bool = False,
133+
threadsafe: bool | None = None,
134134
) -> AsyncIterator[ScopeFacade]:
135135
async with AsyncScope() as scope:
136136
with _bind_scope(name, scope, kind, threadsafe) as facade:
@@ -142,7 +142,7 @@ def define_scope(
142142
name: str,
143143
/,
144144
kind: ScopeKind | ScopeKindStr = ScopeKind.get_default(),
145-
threadsafe: bool = False,
145+
threadsafe: bool | None = None,
146146
) -> Iterator[ScopeFacade]:
147147
with SyncScope() as scope:
148148
with _bind_scope(name, scope, kind, threadsafe) as facade:
@@ -194,7 +194,7 @@ def _bind_scope(
194194
name: str,
195195
scope: Scope,
196196
kind: ScopeKind | ScopeKindStr,
197-
threadsafe: bool,
197+
threadsafe: bool | None,
198198
) -> Iterator[ScopeFacade]:
199199
lock = get_lock(threadsafe)
200200

0 commit comments

Comments
 (0)