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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Literal type #65

Closed
CamDavidsonPilon opened this issue Feb 10, 2022 · 6 comments 路 Fixed by #71
Closed

Support Literal type #65

CamDavidsonPilon opened this issue Feb 10, 2022 · 6 comments 路 Fixed by #71
Labels
enhancement New feature or request

Comments

@CamDavidsonPilon
Copy link
Contributor

CamDavidsonPilon commented Feb 10, 2022

馃憢 what are your thoughts on supporting the Literal type?

@jcrist
Copy link
Owner

jcrist commented Feb 10, 2022

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

@CamDavidsonPilon
Copy link
Contributor Author

CamDavidsonPilon commented Feb 10, 2022

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!

@jcrist
Copy link
Owner

jcrist commented Feb 10, 2022

Cool, yeah, I can definitely add support for this. Adding it to the queue, thanks for the suggestion!

@jcrist jcrist added the enhancement New feature or request label Feb 10, 2022
@jcrist
Copy link
Owner

jcrist commented Feb 22, 2022

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

@CamDavidsonPilon
Copy link
Contributor Author

Already trying it :)

Thanks for devoting time to this, @jcrist!

@jcrist
Copy link
Owner

jcrist commented Feb 23, 2022

This has now been released. Up on pypi now, should be on conda-forge shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants