Skip to content

Commit 9e4be34

Browse files
committed
fixed nasty bug in type_safe on how dicts with primitive types were being serialised (the fix is quite elegant, since it reuses the existing main serialisation method)
1 parent 2d4d5a8 commit 9e4be34

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

osbot_utils/utils/Objects.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,13 @@ def serialize_to_dict(obj):
288288
elif isinstance(obj, (set, frozenset)):
289289
return [serialize_to_dict(item) for item in obj]
290290
elif isinstance(obj, dict):
291-
serialized_dict = {} # todo: refactor to separate method
291+
serialized_dict = {}
292292
for key, value in obj.items():
293-
if isinstance(key, type): # Handle type keys by converting to fully qualified string
294-
serialized_key = f"{key.__module__}.{key.__name__}"
295-
else:
296-
serialized_key = key
297-
serialized_dict[serialized_key] = serialize_to_dict(value) # Recursively serialize the value
293+
serialized_key = serialize_to_dict(key) # Recursively handle ALL key types
294+
serialized_value = serialize_to_dict(value)
295+
serialized_dict[serialized_key] = serialized_value
298296
return serialized_dict
299-
#return {key: serialize_to_dict(value) for key, value in obj.items()}
297+
300298
elif callable(obj) and not isinstance(obj, type): # For functions/lambdas, return a string representation
301299
if hasattr(obj, '__name__'):
302300
return obj.__name__

tests/unit/type_safe/primitives/domains/identifiers/test_Cache_Id.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
from typing import List
2-
from unittest import TestCase
31
import pytest
4-
from osbot_utils.testing.__ import __
5-
from osbot_utils.type_safe.Type_Safe import Type_Safe
6-
from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive
7-
from osbot_utils.type_safe.primitives.domains.identifiers.Cache_Id import Cache_Id
2+
from typing import List
3+
from unittest import TestCase
4+
from osbot_utils.testing.__ import __
5+
from osbot_utils.type_safe.Type_Safe import Type_Safe
6+
from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive
7+
from osbot_utils.type_safe.primitives.domains.identifiers.Cache_Id import Cache_Id
88
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
99
from osbot_utils.type_safe.type_safe_core.collections.Type_Safe__List import Type_Safe__List
10-
from osbot_utils.utils.Json import json_to_str, json_round_trip
11-
from osbot_utils.utils.Misc import is_guid
12-
from osbot_utils.utils.Objects import base_classes
10+
from osbot_utils.utils.Json import json_to_str, json_round_trip
11+
from osbot_utils.utils.Misc import is_guid
12+
from osbot_utils.utils.Objects import base_classes
1313

1414

1515
class test_Cache_Id(TestCase):
@@ -80,7 +80,7 @@ def test__init__validates_like_random_guid(self):
8080
# ═══════════════════════════════════════════════════════════════════════════════
8181

8282
def test__is_string_subclass(self): # Test that Cache_Id is a string
83-
cache_id = Cache_Id()
83+
cache_id = Cache_Id(Random_Guid())
8484

8585
assert isinstance(cache_id, str)
8686
assert isinstance(cache_id, Random_Guid)

tests/unit/type_safe/primitives/domains/identifiers/test_Graph_Id.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ def test__init__with_bad(self):
6161
# ═══════════════════════════════════════════════════════════════════════════════
6262

6363
def test__is_string_subclass(self): # Test that Graph_Id is a string
64-
graph_id = Graph_Id()
64+
graph_id = Graph_Id(Obj_Id())
6565

6666
assert isinstance(graph_id, str )
6767
assert isinstance(graph_id, Graph_Id )
6868

6969
def test__can_be_used_as_string(self): # Test string operations work
70-
graph_id = Graph_Id()
70+
graph_id = Graph_Id(Obj_Id())
7171

7272
assert graph_id.upper() == graph_id.upper() # String methods work
7373
assert graph_id.lower() == graph_id.lower()
@@ -130,7 +130,7 @@ def test__multiple_empty_instances(self):
130130
assert type(empty) is Graph_Id
131131

132132
def test__use_in_dict_key(self): # Test Graph_Id can be used as dict key
133-
graph_id = Graph_Id()
133+
graph_id = Graph_Id(Obj_Id())
134134
data = {graph_id: 'test_value'}
135135

136136
assert data[graph_id] == 'test_value'

0 commit comments

Comments
 (0)