diff --git a/CHANGELOG.md b/CHANGELOG.md index 424148a..8823bcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------ diff --git a/munch/__init__.py b/munch/__init__.py index 83420a4..384c504 100644 --- a/munch/__init__.py +++ b/munch/__init__.py @@ -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 diff --git a/tests/test_yaml.py b/tests/test_yaml.py index 92054a8..6d15261 100644 --- a/tests/test_yaml.py +++ b/tests/test_yaml.py @@ -1,5 +1,5 @@ import pytest -from munch import Munch +from munch import Munch, DefaultMunch def test_from_yaml(yaml): @@ -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