Conversation
|
It looks like the Git history needs to be fixed on this one. |
8c80626 to
f406983
Compare
|
@ofek done — force-pushed a clean rewrite of the branch:
Build job is still red on the link checker but that's the unrelated #1008 fix; nothing in this branch touches it. |
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. Constraint bounds passed as int or float are coerced to Decimal via their string representation to preserve precision. multiple_of uses Python's % operator for exact arithmetic. Closes #683
d2ce46f to
023b9e1
Compare
|
Rebased onto main now that #1004 has landed. #1004 took bits 36-37 for
No other constraint bits were moved. SLOT numbering and the |
provinzkraut
left a comment
There was a problem hiding this comment.
I think this should have a check that disallows Decimal for gt, ge, etc. constraints where the type is not also a Decimal
Coercing a Decimal bound to int or float silently drops precision, which defeats the point of specifying the bound as a Decimal in the first place. Only allow Decimal bounds when the annotated type is also Decimal, and raise a TypeError otherwise at TypeNode construction time.
|
@provinzkraut added the check: a Covered with a parametrized test, docs updated. |
Reopening #998 (auto-closed when fork was deleted).
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 has been open since May 2024. The author has since explicitly declined to continue it and invited others to take it over. This is a fresh implementation with documentation and full test coverage.
Changes
_core.c: Added five newTypeNodeconstraint flags backed byPyObject*slots storingDecimalinstances. A newms_check_decimal_constraints()function is called after everyDecimaldecode path.__init__.pyi: UpdatedMeta.__init__parameter types to includeDecimal.docs/constraints.rst: Updated Numeric Constraints section to mentiondecimal.Decimal.tests/unit/test_constraints.py: AddedTestDecimalConstraintscovering all constraint types.Notable design decisions
multiple_offorDecimaluses Python's%operator, which gives exact arithmetic.floatare converted toDecimalvia string representation to preserve precision.