Skip to content

Commit

Permalink
Support fromYAML classmethod for all Munch subclasses (fixes #34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayala Shachar committed Oct 29, 2019
1 parent dede29c commit cc2edf7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
Next Version
------------

* Support ``fromYAML`` classmethod for all Munch subclasses (PR [#52](https://github.com/Infinidat/munch/pull/52) fixes [#34](https://github.com/Infinidat/munch/issues/34)

2.4.0 (2019-10-29)
------------------

Expand Down
9 changes: 5 additions & 4 deletions munch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,13 @@ def toYAML(self, **options):
else:
return yaml.dump(self, **opts)

def fromYAML(*args, **kwargs):
kwargs.setdefault('Loader', yaml.FullLoader)
return munchify(yaml.load(*args, **kwargs))
def fromYAML(cls, stream, *args, **kwargs):
factory = lambda d: cls(*(args + (d,)), **kwargs)
loader_class = kwargs.pop('Loader', yaml.FullLoader)
return munchify(yaml.load(stream, Loader=loader_class), factory=factory)

Munch.toYAML = toYAML
Munch.fromYAML = staticmethod(fromYAML)
Munch.fromYAML = classmethod(fromYAML)

except ImportError:
pass
17 changes: 16 additions & 1 deletion tests/test_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from munch import Munch
from munch import Munch, DefaultMunch


def test_from_yaml(yaml):
Expand Down Expand Up @@ -39,7 +39,22 @@ def test_toYAML(yaml):

@pytest.mark.usefixtures('yaml')
def test_fromYAML():
# pylint: disable=unidiomatic-typecheck
yaml_str = 'foo:\n bar:\n - 1\n - 2\n hello: world\n'
obj = Munch.fromYAML(yaml_str)
assert type(obj) == Munch
assert obj == Munch(foo=Munch(bar=[1, 2], hello='world'))
assert obj.toYAML() == yaml_str


@pytest.mark.usefixtures('yaml')
def test_fromYAML_default_munch():
# pylint: disable=unidiomatic-typecheck
yaml_str = 'foo:\n bar:\n - 1\n - 2\n hello: world\n'
default_value = object()
obj = DefaultMunch.fromYAML(yaml_str, default_value)
assert type(obj) == DefaultMunch
assert obj == DefaultMunch(foo=Munch(bar=[1, 2], hello='world'))
assert obj['not_exist'] is default_value
assert obj.not_exist is default_value
assert obj.toYAML() == yaml_str

0 comments on commit cc2edf7

Please sign in to comment.