Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NCRonB opened this issue Apr 29, 2024 · 1 comment

Comments

@NCRonB
Copy link

NCRonB commented Apr 29, 2024

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))')
@jankotuc-photoneo
Copy link

affects me as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants