-
Notifications
You must be signed in to change notification settings - Fork 72
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
Unable to handle a self-referencing union of primitive types #722
Comments
Hmmm, I thought we raised a better error message in that case, but maybe not. Recursive basic types aren't currently expressable in msgspec's internal type system, and refactoring to support them would be a major bit of work. When I added support for python 3.12's For this specific case, the output of If your goal is to get systems like from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
type JsonTypes = None | str | float | bool | int | list["JsonTypes"] | dict[str, "JsonTypes"]
else:
JsonTypes = Any This should let static analysis tools still do their thing, while still working with msgspec. |
Thank you! Unfortunately, pydantic is as much a target as static analysis tools are; so as necessary as the workaround may be, it seems a bit unfortunate. Some context that my contrieved example dropped is that I don't actually require JSON (de)serialization in the use case that prompted this ticket: The issue at hand can be observed when A less-contrieved reproducer, then, might look like:
...triggering the stack trace, when run:
Of course, this may be something that's more in Litestar's world than msgspec's, and I'm glad to move back over to their support channels if it's appropriate to do so -- but a means to treat objects whose types can't be understood as completely opaque would, in this context, be an entirely satisfactory fix. |
Description
I'm trying to use msgspec in a (historically pydantic-based) codebase that uses a self-referencing type declaration to describe contents that can be successfully serialized as JSON. Unfortunately, msgspec fails when handling type definitions that include any values leveraging that type; a minimal reproducer follows:
...which yields:
The problem does not take place if we modify the definition to
type JsonTypes = None | str | float | bool | int | list[Any] | dict[str, Any]
-- but that would defeat the point.The text was updated successfully, but these errors were encountered: