Skip to content

Commit

Permalink
Merge pull request Infinidat#54 from Infinidat/ayalas/issue-53
Browse files Browse the repository at this point in the history
Fix DefaultMunch/DefaultFactoryMunch return value for get method (fixes Infinidat#53)
  • Loading branch information
maormarcus authored Oct 30, 2019
2 parents 17636ee + e6117e7 commit 48a722d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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)

Expand Down
5 changes: 2 additions & 3 deletions munch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,9 @@ def get(self, k, d=None):
"""
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):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import munch


@pytest.fixture(name='yaml')
Expand All @@ -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)
4 changes: 4 additions & 0 deletions tests/test_munch.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,7 @@ def __getitem__(self, k):
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"

0 comments on commit 48a722d

Please sign in to comment.