diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py index 45badb7c..c406213a 100644 --- a/oslo_utils/strutils.py +++ b/oslo_utils/strutils.py @@ -172,21 +172,33 @@ def int_from_bool_as_string(subject: Any) -> int: return int(bool_from_string(subject)) +@overload def bool_from_string( subject: Any, strict: bool = False, default: bool = False -) -> bool: +) -> bool: ... + + +@overload +def bool_from_string( + subject: Any, strict: bool = False, default: bool | None = False +) -> bool | None: ... + + +def bool_from_string( + subject: Any, strict: bool = False, default: bool | None = False +) -> bool | None: """Interpret a subject as a boolean. A subject can be a boolean, a string or an integer. Boolean type value will be returned directly, otherwise the subject will be converted to a string. A case-insensitive match is performed such that strings - matching 't','true', 'on', 'y', 'yes', or '1' are considered True and, - when `strict=False`, anything else returns the value specified by - 'default'. + matching 't', 'true', 'on', 'y', 'yes', or '1' are considered True and, + when ``strict=False``, anything else returns the value specified by + ``default``. Useful for JSON-decoded stuff and config file parsing. - If `strict=True`, unrecognized values, including None, will raise a + If ``strict=True``, unrecognized values, including None, will raise a ValueError which is useful when parsing values passed in from an API call. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """