1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
- import inspect
5
4
from abc import ABC , abstractmethod
6
5
from collections import OrderedDict
7
6
from collections .abc import (
17
16
from dataclasses import dataclass , field
18
17
from enum import StrEnum
19
18
from functools import partialmethod , singledispatchmethod , update_wrapper
20
- from inspect import Signature , isclass , iscoroutinefunction
19
+ from inspect import Signature , isclass , iscoroutinefunction , markcoroutinefunction
20
+ from inspect import signature as inspect_signature
21
21
from logging import Logger , getLogger
22
22
from queue import Empty , Queue
23
23
from types import MethodType
29
29
NamedTuple ,
30
30
Protocol ,
31
31
Self ,
32
- TypeGuard ,
33
32
overload ,
34
- override ,
35
33
runtime_checkable ,
36
34
)
37
35
from uuid import uuid4
@@ -76,7 +74,6 @@ class LocatorDependenciesUpdated[T](LocatorEvent):
76
74
classes : Collection [InputType [T ]]
77
75
mode : Mode
78
76
79
- @override
80
77
def __str__ (self ) -> str :
81
78
length = len (self .classes )
82
79
formatted_types = ", " .join (f"`{ cls } `" for cls in self .classes )
@@ -95,7 +92,6 @@ class ModuleEvent(Event, ABC):
95
92
class ModuleEventProxy (ModuleEvent ):
96
93
event : Event
97
94
98
- @override
99
95
def __str__ (self ) -> str :
100
96
return f"`{ self .module } ` has propagated an event: { self .origin } "
101
97
@@ -116,7 +112,6 @@ class ModuleAdded(ModuleEvent):
116
112
module_added : Module
117
113
priority : Priority
118
114
119
- @override
120
115
def __str__ (self ) -> str :
121
116
return f"`{ self .module } ` now uses `{ self .module_added } `."
122
117
@@ -125,7 +120,6 @@ def __str__(self) -> str:
125
120
class ModuleRemoved (ModuleEvent ):
126
121
module_removed : Module
127
122
128
- @override
129
123
def __str__ (self ) -> str :
130
124
return f"`{ self .module } ` no longer uses `{ self .module_removed } `."
131
125
@@ -135,7 +129,6 @@ class ModulePriorityUpdated(ModuleEvent):
135
129
module_updated : Module
136
130
priority : Priority
137
131
138
- @override
139
132
def __str__ (self ) -> str :
140
133
return (
141
134
f"In `{ self .module } `, the priority `{ self .priority } ` "
@@ -242,7 +235,6 @@ class Locator(Broker):
242
235
243
236
static_hooks : ClassVar [LocatorHooks [Any ]] = LocatorHooks .default ()
244
237
245
- @override
246
238
def __getitem__ [T ](self , cls : InputType [T ], / ) -> Injectable [T ]:
247
239
for input_class in self .__standardize_inputs ((cls ,)):
248
240
try :
@@ -254,15 +246,13 @@ def __getitem__[T](self, cls: InputType[T], /) -> Injectable[T]:
254
246
255
247
raise NoInjectable (cls )
256
248
257
- @override
258
249
def __contains__ (self , cls : InputType [Any ], / ) -> bool :
259
250
return any (
260
251
input_class in self .__records
261
252
for input_class in self .__standardize_inputs ((cls ,))
262
253
)
263
254
264
255
@property
265
- @override
266
256
def is_locked (self ) -> bool :
267
257
return any (injectable .is_locked for injectable in self .__injectables )
268
258
@@ -284,15 +274,13 @@ def update[T](self, updater: Updater[T]) -> Self:
284
274
285
275
return self
286
276
287
- @override
288
277
@synchronized ()
289
278
def unlock (self ) -> Self :
290
279
for injectable in self .__injectables :
291
280
injectable .unlock ()
292
281
293
282
return self
294
283
295
- @override
296
284
async def all_ready (self ) -> None :
297
285
for injectable in self .__injectables :
298
286
await injectable .aget_instance ()
@@ -387,20 +375,17 @@ class Module(Broker, EventListener):
387
375
def __post_init__ (self ) -> None :
388
376
self .__locator .add_listener (self )
389
377
390
- @override
391
378
def __getitem__ [T ](self , cls : InputType [T ], / ) -> Injectable [T ]:
392
379
for broker in self .__brokers :
393
380
with suppress (KeyError ):
394
381
return broker [cls ]
395
382
396
383
raise NoInjectable (cls )
397
384
398
- @override
399
385
def __contains__ (self , cls : InputType [Any ], / ) -> bool :
400
386
return any (cls in broker for broker in self .__brokers )
401
387
402
388
@property
403
- @override
404
389
def is_locked (self ) -> bool :
405
390
return any (broker .is_locked for broker in self .__brokers )
406
391
@@ -695,15 +680,13 @@ def change_priority(self, module: Module, priority: Priority | PriorityStr) -> S
695
680
696
681
return self
697
682
698
- @override
699
683
@synchronized ()
700
684
def unlock (self ) -> Self :
701
685
for broker in self .__brokers :
702
686
broker .unlock ()
703
687
704
688
return self
705
689
706
- @override
707
690
async def all_ready (self ) -> None :
708
691
for broker in self .__brokers :
709
692
await broker .all_ready ()
@@ -720,7 +703,6 @@ def remove_listener(self, listener: EventListener) -> Self:
720
703
self .__channel .remove_listener (listener )
721
704
return self
722
705
723
- @override
724
706
def on_event (self , event : Event , / ) -> ContextManager [None ] | None :
725
707
self_event = ModuleEventProxy (self , event )
726
708
return self .dispatch (self_event )
@@ -890,7 +872,7 @@ def signature(self) -> Signature:
890
872
return self .__signature
891
873
892
874
with synchronized ():
893
- signature = inspect . signature (self .wrapped , eval_str = True )
875
+ signature = inspect_signature (self .wrapped , eval_str = True )
894
876
self .__signature = signature
895
877
896
878
return signature
@@ -915,13 +897,11 @@ def bind(
915
897
additional_arguments = self .__dependencies .get_arguments ()
916
898
return self .__bind (args , kwargs , additional_arguments )
917
899
918
- @override
919
900
async def acall (self , / , * args : P .args , ** kwargs : P .kwargs ) -> T :
920
901
self .__setup ()
921
902
arguments = await self .abind (args , kwargs )
922
903
return self .wrapped (* arguments .args , ** arguments .kwargs )
923
904
924
- @override
925
905
def call (self , / , * args : P .args , ** kwargs : P .kwargs ) -> T :
926
906
self .__setup ()
927
907
arguments = self .bind (args , kwargs )
@@ -957,7 +937,6 @@ def decorator(wp: Callable[_P, _T]) -> Callable[_P, _T]:
957
937
return decorator (wrapped ) if wrapped else decorator
958
938
959
939
@singledispatchmethod
960
- @override
961
940
def on_event (self , event : Event , / ) -> ContextManager [None ] | None : # type: ignore[override]
962
941
return None
963
942
@@ -1014,11 +993,9 @@ def __init__(self, metadata: InjectMetadata[P, T]) -> None:
1014
993
update_wrapper (self , metadata .wrapped )
1015
994
self .__inject_metadata__ = metadata
1016
995
1017
- @override
1018
996
def __repr__ (self ) -> str : # pragma: no cover
1019
997
return repr (self .__inject_metadata__ .wrapped )
1020
998
1021
- @override
1022
999
def __str__ (self ) -> str : # pragma: no cover
1023
1000
return str (self .__inject_metadata__ .wrapped )
1024
1001
@@ -1043,34 +1020,23 @@ def __set_name__(self, owner: type, name: str) -> None:
1043
1020
class AsyncInjectedFunction [** P , T ](InjectedFunction [P , Awaitable [T ]]):
1044
1021
__slots__ = ()
1045
1022
1046
- @override
1023
+ def __init__ (self , metadata : InjectMetadata [P , Awaitable [T ]]) -> None :
1024
+ super ().__init__ (metadata )
1025
+ markcoroutinefunction (self )
1026
+
1047
1027
async def __call__ (self , / , * args : P .args , ** kwargs : P .kwargs ) -> T :
1048
1028
return await (await self .__inject_metadata__ .acall (* args , ** kwargs ))
1049
1029
1050
1030
1051
1031
class SyncInjectedFunction [** P , T ](InjectedFunction [P , T ]):
1052
1032
__slots__ = ()
1053
1033
1054
- @override
1055
1034
def __call__ (self , / , * args : P .args , ** kwargs : P .kwargs ) -> T :
1056
1035
return self .__inject_metadata__ .call (* args , ** kwargs )
1057
1036
1058
1037
1059
- def _is_coroutine_function [** P , T ](
1060
- function : Callable [P , T ] | Callable [P , Awaitable [T ]],
1061
- ) -> TypeGuard [Callable [P , Awaitable [T ]]]:
1062
- if iscoroutinefunction (function ):
1063
- return True
1064
-
1065
- elif isclass (function ):
1066
- return False
1067
-
1068
- call = getattr (function , "__call__" , None )
1069
- return iscoroutinefunction (call )
1070
-
1071
-
1072
1038
def _get_caller [** P , T ](function : Callable [P , T ]) -> Caller [P , T ]:
1073
- if _is_coroutine_function (function ):
1039
+ if iscoroutinefunction (function ):
1074
1040
return AsyncCaller (function )
1075
1041
1076
1042
elif isinstance (function , InjectedFunction ):
0 commit comments