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
In test case 1 I create an unstructure hook to manipulate how a timedelta is serialized. This works fine. In the second case I define a hook for ClassOne, because I don't want internal_id to appear in the result. This works also fine, but the hook for the time field now is being ignored. I know that I can do this when creating hook by using time = override( unstruct_hook = ...), but that's going to be tedious if I had 20 different time fields. But I lack some understanding when and how hooks are evaluated?
The text was updated successfully, but these errors were encountered:
The short answer: you should register the timedelta hook before creating the unstructure hook for ClassOne.
The long answer: to me, it seems like your mental model of the hook being generated for ClassOne would look (logically, and ignoring _cattrs_omit_if_default and overrides) like this:
So at the time you call make_dict_unstructure_fn( ClassOne, converter, ...), the hook for timedelta is still the default one, and this is what you see in the payload.
Why is this the default behavior? Because it's much, much faster to do the hook dispatch once and early rather than on every unstructure (even though the converter has a cache) and in practice controlling the order in which hooks are registered isn't too hard. I agree it can be confusing if you don't understand what's going on.
Description
Maybe this is not a bug, but expected behaviour, but at least I feel it's confusing. Consider the following code:
In test case 1 I create an unstructure hook to manipulate how a timedelta is serialized. This works fine. In the second case I define a hook for ClassOne, because I don't want internal_id to appear in the result. This works also fine, but the hook for the time field now is being ignored. I know that I can do this when creating hook by using
time = override( unstruct_hook = ...)
, but that's going to be tedious if I had 20 different time fields. But I lack some understanding when and how hooks are evaluated?The text was updated successfully, but these errors were encountered: