-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Open
Labels
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
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.model_dump()and.model_dump_json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
model_construct(), pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.