pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

Fix `exclude_defaults` when value's `__eq__` method raises an exception.

Open sneakers-the-rat opened this issue 1 year ago • 5 comments

First opening this as a draft with just the failing test to demo that we fix it... edit: and now added the fix :) ready to review

Change Summary

Catch errors in comparing a value to the default value when either the default or the provided value has some __eq__ method that raises an error, treat those as "not equal to the default."

This is especially important for types like arrays which traditionally raise a ValueError like "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" when comparing equality - i.e. the exception in the __eq__ method is expected behavior, which breaks optional fields with a default None that can take an array.

Related issue number

Fix: https://github.com/pydantic/pydantic/issues/10547

Checklist

  • [x] Unit tests for the changes exist
  • [x] Documentation reflects the changes where applicable
  • [x] Pydantic tests pass with this pydantic-core (except for expected changes)
  • [x] My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @davidhewitt

sneakers-the-rat avatar Oct 22 '24 00:10 sneakers-the-rat