From 93a9b39d8a58afae8c6bb5cdef950d16a655c296 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 12 Mar 2026 22:09:47 +0000 Subject: [PATCH] typing: Allow None default for bool_from_string This is used in some places in lieu of strict to determine whether a valid value (or any value) was passed. We also fix some nits in the docstring. Change-Id: I82cb32732c3224f32466f4dfabd8a259a0d64537 Signed-off-by: Stephen Finucane --- oslo_utils/strutils.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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'. """