Skip to content

Commit

Permalink
fix(polywrap-msgpack): enum serialization issue (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
Niraj-Kamdar authored Sep 6, 2023
1 parent 6a62193 commit 12d327a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/polywrap-msgpack/polywrap_msgpack/sanitize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
python values into msgpack compatible values."""
from __future__ import annotations

from enum import IntEnum
from typing import Any, Dict, List, Set, Tuple, cast

from .extensions.generic_map import GenericMap
Expand Down Expand Up @@ -42,6 +43,8 @@ def sanitize(value: Any) -> Any:
...
ValueError: GenericMap key must be string, got 1 of type <class 'int'>
"""
if isinstance(value, IntEnum):
return value.value
if isinstance(value, GenericMap):
dictionary: Dict[Any, Any] = cast(
GenericMap[Any, Any], value
Expand Down
1 change: 1 addition & 0 deletions packages/polywrap-msgpack/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ disable = [
"too-many-return-statements", # too picky about return statements
"protected-access", # Needed for internal use
"invalid-name", # too picky about names
"too-many-branches", # sanitize function has too many branches
]
ignore = [
"tests/"
Expand Down
4 changes: 3 additions & 1 deletion packages/polywrap-msgpack/tests/strategies/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .basic_strategies import *
from .class_strategies import *
from .class_strategies import *
from .enum_strategies import *
from .generic_map_strategies import *
14 changes: 14 additions & 0 deletions packages/polywrap-msgpack/tests/strategies/enum_strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from enum import IntEnum
from hypothesis import strategies as st


class TestEnum(IntEnum):
Test0 = 0
Test1 = 1
Test2 = 2
Test3 = 3


def enum_st() -> st.SearchStrategy[IntEnum]:
"""Define a strategy for generating valid `Enum`."""
return st.sampled_from(list(TestEnum))
7 changes: 7 additions & 0 deletions packages/polywrap-msgpack/tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
valid_generic_map_st,
)

from .strategies.enum_strategies import enum_st


@given(scalar_st())
def test_mirror_scalar(s: Any):
assert msgpack_decode(msgpack_encode(s)) == s


@given(enum_st())
def test_mirror_enum(s: Any):
assert msgpack_decode(msgpack_encode(s)) == s


@given(sequence_of_scalar_st())
def test_mirror_any_sequence_of_scalars(s: Sequence[Any]):
assert msgpack_decode(msgpack_encode(s)) == list(s)
Expand Down

0 comments on commit 12d327a

Please sign in to comment.