-
Notifications
You must be signed in to change notification settings - Fork 268
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
Numerically constrainted fields don't generate proper error on non-Number fields #1338
Comments
You're absolutely correct in identifying |
I've had a look at the code, but I'm afraid my Rust is not up to snuff to put together a proper PR 😅. |
I can only reproduce this with class CustomComarable:
value: int
def __init__(self, value):
self.value = value
def __gt__(self, other):
raise TypeError("CustomComarable does not support comparison")
v = TypeAdapter(Annotated[CustomComarable, Field(gt="2")], config={"arbitrary_types_allowed": True})
v.validate_python(CustomComarable(1)) I think what @AnthonyVH is seeing is a type error being raised by # pydantic/_internal/_validators.py:272
def greater_than_validator(x: Any, gt: Any) -> Any:
try:
if not (x > gt):
raise PydanticKnownError('greater_than', {'gt': _safe_repr(gt)})
return x
except TypeError:
raise TypeError(f"Unable to apply constraint 'gt' to supplied value {x}") It kinda makes sense that TypeAdapter(Annotated[str, Field(gt=5*astropy.units.meter)])
# PydanticSerializationError: Unable to serialize unknown type: <class 'astropy.units.quantity.Quantity'>` so I think @AnthonyVH's model might be using something like All that said I think
Would address the idea from @davidhewitt? But I can't figure out a test case for it :( I guess there's the question of whether the schema should allow you to supply constraints which are not valid for a given type? I think it would be very difficult to implement and expensive to check when generating schema. |
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, aTypeError
exception is thrown during the generation of thePydanticKnownError
.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 aNumber
(well, forGreaterThan
it does). When that isn't the case, it triggers line 56 insrc/errors/types.rs
, causing theTypeError
.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 example5 * astropy.units.meter
.It's not clear to me why
pydantic-core
requires this object to be aNumber
. 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 aastropy.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 theTypeError
is raised.The text was updated successfully, but these errors were encountered: