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
{{ message }}
Repository navigation
Struct __post_init__ is not called when converting to Struct with from_attributes=True #673
When using msgspec.convert() to convert to a Struct with from_attributes=True, the Struct's __post_init__ is not called. It works as expected with dataclasses.
@NCRonB I thought the same, but I found out the __post_init__() is indeed being called. However, if there are any new fields defined in it (as in your example), these are silently ignored. To fix it, you have to create your StructEgg with the configuration option dict=True.
If the __post_init__() only contains validations or similar, there's probably no need for the dict=True option.
@jankotuc-photoneo I'm not defining any new fields — only setting the one field grade to a different value. If you change the __post_init__() to simply print() something, you'll see that it doesn't print.
I tried what you suggested, and it still doesn't work. If you have an example where it does, please share.
Activity
jankotuc-brightpick commentedon Jun 13, 2024
@NCRonB I thought the same, but I found out the
__post_init__()is indeed being called. However, if there are any new fields defined in it (as in your example), these are silently ignored. To fix it, you have to create yourStructEggwith the configuration optiondict=True.If the
__post_init__()only contains validations or similar, there's probably no need for thedict=Trueoption.NCRonB commentedon Jun 14, 2024
@jankotuc-photoneo I'm not defining any new fields — only setting the one field
gradeto a different value. If you change the__post_init__()to simply print() something, you'll see that it doesn't print.I tried what you suggested, and it still doesn't work. If you have an example where it does, please share.