8000 Support `nan` in combination with limits · Issue #12561 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Support nan in combination with limits #12561

@keriksson-rosenqvist-oqc

Description

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Currently Field information can define limits, defaults, and settings for things like allowing inf and nan values. However when combining them there can be compatibility issues, especially after serialisation and de-serialisation.
While it makes sense that +inf should not be allowed with an upper limit, nan is often used to indicate that a value is not yet set. It would be useful if these allow_x values also applied when using limits. It may be needed to also split the allow_inf_nan into allow_inf and allow_nan.

class Grade:
    name: str
    score: float = Field(default=float('nan'), ge=0.0, le=10.0, allow_inf_nan=True)

jd = grade(name="John Doe")  # currently make jd = {name='John Doe', score=nan}
jd2 = Grade.model_validate(jd.model_dump())  # Currently fails validation with: Input should be less than or equal to 10 [type=less_than_equal, input_value=nan, input_type=float]

Affected Components

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0