8000 ConstrainedFloat schema: differences between IEEE floats and json (#1… · ag-python/pydantic@833d33d · GitHub
[go: up one dir, main page]

Skip to content

Commit 833d33d

Browse files
ConstrainedFloat schema: differences between IEEE floats and json (pydantic#1422)
* ConstrainedFloat schema: account for differences between IEEE floats and json * add changes * Update changes/1417-vdwees.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update changes/1417-vdwees.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * fixup * Update pydantic/types.py Difference of styles :) Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * merge _schema_ieee_compatibility_transform into parent method * capitalize * use type_, not outer_type_ Co-authored-by: Jesse VanderWees <jesse.vanderwees@kisters-bv.nl> Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
1 parent 9b2310a commit 833d33d

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

changes/1417-vdwees.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Modify schema constraints on `ConstrainedFloat` so that `exclusiveMinimum` and
2+
minimum are not included in the schema if they are equal to `-math.inf` and
3+
`exclusiveMaximum` and `maximum` are not included if they are equal to `math.inf`.

pydantic/schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ def get_field_schema_validations(field: ModelField) -> Dict[str, Any]:
234234
f_schema['const'] = field.default
235235
if field.field_info.extra:
236236
f_schema.update(field.field_info.extra)
237+
modify_schema = getattr(field.type_, '__modify_schema__', None)
238+
if modify_schema:
239+
modify_schema(f_schema)
237240
return f_schema
238241

239242

pydantic/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import re
23
import warnings
34
from decimal import Decimal
@@ -339,6 +340,15 @@ def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
339340
maximum=cls.le,
340341
multipleOf=cls.multiple_of,
341342
)
343+
# Modify constraints to account for differences between IEEE floats and JSON
344+
if field_schema.get('exclusiveMinimum') == -math.inf:
345+
del field_schema['exclusiveMinimum']
346+
if field_schema.get('minimum') == -math.inf:
347+
del field_schema['minimum']
348+
if field_schema.get('exclusiveMaximum') == math.inf:
349+
del field_schema['exclusiveMaximum']
350+
if field_schema.get('maximum') == math.inf:
351+
del field_schema['maximum']
342352

343353
@classmethod
344354
def __get_validators__(cls) -> 'CallableGenerator':

tests/test_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import os
23
import sys
34
import tempfile
@@ -1119,6 +1120,10 @@ class UserModel(BaseModel):
11191120
({'lt': 5}, float, {'type': 'number', 'exclusiveMaximum': 5}),
11201121
({'ge': 2}, float, {'type': 'number', 'minimum': 2}),
11211122
({'le': 5}, float, {'type': 'number', 'maximum': 5}),
1123+
({'gt': -math.inf}, float, {'type': 'number'}),
1124+
({'lt': math.inf}, float, {'type': 'number'}),
1125+
({'ge': -math.inf}, float, {'type': 'number'}),
1126+
({'le': math.inf}, float, {'type': 'number'}),
11221127
({'multiple_of': 5}, float, {'type': 'number', 'multipleOf': 5}),
11231128
({'gt': 2}, Decimal, {'type': 'number', 'exclusiveMinimum': 2}),
11241129
({'lt': 5}, Decimal, {'type': 'number', 'exclusiveMaximum': 5}),

0 commit comments

Comments
 (0)
0