Skip to content

Commit 6ff8890

Browse files
committed
Add Dictionary.update method
1 parent 535c764 commit 6ff8890

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pythonscript/embedded/godot/dictionary.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def copy(self):
8080
lib.godot_dictionary_new_copy(gd_ptr, self._gd_ptr)
8181
return Dictionary.build_from_gdobj(gd_ptr, steal=True)
8282

83+
def update(self, items):
84+
if not isinstance(items, (Dictionary, dict)):
85+
raise TypeError("Param `items` should be of type `dict` or `Dictionary`")
86+
for k, v in items.items():
87+
self[k] = v
88+
8389
def pop(self, *args):
8490
key, *default = args
8591
try:

tests/bindings/test_dictionary.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def test_delitem(self):
102102
with pytest.raises(TypeError):
103103
del v[object()]
104104

105+
def test_update(self):
106+
v = Dictionary({'a': 1, 'b': 2, 'c': 3})
107+
v.update({'a': 'one', 'd': 'four'})
108+
v.update(Dictionary({'b': 'two', 'e': 'five'}))
109+
assert set(v.keys()) == {'a', 'b', 'c', 'd', 'e'}
110+
assert set(v.values()) == {'one', 'two', 3, 'four', 'five'}
111+
105112
def test_contains(self):
106113
v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
107114
assert "a" in v

0 commit comments

Comments
 (0)