Comments (6)
Cool, yeah, I can definitely add support for this. Adding it to the queue, thanks for the suggestion!
from msgspec.
Already trying it :)
Thanks for devoting time to this, @jcrist!
from msgspec.
Hmmm, we could definitely make that work. Can you expand a bit on your use case here?
Note that if you're just trying to restrict a field to a smaller set of valid values, you can kind of do this with enums today. Enum
types are serialized as strings of their field names, and IntEnum
types are serialized as their integer values, so as far as decoders are concerned, these are roughly equivalent:
class Foo(msgspec.Struct):
x: Literal[1, 2, 3]
class MyEnum(enum.IntEnum):
a = 1
b = 2
c = 3
class Foo2(msgspec.Struct):
x: MyEnum
msgspec.json.decode(b'{"x": 3}', type=Foo) # Ok, returns 3 (not implemented currently)
msgspec.json.decode(b'{"x": 3}', type=Foo2) # Ok, returns MyEnum.c
msgspec.json.decode(b'{"x": 4}', type=Foo) # Errors, int not in set of valid literals (not implemented currently)
msgspec.json.decode(b'{"x": 4}', type=Foo2) # Errors, int not in set of valid enum values
from msgspec.
Simply: I have existing types that use Literal
:
PdChannel = Literal["1", "2"]
...
class ODReadings(Struct):
timestamp: str
od_raw: dict[PdChannel, ODReading]
msgspec.json.decode(..., type=ODReadings) # TypeError: Type 'typing.Literal['1', '2']' is not supported
I could use Enums, but I kinda like literals in this case!
from msgspec.
Thanks for the issue request, this has been fixed in #71. I have a few small fixups I hope to get in before releasing, but feel free to try this out from the main branch if you're interested:
pip install git+https://github.com/jcrist/msgspec
from msgspec.
This has now been released. Up on pypi now, should be on conda-forge shortly.
from msgspec.
Related Issues (20)
- Cannot introspect generic structs, possibly bug in python stdlib. HOT 1
- typing.Annotated not working as expected.
- Callbacks to `Encoder`/`Decoder` are not respected in `datetime` objects HOT 1
- Callbacks to `Encoder`/`Decoder` are not respected in `datetime` objects HOT 2
- Support setting `strict=False` on specific fields
- Support adding hooks for supported objects types HOT 2
- Hot to get field alias name from Struct? HOT 3
- Struct `__post_init__` is not called when converting to Struct with `from_attributes=True`
- can msgspec provide a faster enum implementation? HOT 1
- Coerce a `None` value to the default HOT 4
- ForwardRef is not supported
- License discrepancy HOT 1
- Support freezegun fake types
- Let json.schema take an enc_hook for handling default values of custom types
- Support encoding any Enum value type
- Json Encoder: dataclass classes are encoded to empty dict
- Tighten typing of msgspec.json.schema
- Support numeric constraints for Decimal values
- Support decoding empty strings as unset for all field types
- Does msgspec have a concept of schema mode?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgspec.