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

Allow to safe_dump with OrderedDict instances #5

Merged
merged 3 commits into from
Apr 17, 2018
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
1 change: 1 addition & 0 deletions oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def map_constructor(loader, node):

pyyaml.add_representer(dict, map_representer)
pyyaml.add_representer(OrderedDict, map_representer)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=pyyaml.dumper.SafeDumper)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, why are both of the above lines needed?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyyaml.add_representer works on the Dumper class obj passed into it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but if I remove line 20 everything still works for me. Do you have a test that fails when that line is removed?

Copy link
Owner Author

@wimglenn wimglenn Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? That's odd. For me, the existing test_dump and test_dump_all fail without line 20. Could you send me your pip freeze.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, "works for me" is not "running the oyaml tests" :-) All good, if you have tests that prove it's needed, that's weird, but okay :-)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I've released v0.3 to warehouse (the new PyPI).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git tags are a thing you know ;-)



if sys.version_info < (3, 7):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from distutils.core import setup
from setuptools import setup

setup(
name='oyaml',
version='0.2',
version='0.3',
description='Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering',
author='Wim Glenn',
author_email='hey@wimglenn.com',
Expand Down
46 changes: 40 additions & 6 deletions test_oyaml.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
import sys
from collections import OrderedDict
from types import GeneratorType

import pytest
from yaml.representer import RepresenterError

import oyaml as yaml


d = OrderedDict([('k1', 'v1'), ('k3', 'v3'), ('k2', 'v2')])
data = OrderedDict([('x', 1), ('z', 3), ('y', 2)])


def test_dump():
assert yaml.dump(d) == '{k1: v1, k3: v3, k2: v2}\n'
assert yaml.dump(data) == '{x: 1, z: 3, y: 2}\n'


def test_safe_dump():
assert yaml.safe_dump(data) == '{x: 1, z: 3, y: 2}\n'


def test_dump_all():
assert yaml.dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n'


def test_safe_dump_all():
assert yaml.safe_dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n'


def test_load():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
assert loaded == {'k1': 'v1', 'k3': 'v3', 'k2': 'v2'}
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert loaded == {'x': 1, 'z': 3, 'y': 2}


def test_load_all():
gen = yaml.load_all('{x: 1, z: 3, y: 2}\n--- {}\n')
assert isinstance(gen, GeneratorType)
ordered_data, empty_dict = gen
assert empty_dict == {}
assert ordered_data == data


@pytest.mark.skipif(sys.version_info >= (3,7), reason="requires python3.6-")
def test_loads_to_ordered_dict():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert isinstance(loaded, OrderedDict)


@pytest.mark.skipif(sys.version_info < (3,7), reason="requires python3.7+")
def test_loads_to_std_dict():
loaded = yaml.load('{k1: v1, k3: v3, k2: v2}')
loaded = yaml.load('{x: 1, z: 3, y: 2}')
assert not isinstance(loaded, OrderedDict)
assert isinstance(loaded, dict)


def test_subclass_dump():

class MyOrderedDict(OrderedDict):
pass

data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.dump(data)
with pytest.raises(RepresenterError) as cm:
yaml.safe_dump(data)
assert str(cm.value) == "cannot represent an object: MyOrderedDict([('x', 1), ('y', 2)])"


def test_anchors_and_references():
text = '''
defaults:
Expand Down