-
Notifications
You must be signed in to change notification settings - Fork 56
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Literal
type
#65
Comments
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. 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 |
Simply: I have existing types that use 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! |
Cool, yeah, I can definitely add support for this. Adding it to the queue, thanks for the suggestion! |
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:
|
Already trying it :) Thanks for devoting time to this, @jcrist! |
This has now been released. Up on pypi now, should be on conda-forge shortly. |
馃憢 what are your thoughts on supporting the
Literal
type?The text was updated successfully, but these errors were encountered: