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)
- Inherit __init_subclass__ args to subclass HOT 1
- ormsgpack benchmark comparison HOT 1
- encode sort_keys argument HOT 16
- Allow more than one str-like type in unions HOT 5
- decode's strict=False does not cast floats to ints HOT 3
- Typed encoding HOT 1
- Validation on serialization HOT 5
- Make msgspec more aware of large data and other serialization protocols
- Recommendation for Efficiently Decoding BSON from PyMongo with msgspec
- leading underscores and `"camel"` rename strategy HOT 2
- Constraint check for typing.Optional field HOT 1
- Automatically support `functools.cached_property` without requiring setting `dict=True` HOT 2
- UnboundLocalError for `new_scope` HOT 3
- Sign release tags when making future releases? HOT 4
- How can I do data normalization on a frozen instance in `__post_init__`? HOT 1
- Cannot convert with `from_attributes` when using a rename convention HOT 3
- Cannot set `gc=False` on Generic structs? HOT 5
- Support types.MappingProxyType HOT 3
- Add either `init_omit_defaults` or `omit_none` HOT 5
- Consider making `DecodeError` and `ValidationError` inherit from `ValueError` HOT 1
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.