Description
It's possible to create for example a GreaterThan
constraint on a field which is not number, but still has the required comparison operators. This works fine, except that when the constraint is violated, a TypeError
exception is thrown during the generation of the PydanticKnownError
.
The cause of this is the call to field_from_context
when the constraint isn't met. This requires the object to compare against to be a Number
(well, for GreaterThan
it does). When that isn't the case, it triggers line 56 in src/errors/types.rs
, causing the TypeError
.
I'm running into this for a model I've made that validates astropy.units.Quantity
fields. In this case, the object to constrain against could be for example 5 * astropy.units.meter
.
It's not clear to me why pydantic-core
requires this object to be a Number
. It seems that it having the required comparison operator (to do the check) and being able to be converted into a string (to generate an exception message) should be enough. Both are the case for a astropy.units.Quantity
object.
Edit: I can't trigger this when the annotated field is a string (Annonated[str, Field(gt="b")]
) so maybe I'm misunderstanding the exact reason the TypeError
is raised.