MCPcopy Index your code
hub / github.com/reactive-python/reactpy / Option

Class Option

src/py/reactpy/reactpy/_option.py:14–137  ·  view source on GitHub ↗

An option that can be set using an environment variable of the same name

Source from the content-addressed store, hash-verified

12
13
14class Option(Generic[_O]):
15 """An option that can be set using an environment variable of the same name"""
16
17 def __init__(
18 self,
19 name: str,
20 default: _O = UNDEFINED,
21 mutable: bool = True,
22 parent: Option[_O] | None = None,
23 validator: Callable[[Any], _O] = lambda x: cast(_O, x),
24 ) -> None:
25 self._name = name
26 self._mutable = mutable
27 self._validator = validator
28 self._subscribers: list[Callable[[_O], None]] = []
29
30 if name in os.environ:
31 self._current = validator(os.environ[name])
32
33 if parent is not None:
34 if not (parent.mutable and self.mutable):
35 raise TypeError("Parent and child options must be mutable")
36 self._default = parent.default
37 parent.subscribe(self.set_current)
38 elif default is not UNDEFINED:
39 self._default = default
40 else:
41 raise TypeError("Must specify either a default or a parent option")
42
43 logger.debug(f"{self._name}={self.current}")
44
45 @property
46 def name(self) -> str:
47 """The name of this option (used to load environment variables)"""
48 return self._name
49
50 @property
51 def mutable(self) -> bool:
52 """Whether this option can be modified after being loaded"""
53 return self._mutable
54
55 @property
56 def default(self) -> _O:
57 """This option's default value"""
58 return self._default
59
60 @property
61 def current(self) -> _O:
62 try:
63 return self._current
64 except AttributeError:
65 return self._default
66
67 @current.setter
68 def current(self, new: _O) -> None:
69 self.set_current(new)
70
71 @current.deleter

Callers 15

test_option_reprFunction · 0.90
test_option_from_defaultFunction · 0.90
test_option_validatorFunction · 0.90
test_immutable_optionFunction · 0.90
test_option_resetFunction · 0.90
test_option_reloadFunction · 0.90
test_option_setFunction · 0.90
test_option_set_defaultFunction · 0.90
test_option_subscribeFunction · 0.90
test_option_parentFunction · 0.90

Calls

no outgoing calls

Tested by 14

test_option_reprFunction · 0.72
test_option_from_defaultFunction · 0.72
test_option_validatorFunction · 0.72
test_immutable_optionFunction · 0.72
test_option_resetFunction · 0.72
test_option_reloadFunction · 0.72
test_option_setFunction · 0.72
test_option_set_defaultFunction · 0.72
test_option_subscribeFunction · 0.72
test_option_parentFunction · 0.72