Skip to content

Commit 12d327a

Browse files
authored
fix(polywrap-msgpack): enum serialization issue (#264)
1 parent 6a62193 commit 12d327a

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/polywrap-msgpack/polywrap_msgpack/sanitize.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
python values into msgpack compatible values."""
33
from __future__ import annotations
44

5+
from enum import IntEnum
56
from typing import Any, Dict, List, Set, Tuple, cast
67

78
from .extensions.generic_map import GenericMap
@@ -42,6 +43,8 @@ def sanitize(value: Any) -> Any:
4243
...
4344
ValueError: GenericMap key must be string, got 1 of type <class 'int'>
4445
"""
46+
if isinstance(value, IntEnum):
47+
return value.value
4548
if isinstance(value, GenericMap):
4649
dictionary: Dict[Any, Any] = cast(
4750
GenericMap[Any, Any], value

packages/polywrap-msgpack/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ disable = [
4848
"too-many-return-statements", # too picky about return statements
4949
"protected-access", # Needed for internal use
5050
"invalid-name", # too picky about names
51+
"too-many-branches", # sanitize function has too many branches
5152
]
5253
ignore = [
5354
"tests/"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
from .basic_strategies import *
2-
from .class_strategies import *
2+
from .class_strategies import *
3+
from .enum_strategies import *
4+
from .generic_map_strategies import *
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from enum import IntEnum
2+
from hypothesis import strategies as st
3+
4+
5+
class TestEnum(IntEnum):
6+
Test0 = 0
7+
Test1 = 1
8+
Test2 = 2
9+
Test3 = 3
10+
11+
12+
def enum_st() -> st.SearchStrategy[IntEnum]:
13+
"""Define a strategy for generating valid `Enum`."""
14+
return st.sampled_from(list(TestEnum))

packages/polywrap-msgpack/tests/test_mirror.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424
valid_generic_map_st,
2525
)
2626

27+
from .strategies.enum_strategies import enum_st
28+
2729

2830
@given(scalar_st())
2931
def test_mirror_scalar(s: Any):
3032
assert msgpack_decode(msgpack_encode(s)) == s
3133

3234

35+
@given(enum_st())
36+
def test_mirror_enum(s: Any):
37+
assert msgpack_decode(msgpack_encode(s)) == s
38+
39+
3340
@given(sequence_of_scalar_st())
3441
def test_mirror_any_sequence_of_scalars(s: Sequence[Any]):
3542
assert msgpack_decode(msgpack_encode(s)) == list(s)

0 commit comments

Comments
 (0)