Skip to content

Commit

Permalink
Support UUID subclasses in convert
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Nov 22, 2023
1 parent f5c4549 commit ae9a77e
Show file tree
Hide file tree
Showing 2 changed files with 9 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 @@ -20608,7 +20608,7 @@ convert(
else if (pytype == PyDateTimeAPI->DeltaType) {
return convert_immutable(self, MS_TYPE_TIMEDELTA, "duration", obj, type, path);
}
else if (pytype == (PyTypeObject *)self->mod->UUIDType) {
else if (PyType_IsSubtype(pytype, (PyTypeObject *)(self->mod->UUIDType))) {
return convert_immutable(self, MS_TYPE_UUID, "uuid", obj, type, path);
}
else if (pytype == (PyTypeObject *)self->mod->DecimalType) {
Expand Down
8 changes: 8 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,14 @@ def test_uuid_disabled(self):
with pytest.raises(ValidationError, match="Expected `uuid`, got `bytes`"):
convert(typ(u.bytes), uuid.UUID, builtin_types=(uuid.UUID,))

def test_convert_uuid_subclass(self):
class UUID2(uuid.UUID):
...

u1 = uuid.uuid4()
u2 = UUID2(str(u1))
assert convert(u2, uuid.UUID) is u2


class TestDecimal:
def test_decimal_wrong_type(self):
Expand Down

0 comments on commit ae9a77e

Please sign in to comment.