Skip to content
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

Handle equality exceptions from exclude_default as False when serializing #10547

Open
4 of 13 tasks
sneakers-the-rat opened this issue Oct 3, 2024 · 0 comments · May be fixed by pydantic/pydantic-core#1490
Open
4 of 13 tasks

Comments

@sneakers-the-rat
Copy link

sneakers-the-rat commented Oct 3, 2024

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

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:

class MyModel(BaseModel):
    array: Optional[NDArray] = None

if an array is provided, model_dump<_json>(exclude_defaults=True) will fail because bool(None == np.array(data)) raises the ambiguous bool ValueError.

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 return False 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant