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

ORM: raise when trying to pickle instance of Entity #5549

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aiida/orm/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from plumpy.base.utils import call_with_super_check, super_check

from aiida.common.exceptions import InvalidOperation
from aiida.common.lang import classproperty, type_check
from aiida.common.warnings import warn_deprecation
from aiida.manage import get_manager
Expand Down Expand Up @@ -217,6 +218,10 @@ def __init__(self, backend_entity: BackendEntityType) -> None:
self._backend_entity = backend_entity
call_with_super_check(self.initialize)

def __getstate__(self):
"""Prevent an ORM entity instance from being pickled."""
raise InvalidOperation('pickling of AiiDA ORM instances is not supported.')
Copy link
Member

Choose a reason for hiding this comment

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

Can you point the user to another method of serialization that they can use?


@super_check
def initialize(self) -> None:
"""Initialize instance attributes.
Expand Down
8 changes: 8 additions & 0 deletions tests/orm/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
###########################################################################
# pylint: disable=no-self-use
"""Test for general backend entities"""
import pickle

import pytest

from aiida import orm
from aiida.common.exceptions import InvalidOperation


@pytest.mark.usefixtures('aiida_profile_clean')
Expand All @@ -37,3 +40,8 @@ def test_collections_count(self):
number_of_users, \
'{} User(s) was/were found using Collections\' count() method, ' \
'but {} User(s) was/were found using QueryBuilder directly'.format(user_collection_count, number_of_users)

def test_pickle(self):
"""Pickling is not supported and should raise."""
with pytest.raises(InvalidOperation):
pickle.dumps(orm.Entity({}))