Repository: Infinidat/munch

Issue:
DefaultMunch getter now returns None instead of DefaultValue
If a DefaultMunch has a default value of None, munch.get(attr_name, default_value) will now return None instead of default_value

```
>>> from munch import DefaultMunch
>>> d = DefaultMunch(None, {'a': 1})
>>> d.get('a')
1
>>> d.get('b')
>>> d.get('b', 'test')
>>> d.get('b', 'test') is None
True
```

This is a devastating change in behavior from the previous version:
```
>>> from munch import DefaultMunch
>>> d = DefaultMunch(None, {'a': 1})
>>> d.get('a')
1
>>> d.get('b')
>>> d.get('b', 'test')
'test'
>>> d.get('b', 'test') is None
False
```

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 424148a..3f5a26d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,9 @@ Changelog
 
 Next Version
 ------------
+* Fix return value of DefaultMunch and DefaultFactoryMunch's get method (fixes [#53](https://github.com/Infinidat/munch/issues/53))
+
+* 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..98b92c7 100644
--- a/munch/__init__.py
+++ b/munch/__init__.py
@@ -238,10 +238,9 @@ class Munch(dict):
         """
         D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
         """
-        try:
-            return self[k]
-        except KeyError:
+        if k not in self:
             return d
+        return self[k]
 
     def setdefault(self, k, d=None):
         """
@@ -615,12 +614,13 @@ try:
         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


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 259071a..45d6b23 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,4 +1,5 @@
 import pytest
+import munch
 
 
 @pytest.fixture(name='yaml')
@@ -9,3 +10,12 @@ def yaml_module():
     except ImportError:
         pass
     pytest.skip("Module 'PyYAML' is required")
+
+
+@pytest.fixture(params=[munch.Munch, munch.AutoMunch, munch.DefaultMunch, munch.DefaultFactoryMunch])
+def munch_obj(request):
+    cls = request.param
+    args = tuple()
+    if cls == munch.DefaultFactoryMunch:
+        args = args + (lambda: None,)
+    return cls(*args, hello="world", number=5)
diff --git a/tests/test_munch.py b/tests/test_munch.py
index fc55165..9c645cc 100644
--- a/tests/test_munch.py
+++ b/tests/test_munch.py
@@ -533,3 +533,7 @@ def test_getitem_dunder_for_subclass():
     assert custom_munch.a == 42
     assert custom_munch.get('b') == 42
     assert custom_munch.copy() == Munch(a=42, b=42)
+
+
+def test_get_default_value(munch_obj):
+    assert munch_obj.get("fake_key", "default_value") == "default_value"
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