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
Now, when you just do converter.structure(True, int), cattrs will do coercion - it will try to turn True into an int by essentially doing int(True) for you.
The union passthrough strategy doesn't do coercion, but validation. This is by design - doing coercion here would introduce other design limitations which I want to avoid. In your case, it will essentially do: True.__class__ in {int, bool, float} and then return it. bool is in that set since there's logic in cattrs to include subclasses, and technically speaking, this is correct behavior (True is an int, from Python's perspective).
So I'm going to close the issue as WONTFIX since I'm not sure how to approach this in a general way.
That said, we can continue the conversation for your particular case if you like. If it's important to you to coerce True into an int here, you might do something like this:
When structuring a bool as an int, the behavior is to convert it to an int:
But when using
configure_union_passthrough
structuring to a union, it doesn't get converted and remains a bool:My expectation was that it would be converted to an int in both cases.
This is with cattrs 24.1.2.
The text was updated successfully, but these errors were encountered: