-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Deserialization doesn't work with dynamically created dataclasses #96
Comments
Hi @omaxx There are two problems here, but they are solvable. Fortunately for us. Firstly, a class created using AttributeError: module 'types' has no attribute 'User' To fix this you should pass the correct module name which is the module where your make_dataclass(..., namespace={"__module__": __name__, ...}) Secondly, to deserialize an object of the f'{__qualname__}.__annotations__["user"]' To summarize, your example needs to be modified as follows to make it work: @dataclass
class Device(DataClassDictMixin):
name: str
user: make_dataclass(
'User',
[
("name", str),
("pw", str),
],
namespace={
"__module__": __name__,
"__qualname__": f'{__qualname__}.__annotations__["user"]',
},
bases=(DataClassDictMixin,)
)
device = Device.from_dict({"name": "device", "user": {"name": "jon", "pw": "snow"}})
print(device)
# Device(name='device', user=Device.__annotations__["user"](name='jon', pw='snow'))
print(device.to_dict())
# {'name': 'device', 'user': {'name': 'jon', 'pw': 'snow'}} If it's not essential for you to create the User = make_dataclass(
'User',
[
("name", str),
("pw", str),
],
namespace={
"__module__": __name__,
},
bases=(DataClassDictMixin,)
)
@dataclass
class Device(DataClassDictMixin):
name: str
user: User
device = Device.from_dict({"name": "device", "user": {"name": "jon", "pw": "snow"}})
print(device)
# Device(name='device', user=User(name='jon', pw='snow'))
print(device.to_dict())
# {'name': 'device', 'user': {'name': 'jon', 'pw': 'snow'}} Well, after all this investigation, I would like to finally know what are you doing if you need to create dataclasses in such a non-standard way? :) |
Hi @Fatal1ty I only try to figure out how is it possible to define dataclass with hierarchical structure in the simplest way. |
What do you mean by hierarchical structure? Using |
Under "hierarchical structure" I mean case when dataclass has fields with type of another dataclass, and that dataclass also could have dataclass fields. Defining field dataclass type inside fields for me looks more readable.
vs
|
Ah, I see. What about this variant? @dataclass
class Device(DataClassDictMixin):
name: str
user: "User"
@dataclass
class User(DataClassDictMixin):
name: str
credentials: "Credentials"
@dataclass
class Credentials(DataClassDictMixin):
login: str
pw: str
data = {
"name": "device",
"user": {
"name": "jon",
"credentials": {"login": "john", "pw": "snow"}
}
}
device = Device.from_dict(data)
# Device(
# name='device',
# user=User(
# name='john',
# credentials=Device.User.Credentials(
# login='john',
# pw='snow'
# )
# )
# )
assert device.to_dict() == data |
Meanwhile, incorrect |
Thank you! This variant is better! |
Ok, good to hear that. If there is another problem, open an issue. I’m closing this one. |
This code works:
and this code doesn't:
Error:
The text was updated successfully, but these errors were encountered: