python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.58k stars 578 forks source link

Python NaN validates against all numeric limits, and shouldn't #1222

Closed slbayer closed 6 months ago

slbayer commented 6 months ago

In the most recent version of python-jsonschema (4.21.1) using Python 3.10.11 on macOS 13.6.4:

>>> import jsonschema
>>> jsonschema.validate(float("nan"), {"type": "number", "minimum": 0})
>>> float("nan") >= 0
False
>>> jsonschema.validate(float("nan"), {"type": "number", "exclusiveMinimum": 0})
>>> float("nan") > 0
False

And so on. NaN is certainly a number, but it is not greater than 0, or greater than or equal to 0, etc.

Julian commented 6 months ago

NaN isn't part of the JSON data model, and therefore not part of the JSON Schema specification, so any results you get from it are somewhat bound to be nonsensical.

Julian commented 6 months ago

(For what it's worth, to elaborate on your specific example in the context of Python behavior, while nan isn't greater than 0, it's not less than it either, as float("nan") < 0 is also false.)