-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Allow propagation of class based discriminator settings to subclasses #196
Comments
Hi @Sanavesa
Since you mentioned abstract classes in the context of inclusion, I assume you expect a concrete subclass to be used to deserialize any of its subclasses? What would you be expecting in the following case? An error or a from dataclasses import dataclass
from typing import Literal
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.types import Discriminator
@dataclass
class Base(DataClassDictMixin):
class Config(BaseConfig):
discriminator = Discriminator(field="type", include_subtypes=True)
@dataclass
class Foo(Base):
type: Literal["foo"] = "foo"
@dataclass
class Bar(Foo):
type: Literal["bar"] = "bar"
obj = Foo.from_dict({"type": "bar"}) # what to expect here? |
In the code example provided, I noticed that Regarding class hierarchy and their subtype variants, each class seems to have its own lookup map (
The expected lookup maps would be:
In the What are your thoughts on this approach? Thank you for considering my feedback. |
Sorry for being silent for a long time and thank you for the detailed answer. I like your proposal, however, its implementation may require heavy improvements.
Unfortunately, this is not true in the current implementation. When a dataclass has a discriminator in its config, a map
I see the following way to resolve this issue:
With an existing workaround, I think this issue will have a low priority. However, if you or someone else can handle it, PR welcome. |
Description
Issue Summary:
When serializing and deserializing a hierarchy of dataclasses involving abstract classes using mashumaro's
to_dict()
andfrom_dict()
methods, attempting to deserialize a dictionary back to an object through an abstract parent class results in aTypeError
due to the inability to instantiate the abstract class, even when the dictionary represents a concrete subclass.Expected Behavior:
Deserialization of a dictionary to an object should be successful through any class in the hierarchy (including abstract classes) as long as the dictionary represents a concrete subclass that implements all abstract methods. This is expected to work because of the specified discriminator field that indicates the concrete subclass type.
Actual Behavior:
Deserializing a dictionary to an object through an abstract class using the
from_dict()
method throws aTypeError
, indicating an attempt to instantiate an abstract class with unimplemented abstract methods.What I Did
Superclass
that inherits fromDataClassDictMixin
. This class includes aConfig
subclass with aDiscriminator
configured on fieldtype
and includes all subtypes.AbstractSubclass
inheriting fromSuperclass
with an abstract methodfoo()
.ConcreteSubclass
inheriting fromAbstractSubclass
, implementing thefoo()
method and setting the discriminator fieldtype
to"concrete_subclass"
.ConcreteSubclass
to a dictionary usingto_dict()
.from_dict()
on all classes in the hierarchy.Error Message:
Temporary Workaround:
Adding the same
Config
subclass with aDiscriminator
configuration to theAbstractSubclass
resolves the issue. This suggests a potential inconsistency in how discriminator configurations are inherited or recognized in the class hierarchy.The text was updated successfully, but these errors were encountered: