msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Struct `__post_init__` is not called when converting to Struct with `from_attributes=True`

Open NCRonB opened this issue 1 year ago • 2 comments
trafficstars

Description

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.

from dataclasses import dataclass
import msgspec

@dataclass
class DataclassEgg:
	grade: str
	
	def __post_init__(self):
		self.grade = f"dataclass({self.grade})"

class StructEgg(msgspec.Struct):
	grade: str

	def __post_init__(self):
		self.grade = f"Struct({self.grade})"
>>> egg = {"grade": "A"}

>>> struct_egg = msgspec.convert(egg, StructEgg)
>>> struct_egg
StructEgg(grade='Struct(A)')

>>> dataclass_egg = msgspec.convert(egg, DataclassEgg)
>>> dataclass_egg
DataclassEgg(grade='dataclass(A)')

>>> msgspec.convert(struct_egg, DataclassEgg, from_attributes=True)  # DataclassEgg post init is called
DataclassEgg(grade='dataclass(Struct(A))')

>>> msgspec.convert(dataclass_egg, StructEgg, from_attributes=True)  # StructEgg post init is not called
StructEgg(grade='dataclass(A)')

The last one should be:

StructEgg(grade='Struct(dataclass(A))')

NCRonB avatar Apr 29 '24 09:04 NCRonB

@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-brightpick avatar Jun 13 '24 19:06 jankotuc-brightpick

@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.

NCRonB avatar Jun 14 '24 05:06 NCRonB