diff --git a/atom/src/atomdict.cpp b/atom/src/atomdict.cpp index 85c24718..05756d92 100644 --- a/atom/src/atomdict.cpp +++ b/atom/src/atomdict.cpp @@ -157,7 +157,8 @@ PyObject* AtomDict_setdefault( AtomDict* self, PyObject* args ) { return 0; } - return cppy::incref( dfv ); + // Get the dictionary from the dict itself in case it was corced. + return cppy::incref( PyDict_GetItem( pyobject_cast( self ), key ) ); } diff --git a/releasenotes.rst b/releasenotes.rst index 0295cb68..46fc974e 100644 --- a/releasenotes.rst +++ b/releasenotes.rst @@ -5,6 +5,7 @@ Atom Release Notes ------------------ - fix generation of Value member from union containing object/Any PR #198 +- fix setdefault method of atomdict to return the actually stored item PR #197 0.9.0 - 21/02/2023 ------------------ diff --git a/tests/test_atomdict.py b/tests/test_atomdict.py index 01f135fc..c049c761 100644 --- a/tests/test_atomdict.py +++ b/tests/test_atomdict.py @@ -11,7 +11,7 @@ """ import pytest -from atom.api import Atom, Dict, Int, atomdict +from atom.api import Atom, Dict, Int, List, atomdict, atomlist @pytest.fixture @@ -149,6 +149,16 @@ def test_setdefault(atom_dict): atom_dict.fullytyped.setdefault(2, "") +def test_setdefault_coercion(): + class A(Atom): + d = Dict(int, List(int)) + + a = A() + content = a.d.setdefault(1, []) + assert isinstance(content, atomlist) + assert content is a.d[1] + + def test_update(atom_dict): """Test update a dict.""" atom_dict.untyped.update({"": 1})