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

👌 IMPROVE: Add dataclass serialisation to context #5833

Merged
merged 3 commits into from
Dec 14, 2022

Conversation

chrisjsewell
Copy link
Member

No description provided.

@chrisjsewell chrisjsewell requested a review from sphuber December 13, 2022 13:48
tests/orm/utils/test_serialize.py Outdated Show resolved Hide resolved
@@ -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):
Copy link
Contributor

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)?

Copy link
Member Author

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

Copy link
Member Author

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>
@chrisjsewell chrisjsewell requested a review from sphuber December 13, 2022 23:31
Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, thanks

@chrisjsewell chrisjsewell merged commit 40857b6 into aiidateam:main Dec 14, 2022
@chrisjsewell chrisjsewell deleted the serialize-dataclass branch December 14, 2022 08:36
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

Successfully merging this pull request may close these issues.

2 participants