Skip to content

Commit

Permalink
port fix from cog (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgolsson authored Mar 10, 2023
1 parent 2e35b54 commit 552e84a
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 232 deletions.
4 changes: 4 additions & 0 deletions src/py/empyc/empyc/dataclasses/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
python_sources()

python_tests(
name="tests",
)
5 changes: 3 additions & 2 deletions src/py/empyc/empyc/dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import copy
import dataclasses
import enum
import json
Expand Down Expand Up @@ -111,9 +112,9 @@ def recursive_from_dict(
:param data: the mapping to use as constructor arguments
:skip_unknown: if ``True``, will prune invalid keys
:returns: an instantiated dataclass object
:raises TypeError: If the instantiation was unsuccesful
:raises TypeError: If the instantiation was unsuccessful
"""
return _recursive_instantiate(cls, data, skip_unknown)
return _recursive_instantiate(cls, copy.deepcopy(data), skip_unknown)


def _handle_tuple(field, obj, key, value):
Expand Down
76 changes: 76 additions & 0 deletions src/py/empyc/empyc/dataclasses/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import copy
import json
import string
from dataclasses import dataclass
from typing import Dict, List

import pytest

import empyc.dataclasses


def compare_no_ws(a, b):
remove = {ord(c): None for c in string.punctuation + string.whitespace}

assert a.translate(remove) == b.translate(remove)


@dataclass
class Inner:
list_data: List[int]


@dataclass
class Outer:
inner_data: Inner
something_else: Dict[str, str]


@pytest.fixture
def outer() -> Outer:
return Outer(inner_data=Inner(list_data=[1, 2, 3]), something_else={"hello": "world"})


@pytest.fixture
def outer_dict() -> Outer:
return {
"inner_data": {"list_data": [1, 2, 3]},
"something_else": {"hello": "world"},
}


@pytest.fixture
def outer_json() -> Outer:
return """{
"inner_data": {"list_data": [1,2,3]},
"something_else": {"hello": "world"}
}"""


def test_outer_to_json(outer, outer_json):
compare_no_ws(empyc.dataclasses.to_json(outer), outer_json)


def test_from_dict_no_modify(outer_dict):
indata = copy.deepcopy(outer_dict)
_ = empyc.dataclasses.recursive_from_dict(Outer, indata)

assert indata == outer_dict


def test_outer_from_dict(outer, outer_dict):
other = empyc.dataclasses.recursive_from_dict(Outer, outer_dict)
assert other == outer


def test_outer_from_json(outer, outer_json):
other = empyc.dataclasses.recursive_from_dict(Outer, json.loads(outer_json))
assert other == outer


def test_outer_roundtrip(outer):
other = empyc.dataclasses.recursive_from_dict(
Outer, json.loads(empyc.dataclasses.to_json(outer))
)

assert other == outer
1 change: 0 additions & 1 deletion src/py/empyc/empyc/tests/BUILD

This file was deleted.

56 changes: 0 additions & 56 deletions src/py/empyc/empyc/tests/test_functools.py

This file was deleted.

70 changes: 0 additions & 70 deletions src/py/empyc/empyc/tests/test_itertools.py

This file was deleted.

43 changes: 0 additions & 43 deletions src/py/empyc/empyc/tests/test_random.py

This file was deleted.

60 changes: 0 additions & 60 deletions src/py/empyc/empyc/tests/test_ref.py

This file was deleted.

0 comments on commit 552e84a

Please sign in to comment.