msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Unexpected inheritance behavior

Open RoTorEx opened this issue 10 months ago • 0 comments

Description

Hi there,

Why doesn't it work as imagined, especially in the second stage?

from datetime import datetime

import msgspec


element = {
    "id_": 1,
    "name": "Google",
    "country": "Finland",
    "description": None,
    "created_at": datetime(2025, 1, 20, 12, 28, 55),
    "updated_at": datetime(2025, 1, 20, 18, 58, 42),
}


# Stage ONE
class _FooOne(msgspec.Struct):
    id_: int
    created_at: datetime
    updated_at: datetime


class _BarOne(msgspec.Struct):
    name: str
    country: str
    description: str | None


# class SchemaOne(msgspec.Struct, _FooOne, _BarOne):  # ! TypeError
#     pass


# Stage TWO
class _FooTwo:
    id_: int
    created_at: datetime
    updated_at: datetime


class _BarTwo:
    name: str
    country: str
    description: str | None


class SchemaTwo(_FooTwo, _BarTwo, msgspec.Struct):
    pass


# Stage THREE
class _FooThree(msgspec.Struct):
    id_: int
    created_at: datetime
    updated_at: datetime


class _BarThree(_FooThree):
    name: str
    country: str
    description: str | None


class SchemaThree(_BarThree):
    pass


print(msgspec.convert(element, type=SchemaTwo))  # ! Empty
print(msgspec.convert(element, type=SchemaThree))  # ! Ok

RoTorEx avatar Jan 21 '25 14:01 RoTorEx