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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃憣 IMPROVE: Add dataclass serialisation to context #5833
馃憣 IMPROVE: Add dataclass serialisation to context #5833
Conversation
@@ -136,6 +158,8 @@ def represent_data(self, data): | |||
return represent_computer(self, data) | |||
if isinstance(data, orm.Group): | |||
return represent_group(self, data) | |||
if is_dataclass(data) and not inspect.isclass(data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of interest, why is the second clause necessary? What is an example of an object passing is_dataclass(obj) and inspect.isclass(obj)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In [15]: import inspect
In [16]: from dataclasses import asdict, dataclass, is_dataclass
In [17]: @dataclass
...: class DataClass:
...: """A dataclass for testing."""
...: my_value: int
...:
In [18]: is_dataclass(DataClass) and inspect.isclass(DataClass)
Out[18]: True
In [19]: asdict(DataClass)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-c90f518d2aad> in <module>
----> 1 asdict(DataClass)
~/Documents/GitHub/aiida_core_develop/.tox/py38-pre-commit/lib/python3.8/dataclasses.py in asdict(obj, dict_factory)
1070 """
1071 if not _is_dataclass_instance(obj):
-> 1072 raise TypeError("asdict() should be called on dataclass instances")
1073 return _asdict_inner(obj, dict_factory)
1074
TypeError: asdict() should be called on dataclass instances
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for some reason, _is_dataclass_instance
is private 馃し
Co-authored-by: Sebastiaan Huber <mail@sphuber.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good, thanks
No description provided.