Skip to content

Commit f8abefa

Browse files
committed
Merge dev into main
2 parents 1513696 + 118c66a commit f8abefa

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

osbot_utils/type_safe/type_safe_core/collections/Type_Safe__Dict.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
from typing import Type
23
from osbot_utils.testing.__ import __
34
from osbot_utils.type_safe.Type_Safe__Base import Type_Safe__Base
@@ -64,19 +65,19 @@ def serialize_value(v):
6465
else: # set
6566
return set(serialized)
6667
else:
67-
return serialize_to_dict(v) # Use serialize_to_dict for unknown types (so that we don't return a non json object)
68+
return serialize_to_dict(v) # Use serialize_to_dict for unknown types (so that we don't return a non json object)
6869

6970

7071
result = {}
7172
for key, value in self.items():
72-
# Handle Type objects as keys
73-
if isinstance(key, (type, Type)):
73+
if isinstance(key, (type, Type)): # Handle Type objects as keys
7474
key = f"{key.__module__}.{key.__name__}"
75+
elif isinstance(key, Enum): # Handle Enum keys
76+
key = key.value
7577
elif isinstance(key, Type_Safe__Primitive):
7678
key = key.__to_primitive__()
7779

78-
# Use recursive serialization for values
79-
result[key] = serialize_value(value)
80+
result[key] = serialize_value(value) # Use recursive serialization for values
8081

8182
return result
8283

tests/unit/type_safe/type_safe_core/_bugs/test_Type_Safe__Dict__bugs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class test_Type_Safe__Dict__bugs(TestCase):
88

99

10+
1011
def test__bug__type_safe_list_with_dict_any_type__and__error_message_is_confusing(self): # Document bug where Dict[str, any] fails in List ,and the error message doesn't mention that Any works
1112

1213
class Schema__Order__Bug(Type_Safe):

tests/unit/type_safe/type_safe_core/_bugs/test_Type_Safe__bugs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class test_Type_Safe__bugs(TestCase):
77

8+
89
def test__bug__property_descriptor_handling__doesnt_enforce_type_safety(self):
910

1011
class Test_Class(Type_Safe):

tests/unit/type_safe/type_safe_core/_regression/test_Type_Safe__Dict__regression.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from enum import Enum
23
from unittest import TestCase
34
from typing import Dict, Type, Set, Any, List
45
from osbot_utils.testing.__helpers import obj
@@ -357,4 +358,26 @@ class An_Class_2(Type_Safe):
357358
an_class_2 = An_Class_2()
358359
an_class_2.all_paths = data # FIXED:
359360
assert an_class_2.json() == {"all_paths": data } # confirm roundtrip with .json()
360-
assert an_class_2.obj() == __(all_paths = obj(data)) # confirm roundtrip with obj().
361+
assert an_class_2.obj() == __(all_paths = obj(data)) # confirm roundtrip with obj().
362+
363+
def test__regression__dict__json__enum__conversion(self):
364+
class An_Enum(str, Enum):
365+
A = 'A'
366+
B = 'B'
367+
C = 'C'
368+
369+
class An_Class(Type_Safe):
370+
an_dict : Dict[An_Enum, int]
371+
372+
# assert An_Class() .obj () == __( an_dict = __())
373+
# assert An_Class() .json() == { 'an_dict': {} }
374+
# assert An_Class(an_dict={'A': 42}).obj() == __( an_dict = __(An_Enum_A= 42))
375+
# assert An_Class(an_dict={'A': 42}).json() == { 'an_dict': { An_Enum.A: 42}} # BUG: "An_Enum.A" is an Enum, and it should be a String (namely 'A')
376+
# assert An_Class.from_json(An_Class(an_dict={'A': 42}).json()).obj() == __(an_dict=__(An_Enum_A=42)) # these 3 work
377+
# assert An_Class.from_json({ 'an_dict': { An_Enum.A: 42}} ).obj() == __(an_dict=__(An_Enum_A=42)) # but .json() should NOT be this
378+
# assert An_Class.from_json({ 'an_dict': { 'A' : 42}} ).obj() == __(an_dict=__(An_Enum_A=42)) # it should be this
379+
assert An_Class(an_dict={'A': 42}).obj() == __( an_dict = __(A = 42)) # FIXED: An_Enum_A is now just A (which is a better result
380+
assert An_Class(an_dict={'A': 42}).json() == { 'an_dict': { 'A': 42}} # FIXED
381+
assert An_Class.from_json(An_Class(an_dict={'A': 42}).json()).obj() == __(an_dict=__(A=42)) # FIXED
382+
assert An_Class.from_json({ 'an_dict': { An_Enum.A: 42}} ).obj() == __(an_dict=__(A=42)) #
383+
assert An_Class.from_json({ 'an_dict': { 'A' : 42}} ).obj() == __(an_dict=__(A=42)) #

0 commit comments

Comments
 (0)