Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding a generic class hierarchy #781

Open
nrbnlulu opened this issue Dec 10, 2024 · 0 comments
Open

Decoding a generic class hierarchy #781

nrbnlulu opened this issue Dec 10, 2024 · 0 comments

Comments

@nrbnlulu
Copy link

Question

Hi, thanks for the great library!

Assuming this class hierarchy

import msgspec
from msgspec import Struct


class SystemsStatus[T: msgspec.Struct](Struct, tag=True): ...


class SystemsStatusInitial[T: msgspec.Struct](SystemsStatus[T]):
	nodes: list[T]


class SystemsRemoved[T: msgspec.Struct](SystemsStatus[T]):
	ids: list[int]


class SystemsUpdated[T: msgspec.Struct](SystemsStatus[T]):
	nodes: list[T]


def get_union[T: msgspec.Struct](t: type[T]) -> type[SystemsStatus[T]]:
	return SystemsRemoved[t] | SystemsStatusInitial[t] | SystemsUpdated[t]

DECODER = msgspec.json.Decoder(get_union(SomeDTO))

Is this the prefered way to create a decoder or I can do something like this

DECODER =  msgspec.json.Decoder(SystemsStatus[SomeDTO]) 

Which failed decoding one of the inherited classes data.

@nrbnlulu nrbnlulu changed the title Decoding a **generic** class hierarchy Decoding a *generic* class hierarchy Dec 10, 2024
@nrbnlulu nrbnlulu changed the title Decoding a *generic* class hierarchy Decoding a generic class hierarchy Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant