@@ -33,8 +33,8 @@ def __init__(
33
33
self ._mutable = mutable
34
34
self ._validator = validator
35
35
if name in os .environ :
36
- self ._value = validator (os .environ [name ])
37
- logger .debug (f"{ self ._name } ={ self .get () } " )
36
+ self ._current = validator (os .environ [name ])
37
+ logger .debug (f"{ self ._name } ={ self .current } " )
38
38
ALL_OPTIONS .add (self )
39
39
40
40
@property
@@ -52,39 +52,44 @@ def default(self) -> _O:
52
52
"""This option's default value"""
53
53
return self ._default
54
54
55
+ @property
56
+ def current (self ) -> _O :
57
+ try :
58
+ return self ._current
59
+ except AttributeError :
60
+ return self ._default
61
+
62
+ @current .setter
63
+ def current (self , new : _O ) -> None :
64
+ self .set_current (new )
65
+ return None
66
+
55
67
def is_set (self ) -> bool :
56
68
"""Whether this option has a value other than its default."""
57
- return hasattr (self , "_value" )
58
-
59
- def get (self ) -> _O :
60
- """Get the current value of this option."""
61
- return cast (_O , getattr (self , "_value" , self ._default ))
69
+ return hasattr (self , "_current" )
62
70
63
- def set (self , new : Any ) -> None :
71
+ def set_current (self , new : Any ) -> None :
64
72
"""Set the value of this option
65
73
66
74
Raises a ``TypeError`` if this option is not :attr:`Option.mutable`.
67
75
"""
68
76
if not self ._mutable :
69
77
raise TypeError (f"{ self } cannot be modified after initial load" )
70
- self ._value = self ._validator (new )
71
- logger .debug (f"{ self ._name } ={ self ._value } " )
78
+ self ._current = self ._validator (new )
79
+ logger .debug (f"{ self ._name } ={ self ._current } " )
72
80
73
81
def set_default (self , new : _O ) -> _O :
74
82
"""Set the value of this option if not :meth:`Option.is_set`
75
83
76
84
Returns the current value (a la :meth:`dict.set_default`)
77
85
"""
78
- if not hasattr (self , "_value " ):
79
- self .set (new )
80
- return self ._value
86
+ if not hasattr (self , "_current " ):
87
+ self .set_current (new )
88
+ return self ._current
81
89
82
90
def reload (self ) -> None :
83
- """Reload this option from its environment variable
84
-
85
- Returns the old value of the option.
86
- """
87
- self .set (os .environ .get (self ._name , self ._default ))
91
+ """Reload this option from its environment variable"""
92
+ self .set_current (os .environ .get (self ._name , self ._default ))
88
93
89
94
def reset (self ) -> None :
90
95
"""Reset the value of this option to its default setting
@@ -93,7 +98,7 @@ def reset(self) -> None:
93
98
"""
94
99
if not self ._mutable :
95
100
raise TypeError (f"{ self } cannot be modified after initial load" )
96
- delattr (self , "_value " )
101
+ delattr (self , "_current " )
97
102
98
103
def __repr__ (self ) -> str :
99
- return f"Option({ self ._name } ={ self .get () !r} )"
104
+ return f"Option({ self ._name } ={ self .current !r} )"
0 commit comments