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 types #692

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "jcrist/msgspec",
"image": "mcr.microsoft.com/devcontainers/python:3.12-bookworm",
"postCreateCommand": "scripts/install.sh",
"customizations": {
"vscode": {
"extensions": [
"charliermarsh.ruff",
"ms-python.python",
"ms-vscode.cpptools"
],
"settings": {
"C_Cpp.default.includePath": [
"/usr/local/include/**"
],
"C_Cpp.formatting": "disabled",
"python.testing.pytestArgs": [
"-v",
"tests/"
],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false
}
}
}
}
22 changes: 16 additions & 6 deletions docs/source/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The following constraints are supported:
Numeric Constraints
-------------------

These constraints are valid on `int` or `float` types:
These constraints are valid on `int`, `float`, or `decimal.Decimal` types:

- ``ge``: The value must be greater than or equal to ``ge``.
- ``gt``: The value must be greater than ``gt``.
Expand All @@ -88,23 +88,33 @@ These constraints are valid on `int` or `float` types:

>>> import msgspec

>>> from decimal import Decimal

>>> from typing import Annotated

>>> msgspec.json.decode(b'-1', type=Annotated[int, msgspec.Meta(ge=0)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `int` >= 0

.. warning::
>>> msgspec.json.decode(b'0.3', type=Annotated[Decimal, msgspec.Meta(multiple_of=Decimal('0.1'))])
Decimal('0.3')

.. note::

While ``multiple_of`` works on ``float`` types, we don't recommend
specifying *non-integral* ``multiple_of`` constraints, as they may be
erroneously marked as invalid due to floating point precision issues. For
example, annotating a ``float`` type with ``multiple_of=10`` is fine, but
``multiple_of=0.1`` may lead to issues. See `this GitHub issue
specifying *non-integral* ``multiple_of`` constraints on them,
as they may be erroneously marked as invalid due to floating point
precision issues. For example, annotating a ``float`` type with
``multiple_of=10`` is fine, but ``multiple_of=0.1`` may lead to issues.
See `this GitHub issue
<https://github.com/json-schema-org/json-schema-spec/issues/312>`_ for more
details.

To address this issue, ``msgspec`` supports specifying ``multiple_of``
constraints with `decimal.Decimal` types, that offer arbitrary precision
arithmetic.

String Constraints
------------------

Expand Down
21 changes: 11 additions & 10 deletions msgspec/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
from decimal import Decimal
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -115,11 +116,11 @@ class Meta:
def __init__(
self,
*,
gt: Union[int, float, None] = None,
ge: Union[int, float, None] = None,
lt: Union[int, float, None] = None,
le: Union[int, float, None] = None,
multiple_of: Union[int, float, None] = None,
gt: Union[int, float, Decimal, None] = None,
ge: Union[int, float, Decimal, None] = None,
lt: Union[int, float, Decimal, None] = None,
le: Union[int, float, Decimal, None] = None,
multiple_of: Union[int, float, Decimal, None] = None,
pattern: Union[str, None] = None,
min_length: Union[int, None] = None,
max_length: Union[int, None] = None,
Expand All @@ -130,11 +131,11 @@ class Meta:
extra_json_schema: Union[dict, None] = None,
extra: Union[dict, None] = None,
): ...
gt: Final[Union[int, float, None]]
ge: Final[Union[int, float, None]]
lt: Final[Union[int, float, None]]
le: Final[Union[int, float, None]]
multiple_of: Final[Union[int, float, None]]
gt: Final[Union[int, float, Decimal, None]]
ge: Final[Union[int, float, Decimal, None]]
lt: Final[Union[int, float, Decimal, None]]
le: Final[Union[int, float, Decimal, None]]
multiple_of: Final[Union[int, float, Decimal, None]]
pattern: Final[Union[str, None]]
min_length: Final[Union[int, None]]
max_length: Final[Union[int, None]]
Expand Down
Loading