Skip to content

Commit

Permalink
Fix caching of dataclass subclass info
Browse files Browse the repository at this point in the history
Previously our caching mechanism would do the wrong thing when accessing
type info on a dataclass subclass when the base class info was already
cached. This fixes that so that decoding dataclass subclasses always
works properly.
  • Loading branch information
jcrist committed Dec 4, 2023
1 parent 2dde160 commit dea8056
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3706,7 +3706,7 @@ typenode_from_collect_state(TypeNodeCollectState *state) {

static bool
get_msgspec_cache(MsgspecState *mod, PyObject *obj, PyTypeObject *type, PyObject **out) {
PyObject *cached = PyObject_GetAttr(obj, mod->str___msgspec_cache__);
PyObject *cached = PyObject_GenericGetAttr(obj, mod->str___msgspec_cache__);
if (cached != NULL) {
if (Py_TYPE(cached) != type) {
Py_DECREF(cached);
Expand Down
14 changes: 14 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,20 @@ class Ex:
assert dec.decode(proto.encode(msg)) == msg
assert dec2.decode(proto.encode(msg)) == msg

def test_decode_dataclass_subclasses(self, proto):
@dataclass
class Base:
x: int

@dataclass
class Sub(Base):
y: int

msg = proto.encode({"x": 1, "y": 2})

assert proto.decode(msg, type=Base) == Base(1)
assert proto.decode(msg, type=Sub) == Sub(1, 2)

def test_multiple_dataclasses_errors(self, proto):
@dataclass
class Ex1:
Expand Down

0 comments on commit dea8056

Please sign in to comment.