I have a very specific use case at hand and I would like to ask if there is a better supported way to achieve what I am trying to do.
What I am trying to achieve;
import typing
import msgspec
class Base(msgspec.Struct, tag=True):
pass
class A(Base):
pass
class C(Base):
other: Base # should be union of subclasses of Base
What works;
import typing
import msgspec
class Base(msgspec.Struct, tag=True):
pass
class A(Base):
pass
class C(Base):
other: "BaseSubType"
BaseSubType = typing.Union[tuple(Base.__subclasses__())]
msgspec.json.decode(
'{"type":"C","other":{"type":"C","other":{"type":"A"}}}', type=BaseSubType
)
But unfortunately, this has issues when there are multiple files defining the subclass of Base.
What would be probably ideal but does not work;
import typing
import msgspec
BaseSubType = typing.TypeVar("BaseSubType", bound=Base)
class Base(msgspec.Struct, tag=True):
pass
class A(Base):
pass
class C(Base, typing.Generic[BaseSubType]):
other: BaseSubType
msgspec.json.decode(
'{"type":"C","other":{"type":"C","other":{"type":"A"}}}', type=BaseSubType
)
# ValidationError: Invalid value 'C' - at `$.type`
Question
Hello!
I have a very specific use case at hand and I would like to ask if there is a better supported way to achieve what I am trying to do.
What I am trying to achieve;
What works;
But unfortunately, this has issues when there are multiple files defining the subclass of
Base
.What would be probably ideal but does not work;
Thank you in advance.