Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(polywrap-msgpack): enum serialization issue #264

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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