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

'exclusiveMaximum': inf probably shouldn't appear in model schema #1417

Closed
vdwees opened this issue Apr 22, 2020 · 9 comments
Closed

'exclusiveMaximum': inf probably shouldn't appear in model schema #1417

vdwees opened this issue Apr 22, 2020 · 9 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@vdwees
Copy link
Contributor

vdwees commented Apr 22, 2020

Bug

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?

@vdwees vdwees added the bug V1 Bug related to Pydantic V1.X label Apr 22, 2020
@vdwees vdwees changed the title 'exclusiveMaximum': inf probably shouldn't appear in model schema 'exclusiveMaximum': inf probably shouldn't appear in model schema Apr 22, 2020
@samuelcolvin
Copy link
Member

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.

@vdwees
Copy link
Contributor Author

vdwees commented Apr 23, 2020

This issue is really the definition of an edge case haha. Happy to make any changes you suggest.

@chris-allnutt
Copy link

@samuelcolvin any reason not to use the json equivalent value Infinity ?

{ "valid_infinity" : Infinity }

@samuelcolvin
Copy link
Member

very good point, we should.

@vdwees
Copy link
Contributor Author

vdwees commented Apr 24, 2020

As noted in the PR (#1422 (comment)), 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).

@vdwees
Copy link
Contributor Author

vdwees commented Apr 24, 2020

@chris-allnutt Where it gets really confusing is that Infinity is defined in js... :)

@samuelcolvin
Copy link
Member

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..."

@vdwees
Copy link
Contributor Author

vdwees commented May 7, 2020

Closed by #1422

@vdwees vdwees closed this as completed May 7, 2020
@Sesota
Copy link

Sesota commented Aug 1, 2022

What about math.inf values in default fields? They are processed as Infinity in the JSON and it's not still a JSON valid expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

4 participants