You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for opening this issue. This is definitely in scope for msgspec, and is something I've been thinking about. Can you comment a bit more on your use case here?
If I understand correctly, at the moment the only way to create a generic structure is to use type():
@MaximZayats, can you comment more on your use case for generic structs? I'm starting to look into this, but having a motivating example would be useful.
I am currently working on fast python JSONRPC implementation with msgspec.
So I need to define base Request and Response types.
Currently I have something like this:
classRequest(Struct):
id: int|str|None=Nonemethod: strparams: Raw=Raw() # this field can be generic, but in my case `Raw` is more suitablejsonrpc: str="2.0"classSuccessfulResponse(Struct):
id: int|str|None=Noneresult: Any# <- this field can be genericjsonrpc: str="2.0"
And actually I found out, that generic structs in my case is not necessary :)
So... Maybe my examples is not really good :)
I am looking for the same functionality because I want to inherit a base class:
It is not the nicest example but the idea is like this:
class Base(msgspec.Struct, Generic[T]):
id: int
value: T
class Car(Base[str]):
extra_variable_car: int
class Bike(Base[int]):
extra_variable_bike: int
This means I can attach new fields in the subclass and use the ones from the parent class. But also assume there is always a value field but the type could change which can be checked by Mypy if I create a certain object instance.
Is this somewhere on the roadmap to be implemented?
Is this somewhere on the roadmap to be implemented?
This is definitely something that's in scope, and that I want to support. It's just not done yet. msgspec is a side hobby project for me, so features only get added when I have free time. I can't give you an estimate on when this will be resolved, but hopefully sometime in the next few months.
Reacted by Slava and Bart
Reacted by Maksim Zayats, Robert Craigie, Bart, Peter Schutt, Nils Plaschke and Stefan Hagen
My use case is the following.
I allow users to send messages between nodes and processes, for that I use a wrapper message to route the message to the correct process:
classBaseMessage(Struct, tag_field="msg_type", tag=str.lower):
passclassWrapperMessage(BaseMessage):
to: strmsg: T# <- this is generic
I cannot use Any, because then I cannot decode the message anymore, because the Decoder doesn't decode Any.
I could use Raw but then I need to pass the type in there as well and do the encoding/decoding myself.
I am relying heavily on type hinting to make sure only the appropriate messages are passed, so having a support for Generics out of the box would be nice.
In the end I just need to be able to pass an arbitrary message in a Struct and need to be able to decode to that in the end as well.
Last alternative for me would be to use msgspec in conjunction with cloudpickle for generic messages 🤔
need to encode some mqtt messages that share a common pattern, but a single field (let's call it "data") that holds the actual struct that varies between topics
api responses that have pagination info and the actual data (a list of specific structs) inside a common field
Adding the support for generics would be really awesome!
Support for generic structs has been merged in #386 🚀 🚀. I'm going to add generics support for dataclasses and attrs too before cutting a release (hopefully sometime in the first week of May).
Activity
jcrist commentedon Sep 28, 2022
Thanks for opening this issue. This is definitely in scope for
msgspec, and is something I've been thinking about. Can you comment a bit more on your use case here?That'd work, but we also expose
msgspec.defstruct(https://jcristharif.com/msgspec/api.html#msgspec.defstruct), which should be a bit nicer to use:[-][Enhancement] Support `Generic` structs[/-][+]Support `Generic` structs[/+]jcrist commentedon Oct 10, 2022
@MaximZayats, can you comment more on your use case for generic structs? I'm starting to look into this, but having a motivating example would be useful.
maksimzayats commentedon Oct 10, 2022
@jcrist, sure.
I am currently working on fast python
JSONRPCimplementation withmsgspec.So I need to define base
RequestandResponsetypes.Currently I have something like this:
And actually I found out, that generic structs in my case is not necessary :)
So... Maybe my examples is not really good :)
Btw, pydantic has some examples like mine:
https://pydantic-docs.helpmanual.io/usage/models/#generic-models
tijs2 commentedon Dec 7, 2022
I am looking for the same functionality because I want to inherit a base class:
It is not the nicest example but the idea is like this:
This means I can attach new fields in the subclass and use the ones from the parent class. But also assume there is always a value field but the type could change which can be checked by Mypy if I create a certain object instance.
Is this somewhere on the roadmap to be implemented?
jcrist commentedon Dec 7, 2022
This is definitely something that's in scope, and that I want to support. It's just not done yet.
msgspecis a side hobby project for me, so features only get added when I have free time. I can't give you an estimate on when this will be resolved, but hopefully sometime in the next few months.Chumper commentedon Dec 31, 2022
My use case is the following.
I allow users to send messages between nodes and processes, for that I use a wrapper message to route the message to the correct process:
I cannot use
Any, because then I cannot decode the message anymore, because the Decoder doesn't decodeAny.I could use
Rawbut then I need to pass the type in there as well and do the encoding/decoding myself.I am relying heavily on type hinting to make sure only the appropriate messages are passed, so having a support for Generics out of the box would be nice.
In the end I just need to be able to pass an arbitrary message in a Struct and need to be able to decode to that in the end as well.
Last alternative for me would be to use
msgspecin conjunction withcloudpicklefor generic messages 🤔luochen1990 commentedon Feb 13, 2023
Having same issue. I'm trying to define
MyListlike following:But error occurs:
So, what can I do to support this feature?
gwax commentedon Mar 23, 2023
I'll give another use case where Generic structs would be very useful.
Let's say that we have some sort of tagged structures returned by an API:
and the API uses a generic envelope for returning lists of tagged values such that it can be expressed as:
This works fine as is but loses a lot of type information and validation power when dealing with APIs that have a known return type:
With generics, especially if validated on decode, the above could be expressed as:
saving a lot of casting and making the return types much clearer on reading method signatures
sanzoghenzo commentedon Mar 27, 2023
Hi, just stumbled in this exact issue...
My 2, very similar, use cases:
Adding the support for generics would be really awesome!
jcrist commentedon Apr 23, 2023
Support for generic structs has been merged in #386 🚀 🚀. I'm going to add generics support for
dataclassesandattrstoo before cutting a release (hopefully sometime in the first week of May).If anyone is interested, I'd love it if someone could install from the main branch (https://jcristharif.com/msgspec/install.html#installing-from-github) and try things out. I believe our test suite should cover all the possible edge cases, but if there are bugs it'd be nice to catch 'em now before the release.
gwax commentedon Apr 24, 2023
@jcrist , I can confirm that generic structs are working correctly for me in the current
mainbranch. Thank you.