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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numeric constraints for Decimal values #683

Open
corentin-regent opened this issue May 8, 2024 · 0 comments · May be fixed by #692
Open

Support numeric constraints for Decimal values #683

corentin-regent opened this issue May 8, 2024 · 0 comments · May be fixed by #692

Comments

@corentin-regent
Copy link

Description

I am currently using msgspec for parsing financial (cryptocurrency) data from various exchanges. Precision in this context is crucial, so I use Decimal values quite a lot. Though for safety and documentation, I would also like to set constraints for these values, however msgspec currently does not support it.

Supporting numeric constraints for the Decimal type would add consistency with the int and float types:

>>> msgspec.json.decode('123.456', type=Annotated[float, Meta(gt=0)])
123.456
>>> msgspec.json.decode('123.456', type=Annotated[Decimal, Meta(gt=0)])
TypeError: Can only set `gt` on a numeric type - type `typing.Annotated[decimal.Decimal, msgspec.Meta(gt=0)]` is invalid

Additionally (may be a separate feature), supporting the multiple_of constraint with a Decimal argument (e.g. Meta(multiple_of=Decimal('0.1'))) would provide users with a workaround to the false negatives due to precision loss, from this issue.

@corentin-regent corentin-regent linked a pull request May 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant