Add function to ensure a non-empty string.

This commit is contained in:
2026-04-19 19:10:45 +02:00
parent b71b93000e
commit 3b45c1a8ed
3 changed files with 57 additions and 2 deletions
+25 -1
View File
@@ -127,4 +127,28 @@ def assert_that_is_instance(obj: object, class_or_tuple: Union[type, types.Union
exception_message_expected += human_readable_list(_list_type_names(class_or_tuple), 'or', '\'')
else:
exception_message_expected = '\'' + class_or_tuple.__name__ + '\''
raise TypeError(f'expected {exception_message_expected}, got \'{type(obj).__name__}\'')
raise TypeError(f'expected {exception_message_expected}, got \'{type(obj).__name__}\'')
def non_empty_str(value: Union[str, None]) -> Union[str, None]:
'''
Returns the value or ``None``.
Returns the value if it is not empty and not `Ǹone``; otherwise,
returns ``None``.
Please note: values are stripped, i. e. if the value consists only
of spaces, it is also considered empty.
:param value: The value.
:type value: Union[str, None]
:return: The value or ``None``.
:rtype: Union[str, None]
'''
if value is None:
return None
if value.strip() == '':
return None
return value