-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool_utils.py
More file actions
100 lines (83 loc) · 2.59 KB
/
bool_utils.py
File metadata and controls
100 lines (83 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Boolean coercion utilities for GitHub Actions inputs.
GitHub Actions forwards workflow_call inputs as strings, so these helpers
accept a variety of truthy/falsy spellings and convert them to bool.
"""
from __future__ import annotations
__all__ = ["coerce_bool", "coerce_bool_strict"]
_TRUTHY = {"1", "true", "yes", "on"}
_FALSY = {"0", "false", "no", "off"}
def coerce_bool(value: object, *, default: bool) -> bool:
"""Coerce a value to bool, returning default for None/empty.
Parameters
----------
value
The value to coerce. Accepts bool, str, or None.
default
The value to return when ``value`` is None or an empty string.
Returns
-------
bool
The coerced boolean value.
Raises
------
ValueError
If the value is a string that cannot be interpreted as a boolean.
Examples
--------
>>> coerce_bool("true", default=False)
True
>>> coerce_bool(None, default=True)
True
>>> coerce_bool("", default=False)
False
"""
if value is None:
return default
if isinstance(value, bool):
return value
if isinstance(value, str):
normalised = value.strip().lower()
if not normalised:
return default
if normalised in _TRUTHY:
return True
if normalised in _FALSY:
return False
msg = f"Cannot interpret {value!r} as boolean"
raise ValueError(msg)
def coerce_bool_strict(value: bool | str, *, parameter: str) -> bool: # noqa: FBT001
"""Coerce a value to bool, raising ValueError with parameter name on failure.
Unlike :func:`coerce_bool`, this function treats empty strings as False
rather than falling back to a default, and includes the parameter name
in error messages.
Parameters
----------
value
The value to coerce. Accepts bool or str.
parameter
The name of the parameter, used in error messages.
Returns
-------
bool
The coerced boolean value.
Raises
------
ValueError
If the value cannot be interpreted as a boolean.
Examples
--------
>>> coerce_bool_strict("true", parameter="dry-run")
True
>>> coerce_bool_strict("", parameter="dry-run")
False
"""
if isinstance(value, bool):
return value
if isinstance(value, str):
normalised = value.strip().lower()
if normalised in _TRUTHY:
return True
if normalised in {*_FALSY, ""}:
return False
msg = f"Invalid value for {parameter}: {value!r}. Expected a boolean-like string."
raise ValueError(msg)