From 35c57b9eb63b42531111f27ac7cc76e129ccd14a Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Thu, 31 Aug 2023 19:19:00 +0200 Subject: [PATCH] Remove `with_dbenv` use in `aiida.orm` This forces the import of `aiida.cmdline` in `aiida.orm` which doesn't just slow down, but also is conceptually wrong. The problem of the `with_dbenv` decorator is also that it cannot be imported inside a method to avoid the import cost when importing `aiida.orm` but has to be imported at the top in order to be used. --- aiida/orm/utils/builders/code.py | 7 +++++-- aiida/orm/utils/builders/computer.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/aiida/orm/utils/builders/code.py b/aiida/orm/utils/builders/code.py index e94db906ae..0026146ded 100644 --- a/aiida/orm/utils/builders/code.py +++ b/aiida/orm/utils/builders/code.py @@ -11,7 +11,6 @@ import enum import pathlib -from aiida.cmdline.utils.decorators import with_dbenv from aiida.common.utils import ErrorAccumulator from aiida.common.warnings import warn_deprecation from aiida.orm import InstalledCode, PortableCode @@ -41,9 +40,13 @@ def validate(self, raise_error=True): self._err_acc.run(self.validate_installed) return self._err_acc.result(raise_error=self.CodeValidationError if raise_error else False) - @with_dbenv() def new(self): """Build and return a new code instance (not stored)""" + from aiida.manage import get_manager + + # Load the profile backend if not already the case. + get_manager().get_profile_storage() + self.validate() # Will be used at the end to check if all keys are known (those that are not None) diff --git a/aiida/orm/utils/builders/computer.py b/aiida/orm/utils/builders/computer.py index 3f9e9da6de..4918425498 100644 --- a/aiida/orm/utils/builders/computer.py +++ b/aiida/orm/utils/builders/computer.py @@ -8,7 +8,6 @@ # For further information please visit http://www.aiida.net # ########################################################################### """Manage computer objects with lazy loading of the db env""" -from aiida.cmdline.utils.decorators import with_dbenv from aiida.common.exceptions import ValidationError from aiida.common.utils import ErrorAccumulator @@ -62,11 +61,13 @@ def validate(self, raise_error=True): """Validate the computer options.""" return self._err_acc.result(raise_error=self.ComputerValidationError if raise_error else False) - @with_dbenv() def new(self): """Build and return a new computer instance (not stored)""" + from aiida.manage import get_manager from aiida.orm import Computer + # Load the profile backend if not already the case. + get_manager().get_profile_storage() self.validate() # Will be used at the end to check if all keys are known