diff --git a/aiida/orm/entities.py b/aiida/orm/entities.py index f44cef976d..68d80efa0b 100644 --- a/aiida/orm/entities.py +++ b/aiida/orm/entities.py @@ -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 @@ -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.') + @super_check def initialize(self) -> None: """Initialize instance attributes. diff --git a/tests/orm/test_entities.py b/tests/orm/test_entities.py index 8a4ba067fc..bb290fc4da 100644 --- a/tests/orm/test_entities.py +++ b/tests/orm/test_entities.py @@ -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') @@ -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({}))