Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.5
pydantic compiled: True
python version: 3.7.5 (default, Nov 7 2019, 10:50:52) [GCC 8.3.0]
platform: Linux-4.4.0-18362-Microsoft-x86_64-with-Ubuntu-18.04-bionic
optional deps. installed: ['typing-extensions']
Example code:
import math
from pydantic import BaseModel, Field
class MyModel(BaseModel):
myfloat: float = Field(..., lt=math.inf)
print(MyModel.schema())
# {'title': 'MyModel', 'type': 'object', 'properties': {'myfloat': {'title': 'Myfloat', 'exclusiveMaximum': inf, 'type': 'number'}}, 'required': ['myfloat']}
The inf is not json-compliant:
{
"properties": {
"myfloat": {
"exclusiveMaximum": inf,
"title": "Myfloat",
"type": "number"
}
},
"required": [
"myfloat"
],
"title": "MyModel",
"type": "object"
}
However, json-compliant strings cannot include inf anyway, so the presence of 'exclusiveMaximum': inf (or conversely, 'exclusiveMinimum': -inf) in the schema seems to be a bug.
Note that the reason that I have lt=math.inf in the first place is because I am trying to limit myfloat to real-valued floats. Perhaps pydantic provides a better way to do this?
Happy to accept a pr to fix this. I agree it should be omitted.
Note that the reason that I have lt=math.inf in the first place is because I am trying to limit myfloat to real-valued floats. Perhaps pydantic provides a better way to do this?
You could probably achieve the same thing with a validator and avoid anything begging added to the schema, though it might be less elegant.
This issue is really the definition of an edge case haha. Happy to make any changes you suggest.
@samuelcolvin any reason not to use the json equivalent value Infinity ?
{ "valid_infinity" : Infinity }
very good point, we should.
As noted in the PR (https://github.com/samuelcolvin/pydantic/pull/1422#issuecomment-619053836), NaN, Infinity, and -Infinity are not valid JSON, but a lot of python json libs happily treat them as such. As json schema are used by languages outside of python, it would make sense to stick closely to the spec. For example, fastapi's OpenAPI GUI page would probably break if these values were allowed to be included in the openapi schema (typically openapi.json).
@chris-allnutt Where it gets really confusing is that Infinity _is_ defined in js... :)
I had just imagine the python-ideas thread where someone said "Infinity is already defined in JS, it's only a matter of time before it's valid in JSON, let's just support it and wait..."
Closed by #1422
Most helpful comment
I had just imagine the python-ideas thread where someone said "
Infinityis already defined in JS, it's only a matter of time before it's valid in JSON, let's just support it and wait..."