-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
to_dict_msgpack does not work with inheritance #87
Comments
Oops! I did it again. I'd better be more careful with Thank you for this little investigation. I will fix it soon and release a hot fix. |
@MichelleArk can you check your code with this branch? If it's ok, I will release 3.1.1 then. |
Unfortunately the changes are causing a new error. I believe the second part of the new predicate in this branch (comparing method names) is overly restrictive and not adding pack/unpack methods with longer inheritance chains. Here's another example that breaks with the changes in the branch: from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.mixins.msgpack import DataClassMessagePackMixin
@dataclass
class Entity1(DataClassDictMixin):
field1: str
@dataclass
class Entity2(Entity1):
field2: str
@dataclass
class Entity1WrapperDict(DataClassDictMixin):
entity: Entity1
@dataclass
class Entity2WrapperMessagePack(DataClassMessagePackMixin):
entity: Entity2
@dataclass
class EntityWrapperMessagePack(DataClassMessagePackMixin):
entity1wrapper: Entity1WrapperDict
entity2wrapper: Entity2WrapperMessagePack
entity1w = Entity1WrapperDict(Entity1(field1='foo'))
entity2w = Entity2WrapperMessagePack(Entity2(field1='foo', field2='bar'))
wrapper = EntityWrapperMessagePack(entity1w, entity2w)
print(wrapper.to_msgpack()) Output: AttributeError: 'Entity1' object has no attribute 'to_dict_msgpack' Removing the method name comparison check from the branch resolves the issue, and also passes all unit and integration tests in dbt-core locally. Is there a scenario that check is specifically looking to avoid? |
Yes, just checking for the method to exist wasn't enough in a case with self reference. Here you would get RecursionError: from __future__ import annotations
@dataclass
class EntityWrapperMessagePack(DataClassMessagePackMixin):
entity1wrapper: Entity1WrapperDict
entity2wrapper: Entity2WrapperMessagePack
self: Optional[EntityWrapperMessagePack] So, we have two possible scenarios to check: A) We should avoid building I added additional check |
Nice! Thank you for that breakdown and explanation. I've confirmed the newest changes in the branch handle all cases in dbt-core and would unblock our update path 👍 |
Done! I've published a new version 3.1.1. |
Description
Base classes prevent correct subclass compilation of
to_dict_msgpack
packer/unpacker methods because of order of execution in addingto_dict_msgpack
methods toDataClassDictMixin
classes that are used inDataClassMessagePackMixin
s.Similar issue: #78
What I Did
Possible Solution
In mashamuro/core/meta/builder.py - CodeBuilder checks for the existence of a packer method using
hasattr(origin_type, method_name)
, which also searches ancestor classes. Instead of usinghasattr
, the builder could useorigin_type.__dict__
to check for the existence of a packer method defined by theorigin_type
class itself. Same solution would apply to unpacker methods.A much less ideal workaround to this issue demonstrated here in dbt-core's upgrade for bumping mashumaro[msgpack] to 3.1.
The text was updated successfully, but these errors were encountered: