You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
I was very pleased to see that there was already an option to exclude_defaults when dumping models. I don't expect to get tight control over how defaults are compared, but I feel like one additional case would make it even better:
When using custom types (in particular numpy arrays with a pydantic-compatible wrapper), sometimes they either don't define __eq__ or have special conditions like numpy arrays with "the truth value of an array with more than one element is ambiguous."
Specifically take this example, assuming that the ndarray type works as expected otherwise:
I think that would be more intuitive than not (if two things can't be meaningfully compared, my assumption would be that they are not equal), and lower complexity than trying to handle all the possible ways things can be compared, or being able to pass a custom lambda like lambda x,y: np.array_equal(x, y) into the dump method.
Not a perfect fix, but a step in the right direction and not introducing any footguns i can think of. what ya think?
Initial Checks
Description
I was very pleased to see that there was already an option to
exclude_defaults
when dumping models. I don't expect to get tight control over how defaults are compared, but I feel like one additional case would make it even better:When using custom types (in particular numpy arrays with a pydantic-compatible wrapper), sometimes they either don't define
__eq__
or have special conditions like numpy arrays with "the truth value of an array with more than one element is ambiguous."Specifically take this example, assuming that the ndarray type works as expected otherwise:
if an array is provided,
model_dump<_json>(exclude_defaults=True)
will fail becausebool(None == np.array(data))
raises the ambiguous boolValueError
.What do you think about treating exceptions raised by
__eq__
when deciding whether something is equal to the defaults as being "not equal to the default"? I think that would just be one change here: https://github.com/pydantic/pydantic-core/blob/e3eff5cb8a6dae8914e3831b00c690d9dee4b740/src/serializers/fields.rs#L78-L80 to catch the exception and returnFalse
instead.I think that would be more intuitive than not (if two things can't be meaningfully compared, my assumption would be that they are not equal), and lower complexity than trying to handle all the possible ways things can be compared, or being able to pass a custom lambda like
lambda x,y: np.array_equal(x, y)
into the dump method.Not a perfect fix, but a step in the right direction and not introducing any footguns i can think of. what ya think?
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: