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

Deep construction for maps #654

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def construct_yaml_pairs(self, node):
def construct_yaml_set(self, node):
data = set()
yield data
value = self.construct_mapping(node)
value = self.construct_mapping(node, deep=True)
data.update(value)

def construct_yaml_str(self, node):
Expand All @@ -410,7 +410,7 @@ def construct_yaml_seq(self, node):
def construct_yaml_map(self, node):
data = {}
yield data
value = self.construct_mapping(node)
value = self.construct_mapping(node, deep=True)
data.update(value)

def construct_yaml_object(self, node, cls):
Expand Down
42 changes: 42 additions & 0 deletions tests/lib/test_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from dataclasses import dataclass
from datetime import date
import yaml

def _simple_roundtrip(orig):
loaded = yaml.load(yaml.dump(orig, Dumper=yaml.CDumper, sort_keys=False), Loader=yaml.CLoader)
assert loaded == orig

def test_scalar_types(verbose=False):
_simple_roundtrip({i for i in range(10)})
_simple_roundtrip({str(i) for i in range(10)})
_simple_roundtrip({str(i): i for i in range(10)})

test_scalar_types.unittest = None

@dataclass(frozen=True)
class ModelFitkey:
broker: str
currency: str

@dataclass(frozen=True)
class SingleDayOutput:
day: date
path: str

def test_complex_types(verbose=False):
_simple_roundtrip({ModelFitkey('ib', 'euro'), ModelFitkey('ib', 'usd'), ModelFitkey('cme', 'cad')})
d = {
ModelFitkey('ib', 'euro'): [SingleDayOutput(date(2022, 1, 2), '/a/b/c/20220102/euro.bin'),
SingleDayOutput(date(2022, 1, 3), '/a/b/c/20220103/euro.bin')],
ModelFitkey('ib', 'usd'): [SingleDayOutput(date(2022, 1, 2), '/z/b/20220101/usd.bin'),
SingleDayOutput(date(2022, 1, 3), '/z/b/20220102/usd.bin'),
SingleDayOutput(date(2022, 2, 3), '/z/b/20220202/usd.bin')],
ModelFitkey('cme', 'cad'): []}
_simple_roundtrip(d)

test_complex_types.unittest = None


if __name__ == '__main__':
import test_appliance
test_appliance.run(globals())
1 change: 1 addition & 0 deletions tests/lib/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from test_input_output import *
from test_sort_keys import *
from test_multi_constructor import *
from test_maps import *

from test_schema import *

Expand Down