@@ -73,8 +73,10 @@ def __init__(
73
73
num_args : Optional [int ],
74
74
uncached_args : Optional [Collection [str ]] = None ,
75
75
cache_context : bool = False ,
76
+ name : Optional [str ] = None ,
76
77
):
77
78
self .orig = orig
79
+ self .name = name or orig .__name__
78
80
79
81
arg_spec = inspect .getfullargspec (orig )
80
82
all_args = arg_spec .args
@@ -211,7 +213,7 @@ def __init__(
211
213
212
214
def __get__ (self , obj : Optional [Any ], owner : Optional [Type ]) -> Callable [..., Any ]:
213
215
cache : LruCache [CacheKey , Any ] = LruCache (
214
- cache_name = self .orig . __name__ ,
216
+ cache_name = self .name ,
215
217
max_size = self .max_entries ,
216
218
)
217
219
@@ -241,7 +243,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
241
243
242
244
wrapped = cast (_CachedFunction , _wrapped )
243
245
wrapped .cache = cache
244
- obj .__dict__ [self .orig . __name__ ] = wrapped
246
+ obj .__dict__ [self .name ] = wrapped
245
247
246
248
return wrapped
247
249
@@ -301,12 +303,14 @@ def __init__(
301
303
cache_context : bool = False ,
302
304
iterable : bool = False ,
303
305
prune_unread_entries : bool = True ,
306
+ name : Optional [str ] = None ,
304
307
):
305
308
super ().__init__ (
306
309
orig ,
307
310
num_args = num_args ,
308
311
uncached_args = uncached_args ,
309
312
cache_context = cache_context ,
313
+ name = name ,
310
314
)
311
315
312
316
if tree and self .num_args < 2 :
@@ -321,7 +325,7 @@ def __init__(
321
325
322
326
def __get__ (self , obj : Optional [Any ], owner : Optional [Type ]) -> Callable [..., Any ]:
323
327
cache : DeferredCache [CacheKey , Any ] = DeferredCache (
324
- name = self .orig . __name__ ,
328
+ name = self .name ,
325
329
max_entries = self .max_entries ,
326
330
tree = self .tree ,
327
331
iterable = self .iterable ,
@@ -372,7 +376,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
372
376
wrapped .cache = cache
373
377
wrapped .num_args = self .num_args
374
378
375
- obj .__dict__ [self .orig . __name__ ] = wrapped
379
+ obj .__dict__ [self .name ] = wrapped
376
380
377
381
return wrapped
378
382
@@ -393,6 +397,7 @@ def __init__(
393
397
cached_method_name : str ,
394
398
list_name : str ,
395
399
num_args : Optional [int ] = None ,
400
+ name : Optional [str ] = None ,
396
401
):
397
402
"""
398
403
Args:
@@ -403,7 +408,7 @@ def __init__(
403
408
but including list_name) to use as cache keys. Defaults to all
404
409
named args of the function.
405
410
"""
406
- super ().__init__ (orig , num_args = num_args , uncached_args = None )
411
+ super ().__init__ (orig , num_args = num_args , uncached_args = None , name = name )
407
412
408
413
self .list_name = list_name
409
414
@@ -525,7 +530,7 @@ def errback_all(f: Failure) -> None:
525
530
else :
526
531
return defer .succeed (results )
527
532
528
- obj .__dict__ [self .orig . __name__ ] = wrapped
533
+ obj .__dict__ [self .name ] = wrapped
529
534
530
535
return wrapped
531
536
@@ -577,6 +582,7 @@ def cached(
577
582
cache_context : bool = False ,
578
583
iterable : bool = False ,
579
584
prune_unread_entries : bool = True ,
585
+ name : Optional [str ] = None ,
580
586
) -> Callable [[F ], _CachedFunction [F ]]:
581
587
func = lambda orig : DeferredCacheDescriptor (
582
588
orig ,
@@ -587,13 +593,18 @@ def cached(
587
593
cache_context = cache_context ,
588
594
iterable = iterable ,
589
595
prune_unread_entries = prune_unread_entries ,
596
+ name = name ,
590
597
)
591
598
592
599
return cast (Callable [[F ], _CachedFunction [F ]], func )
593
600
594
601
595
602
def cachedList (
596
- * , cached_method_name : str , list_name : str , num_args : Optional [int ] = None
603
+ * ,
604
+ cached_method_name : str ,
605
+ list_name : str ,
606
+ num_args : Optional [int ] = None ,
607
+ name : Optional [str ] = None ,
597
608
) -> Callable [[F ], _CachedFunction [F ]]:
598
609
"""Creates a descriptor that wraps a function in a `DeferredCacheListDescriptor`.
599
610
@@ -628,6 +639,7 @@ def batch_do_something(self, first_arg, second_args):
628
639
cached_method_name = cached_method_name ,
629
640
list_name = list_name ,
630
641
num_args = num_args ,
642
+ name = name ,
631
643
)
632
644
633
645
return cast (Callable [[F ], _CachedFunction [F ]], func )
0 commit comments