Skip to content

Commit 3e19f4c

Browse files
committed
Merge branch 'main' of github.com:Exabyte-io/code into chore/SOF-7602
2 parents de556cc + 92f6e79 commit 3e19f4c

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/py/mat3ra/code/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def from_json(cls: Type[T], json_str: str) -> T:
6262
def clean(cls: Type[T], config: Dict[str, Any]) -> Dict[str, Any]:
6363
# Validate the config; extra keys are dropped and defaults are substituted.
6464
validated = cls.model_validate(config, strict=False)
65-
return validated.model_dump(exclude_unset=False)
65+
return validated.model_dump(mode="json", exclude_unset=False)
6666

6767
def get_schema(self) -> Dict[str, Any]:
6868
return self.model_json_schema()
@@ -77,7 +77,7 @@ def get_cls_name(self) -> str:
7777
return self.__class__.__name__
7878

7979
def to_dict(self, exclude: Optional[List[str]] = None) -> Dict[str, Any]:
80-
return self.model_dump(exclude=set(exclude) if exclude else None)
80+
return self.model_dump(mode="json", exclude=set(exclude) if exclude else None)
8181

8282
def to_json(self, exclude: Optional[List[str]] = None) -> str:
8383
return self.model_dump_json(exclude=set(exclude) if exclude else None)

src/py/mat3ra/code/vector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List
22

33
import numpy as np
4-
from mat3ra.esse.models.core.abstract.point import PointSchema as Vector3DSchema
4+
from mat3ra.esse.models.core.abstract.vector_3d import Vector3dSchema as Vector3DSchema
55
from mat3ra.utils.mixins import RoundNumericValuesMixin
66
from pydantic import model_serializer
77

tests/py/unit/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from enum import Enum
23

34
from mat3ra.code.entity import InMemoryEntityPydantic
45
from pydantic import BaseModel
@@ -53,3 +54,17 @@ class ExampleNestedKeyAsClassInstanceClass(ExampleNestedSchema, InMemoryEntityPy
5354

5455
class ExampleDoubleNestedKeyAsClassInstancesClass(ExampleDoubleNestedSchema, InMemoryEntityPydantic):
5556
double_nested_key1: ExampleNestedKeyAsClassInstanceClass
57+
58+
59+
class SampleEnum(str, Enum):
60+
VALUE1 = "value1"
61+
VALUE2 = "value2"
62+
63+
64+
class SampleModelWithEnum(BaseModel):
65+
type: SampleEnum
66+
name: str = "test"
67+
68+
69+
class SampleEntityWithEnum(SampleModelWithEnum, InMemoryEntityPydantic):
70+
pass

tests/py/unit/test_entity.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
ExampleNestedKeyAsClassInstanceClass,
1919
ExampleNestedSchema,
2020
ExampleSchema,
21+
SampleEnum,
22+
SampleEntityWithEnum,
2123
)
2224

2325

@@ -165,6 +167,16 @@ def test_to_dict():
165167
assert result_exclude == {"key1": "value1"}
166168

167169

170+
def test_to_dict_with_enum():
171+
entity = SampleEntityWithEnum(type=SampleEnum.VALUE1, name="example")
172+
result = entity.to_dict()
173+
174+
assert isinstance(result, dict)
175+
assert not isinstance(result["type"], SampleEnum) # Should not be an enum object
176+
assert result == {"type": "value1", "name": "example"}
177+
assert result["type"] == "value1" # String, not enum object
178+
179+
168180
def test_to_json():
169181
entity = ExampleClass.create(REFERENCE_OBJECT_VALID)
170182
result = entity.to_json()

0 commit comments

Comments
 (0)