Skip to content

Commit

Permalink
Merge pull request #197 from nucleic/atomdict-setdefault
Browse files Browse the repository at this point in the history
atomdict: return the coerced item from setdefault
  • Loading branch information
MatthieuDartiailh authored Mar 13, 2023
2 parents 7628710 + 9ec68a2 commit 1a13020
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion atom/src/atomdict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}


Expand Down
1 change: 1 addition & 0 deletions releasenotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
12 changes: 11 additions & 1 deletion tests/test_atomdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 1a13020

Please sign in to comment.