Skip to content

Commit 6201ad5

Browse files
authored
Merge pull request #8 from dschenck/0.2.4
0.2.4
2 parents a34c15f + 6c734be commit 6201ad5

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

docs/contents/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Changelog
22
=====================================
33
The source code is hosted and maintained on `github <https://github.com/dschenck/easytree/>`_.
44

5+
Version 0.2.4 (2023-11-25)
6+
--------------------------
7+
- fixed a bug where :code:`dict.setdefault` would never set the default value
8+
59
Version 0.2.3 (2023-09-14)
610
--------------------------
711
- maintain backward compatability with Python 3.6 and 3.7

easytree/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# metadata
2+
__version__ = "0.2.4"
3+
14
from easytree.tree import (
25
Node,
36
serialize,

easytree/types.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,15 @@ def setdefault(self, key, default):
570570
AttributeError
571571
if the dict is frozen, or if the dict is sealed and the key does not exist in the dict
572572
"""
573-
try:
574-
return self[key]
575-
except KeyError:
573+
if key not in self:
576574
if self._frozen:
577575
raise AttributeError(f"Cannot set {key} on frozen easytree.dict")
578576
if self._sealed and key not in self:
579577
raise AttributeError(f"Cannot set {key} on sealed easytree.dict")
580-
return super().setdefault(
581-
key, cast(default, sealed=self._sealed, frozen=self._frozen)
582-
)
578+
return super().setdefault(
579+
key, cast(default, sealed=self._sealed, frozen=self._frozen)
580+
)
581+
return self[key]
583582

584583
def get(self, key, default=None):
585584
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="easytree",
8-
version="0.2.3",
8+
version="0.2.4",
99
author="David Schenck",
1010
author_email="david.schenck@outlook.com",
1111
description="A recursive dot-styled defaultdict to read and write deeply-nested trees",

tests/types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,26 @@ def test_undefined_race_conditions():
799799
b = x.y
800800

801801
assert x == {}
802+
803+
804+
def test_dict_setdefault():
805+
x = easytree.dict()
806+
x.a.b.setdefault("c", "this should be set")
807+
assert x == {"a": {"b": {"c": "this should be set"}}}
808+
809+
x.a.b.setdefault("c", "this should not be overriden")
810+
assert x == {"a": {"b": {"c": "this should be set"}}}
811+
812+
813+
def test_dict_setdefault_on_frozen():
814+
x = easytree.dict(frozen=True)
815+
816+
with pytest.raises(Exception):
817+
x.a.b.setdefault("c", "this should raise an error")
818+
819+
820+
def test_dict_setdefault_on_sealed():
821+
x = easytree.dict(sealed=True)
822+
823+
with pytest.raises(Exception):
824+
x.a.b.setdefault("c", "this should raise an error")

0 commit comments

Comments
 (0)