diff --git a/CHANGELOG.md b/CHANGELOG.md index bda02f79f1c0e2b2d6c5c5d36739c5a1c9652529..3628e62b2db8dcdfbfb32f9d47630f804ce2c4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # v2.8.6 * added `time_us` +* updated `stringify` to correctly handle None-cases diff --git a/satella/__init__.py b/satella/__init__.py index e2f4d6ccabb64f96df4c22260ef04eea01441dfc..0934f03c99a07db6f6e28f29dfdd3a74aee17d8c 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.8.6_a2' +__version__ = '2.8.6_a3' diff --git a/satella/coding/transforms/__init__.py b/satella/coding/transforms/__init__.py index dcf65cf286821e708227ee24817ef686d332555d..e1a099e50a534fbc9c185cb4548b05fe35aea350 100644 --- a/satella/coding/transforms/__init__.py +++ b/satella/coding/transforms/__init__.py @@ -5,7 +5,8 @@ __all__ = ['stringify'] def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = str, - recursively: bool = False) -> tp.Dict[str, str]: + recursively: bool = False, + str_none: bool = False) -> tp.Dict[str, str]: """ Stringify all object: @@ -19,6 +20,7 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s :param obj: a list or a dict :param stringifier: function that accepts any arguments and returns a string representation :param recursively: whether to recursively stringify elements, ie. stringify will be called on all the children + :param str_none: whether to return None if given a None. If True, "None" will be returned instead :return: stringified object """ if isinstance(obj, collections.abc.Mapping): @@ -27,5 +29,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s elif isinstance(obj, collections.abc.Sequence): make_str = (lambda obj2: stringify(obj2, stringifier, True)) if recursively else stringifier return [make_str(v) for v in obj] + elif obj is None: + if str_none: + return stringifier(None) + else: + return None else: return stringifier(obj) diff --git a/tests/test_coding/test_transforms.py b/tests/test_coding/test_transforms.py index 5a94cab89b3e596949bdf36f7f42066829fb75bc..28fa5c147a2c01fa390e2c477bc7440f05d49cfe 100644 --- a/tests/test_coding/test_transforms.py +++ b/tests/test_coding/test_transforms.py @@ -16,3 +16,6 @@ class MyTestCase(unittest.TestCase): dct2 = {1: [1, 2, 3]} self.assertEqual(stringify(dct2, recursively=True), {'1': ['1', '2', '3']}) + + self.assertIsNone(stringify(None)) + self.assertEqual(stringify(None, str_none=True), str(None))