22import hashlib
33import os
44import pickle
5- from typing import Optional , TypedDict , Union
5+ from collections .abc import Mapping
6+ from dataclasses import dataclass , replace
7+ from typing import Optional , Union
68
79from ._types import Backend , HashFunc , Mongetter
810
@@ -16,35 +18,24 @@ def _default_hash_func(args, kwds):
1618 return hashlib .sha256 (serialized ).hexdigest ()
1719
1820
19- class Params (TypedDict ):
20- """Type definition for cachier parameters."""
21-
22- caching_enabled : bool
23- hash_func : HashFunc
24- backend : Backend
25- mongetter : Optional [Mongetter ]
26- stale_after : datetime .timedelta
27- next_time : bool
28- cache_dir : Union [str , os .PathLike ]
29- pickle_reload : bool
30- separate_files : bool
31- wait_for_calc_timeout : int
32- allow_none : bool
33-
34-
35- _default_params : Params = {
36- "caching_enabled" : True ,
37- "hash_func" : _default_hash_func ,
38- "backend" : "pickle" ,
39- "mongetter" : None ,
40- "stale_after" : datetime .timedelta .max ,
41- "next_time" : False ,
42- "cache_dir" : "~/.cachier/" ,
43- "pickle_reload" : True ,
44- "separate_files" : False ,
45- "wait_for_calc_timeout" : 0 ,
46- "allow_none" : False ,
47- }
21+ @dataclass
22+ class Params :
23+ """Default definition for cachier parameters."""
24+
25+ caching_enabled : bool = True
26+ hash_func : HashFunc = _default_hash_func
27+ backend : Backend = "pickle"
28+ mongetter : Optional [Mongetter ] = None
29+ stale_after : datetime .timedelta = datetime .timedelta .max
30+ next_time : bool = False
31+ cache_dir : Union [str , os .PathLike ] = "~/.cachier/"
32+ pickle_reload : bool = True
33+ separate_files : bool = False
34+ wait_for_calc_timeout : int = 0
35+ allow_none : bool = False
36+
37+
38+ _global_params = Params ()
4839
4940
5041def _update_with_defaults (
@@ -57,11 +48,25 @@ def _update_with_defaults(
5748 if kw_name in func_kwargs :
5849 return func_kwargs .pop (kw_name )
5950 if param is None :
60- return cachier .config ._default_params [ name ]
51+ return getattr ( cachier .config ._global_params , name )
6152 return param
6253
6354
64- def set_default_params (** params ):
55+ def set_default_params (** params : Mapping ) -> None :
56+ """Configure default parameters applicable to all memoized functions."""
57+ # It is kept for backwards compatibility with desperation warning
58+ import warnings
59+
60+ warnings .warn (
61+ "Called `set_default_params` is deprecated and will be removed."
62+ " Please use `set_global_params` instead." ,
63+ DeprecationWarning ,
64+ stacklevel = 2 ,
65+ )
66+ set_global_params (** params )
67+
68+
69+ def set_global_params (** params : Mapping ) -> None :
6570 """Configure global parameters applicable to all memoized functions.
6671
6772 This function takes the same keyword parameters as the ones defined in the
@@ -76,28 +81,46 @@ def set_default_params(**params):
7681 """
7782 import cachier
7883
79- valid_params = (
80- p for p in params .items () if p [0 ] in cachier .config ._default_params
84+ valid_params = {
85+ k : v
86+ for k , v in params .items ()
87+ if hasattr (cachier .config ._global_params , k )
88+ }
89+ cachier .config ._global_params = replace (
90+ cachier .config ._global_params , ** valid_params
91+ )
92+
93+
94+ def get_default_params () -> Params :
95+ """Get current set of default parameters."""
96+ # It is kept for backwards compatibility with desperation warning
97+ import warnings
98+
99+ warnings .warn (
100+ "Called `get_default_params` is deprecated and will be removed."
101+ " Please use `get_global_params` instead." ,
102+ DeprecationWarning ,
103+ stacklevel = 2 ,
81104 )
82- _default_params . update ( valid_params )
105+ return get_global_params ( )
83106
84107
85- def get_default_params () :
108+ def get_global_params () -> Params :
86109 """Get current set of default parameters."""
87110 import cachier
88111
89- return cachier .config ._default_params
112+ return cachier .config ._global_params
90113
91114
92115def enable_caching ():
93116 """Enable caching globally."""
94117 import cachier
95118
96- cachier .config ._default_params [ " caching_enabled" ] = True
119+ cachier .config ._global_params . caching_enabled = True
97120
98121
99122def disable_caching ():
100123 """Disable caching globally."""
101124 import cachier
102125
103- cachier .config ._default_params [ " caching_enabled" ] = False
126+ cachier .config ._global_params . caching_enabled = False
0 commit comments