1from __future__ import annotations
2
3from .base import Always, Filter, FilterOrBool, Never
4
5__all__ = [
6 "to_filter",
7 "is_true",
8]
9
10
11_always = Always()
12_never = Never()
13
14
15_bool_to_filter: dict[bool, Filter] = {
16 True: _always,
17 False: _never,
18}
19
20
21def to_filter(bool_or_filter: FilterOrBool) -> Filter:
22 """
23 Accept both booleans and Filters as input and
24 turn it into a Filter.
25 """
26 if isinstance(bool_or_filter, bool):
27 return _bool_to_filter[bool_or_filter]
28
29 if isinstance(bool_or_filter, Filter):
30 return bool_or_filter
31
32 raise TypeError(f"Expecting a bool or a Filter instance. Got {bool_or_filter!r}")
33
34
35def is_true(value: FilterOrBool) -> bool:
36 """
37 Test whether `value` is True. In case of a Filter, call it.
38
39 :param value: Boolean or `Filter` instance.
40 """
41 return to_filter(value)()