Support numeric constraints for decimal.Decimal#998
Closed
Siyet wants to merge 19 commits intojcrist:mainfrom
Closed
Support numeric constraints for decimal.Decimal#998Siyet wants to merge 19 commits intojcrist:mainfrom
Siyet wants to merge 19 commits intojcrist:mainfrom
Conversation
Add explicit -lm linkage for the _core C extension on non-Windows platforms. Fixes undefined symbol errors (nextafter, fmod, lround) on aarch64 Linux systems where libm is not implicitly linked. Closes #971
Derive constraint_kind from non-None union members so that Annotated[X | None, Meta(...)] constraints are validated correctly. Previously any constraint on an Optional[X] type raised TypeError because the union branch left kind as CK_OTHER. Closes #804
Cover API mapping, option equivalents, typed decoding, error handling, and features without direct equivalent. Closes #642
Add MS_TYPE_BOOLLITERAL_TRUE/FALSE flags and handle PyBool_Type before PyLong_Type in typenode_collect_literal. Update decode paths (JSON, msgpack, convert) to validate against specific bool literal values. Closes #859
Store the computed tuple of FieldInfo on the class as __struct_field_info__ to avoid expensive get_class_annotations() and FieldInfo recreation on every call. Generic aliases are not cached since type parameters affect annotations. Closes #963
PyPI distribution name changed to msgspec-arise, import name stays msgspec. Updated URLs, badges, docs, CI references to Siyet/msgspec-arise.
Rename package to msgspec-arise
Update doc links to fork domain
Fix missing libm linkage for _core extension on Linux
Support constraints on Optional (union with None) types
Add porting guide for users coming from orjson
Support Literal[True] and Literal[False] types
Cache structs.fields() result for non-generic structs
Extends gt, ge, lt, le, and multiple_of Meta constraints to work with decimal.Decimal types, enabling exact arithmetic comparisons and sub-integer precision constraints without floating point issues. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
a723b9a to
1d5fbb5
Compare
Collaborator
Author
|
Resolved in the community fork msgspec-arise: PR #14, released in v0.20.2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #683.
Previously, the numeric constraints
gt,ge,lt,le, andmultiple_ofinmsgspec.Metawere only valid forintandfloattypes. This PR extends support todecimal.Decimal.PR #692 by @cocorigon (the issue author) has been open since May 2024. The author has since explicitly declined to continue it and invited others to take it over. This PR is a fresh implementation with documentation and full test coverage.
Changes
_core.c: Added five newTypeNodeconstraint flags (MS_CONSTR_DECIMAL_GT/GE/LT/LE/MULTIPLE_OF) backed byPyObject*slots storingDecimalinstances. Constraint values are converted toDecimalat type-node build time (floats go through string conversion to avoid precision loss). A newms_check_decimal_constraints()function is called after everyDecimaldecode path (JSON string, int, uint64, float, msgpack, convert).__init__.pyi: UpdatedMeta.__init__parameter types and attribute annotations fromUnion[int, float, None]toUnion[int, float, Decimal, None].docs/constraints.rst: Updated the Numeric Constraints section to mentiondecimal.Decimal, added amultiple_ofusage example, and converted themultiple_ofwarning to a note - also adding thatDecimalis the recommended solution for sub-integer precision constraints.tests/unit/test_constraints.py: AddedTestDecimalConstraintscoveringgt/ge/lt/le(parametrized),multiple_of, combined constraints, andconvert().Notable design decisions
multiple_offorDecimaluses Python's%operator (PyNumber_Remainder), which gives exact arithmetic - addressing the precision limitation that motivated this issue.floatare converted toDecimalvia string representation to preserve precision (e.g.Meta(gt=0.1)becomesDecimal('0.1'), notDecimal('0.1000000000000000055...')).