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

♻️ REFACTOR: Remove Entity.from_backend_entity from the public API #5447

Merged
merged 1 commit into from
Dec 14, 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
4 changes: 2 additions & 2 deletions aiida/orm/authinfos.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def enabled(self, enabled: bool) -> None:
def computer(self) -> 'Computer':
"""Return the computer associated with this instance."""
from . import computers # pylint: disable=cyclic-import
return computers.Computer.from_backend_entity(self._backend_entity.computer)
return entities.from_backend_entity(computers.Computer, self._backend_entity.computer)

@property
def user(self) -> 'User':
"""Return the user associated with this instance."""
return users.User.from_backend_entity(self._backend_entity.user)
return entities.from_backend_entity(users.User, self._backend_entity.user)

def get_auth_params(self) -> Dict[str, Any]:
"""Return the dictionary of authentication parameters
Expand Down
2 changes: 1 addition & 1 deletion aiida/orm/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def node(self) -> 'Node':

@property
def user(self) -> 'User':
return users.User.from_backend_entity(self._backend_entity.user)
return entities.from_backend_entity(users.User, self._backend_entity.user)

def set_user(self, value: 'User') -> None:
self._backend_entity.user = value.backend_entity
Expand Down
2 changes: 1 addition & 1 deletion aiida/orm/computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def copy(self) -> 'Computer':
"""
Return a copy of the current object to work with, not stored yet.
"""
return Computer.from_backend_entity(self._backend_entity.copy())
return entities.from_backend_entity(Computer, self._backend_entity.copy())

def store(self) -> 'Computer':
"""
Expand Down
15 changes: 8 additions & 7 deletions aiida/orm/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from collections.abc import Iterator, Mapping, Sized
from functools import singledispatch

from aiida.orm.entities import from_backend_entity
from aiida.orm.implementation import (
BackendAuthInfo,
BackendComment,
Expand Down Expand Up @@ -72,44 +73,44 @@ def _(backend_entity):
def _(backend_entity):
from .groups import load_group_class
group_class = load_group_class(backend_entity.type_string)
return group_class.from_backend_entity(backend_entity)
return from_backend_entity(group_class, backend_entity)


@get_orm_entity.register(BackendComputer)
def _(backend_entity):
from . import computers
return computers.Computer.from_backend_entity(backend_entity)
return from_backend_entity(computers.Computer, backend_entity)


@get_orm_entity.register(BackendUser)
def _(backend_entity):
from . import users
return users.User.from_backend_entity(backend_entity)
return from_backend_entity(users.User, backend_entity)


@get_orm_entity.register(BackendAuthInfo)
def _(backend_entity):
from . import authinfos
return authinfos.AuthInfo.from_backend_entity(backend_entity)
return from_backend_entity(authinfos.AuthInfo, backend_entity)


@get_orm_entity.register(BackendLog)
def _(backend_entity):
from . import logs
return logs.Log.from_backend_entity(backend_entity)
return from_backend_entity(logs.Log, backend_entity)


@get_orm_entity.register(BackendComment)
def _(backend_entity):
from . import comments
return comments.Comment.from_backend_entity(backend_entity)
return from_backend_entity(comments.Comment, backend_entity)


@get_orm_entity.register(BackendNode)
def _(backend_entity):
from .utils.node import load_node_class # pylint: disable=import-error,no-name-in-module
node_class = load_node_class(backend_entity.node_type)
return node_class.from_backend_entity(backend_entity)
return from_backend_entity(node_class, backend_entity)


class ConvertIterator(Iterator, Sized):
Expand Down
32 changes: 16 additions & 16 deletions aiida/orm/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,6 @@ def get(cls, **kwargs):
)
return cls.collection.get(**kwargs) # pylint: disable=no-member

@classmethod
def from_backend_entity(cls: Type[EntityType], backend_entity: BackendEntityType) -> EntityType:
"""Construct an entity from a backend entity instance

:param backend_entity: the backend entity

:return: an AiiDA entity instance
"""
from .implementation.entities import BackendEntity

type_check(backend_entity, BackendEntity)
entity = cls.__new__(cls)
entity._backend_entity = backend_entity
call_with_super_check(entity.initialize)
return entity

def __init__(self, backend_entity: BackendEntityType) -> None:
"""
:param backend_entity: the backend model supporting this entity
Expand Down Expand Up @@ -271,3 +255,19 @@ def backend(self) -> 'StorageBackend':
def backend_entity(self) -> BackendEntityType:
"""Get the implementing class for this object"""
return self._backend_entity


def from_backend_entity(cls: Type[EntityType], backend_entity: BackendEntityType) -> EntityType:
"""Construct an entity from a backend entity instance

:param backend_entity: the backend entity

:return: an AiiDA entity instance
"""
from .implementation.entities import BackendEntity

type_check(backend_entity, BackendEntity)
entity = cls.__new__(cls)
entity._backend_entity = backend_entity # pylint: disable=protected-access
call_with_super_check(entity.initialize)
return entity
2 changes: 1 addition & 1 deletion aiida/orm/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def user(self) -> 'User':
"""
:return: the user associated with this group
"""
return users.User.from_backend_entity(self._backend_entity.user)
return entities.from_backend_entity(users.User, self._backend_entity.user)

@user.setter
def user(self, user: 'User') -> None:
Expand Down
3 changes: 2 additions & 1 deletion aiida/orm/nodes/data/code/installed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from aiida.common.lang import type_check
from aiida.common.log import override_log_level
from aiida.orm import Computer
from aiida.orm.entities import from_backend_entity

from .legacy import Code

Expand Down Expand Up @@ -105,7 +106,7 @@ def get_executable(self) -> pathlib.PurePosixPath:
def computer(self) -> Computer:
"""Return the computer of this code."""
assert self.backend_entity.computer is not None
return Computer.from_backend_entity(self.backend_entity.computer)
return from_backend_entity(Computer, self.backend_entity.computer)

@computer.setter
def computer(self, computer: Computer) -> None:
Expand Down
3 changes: 2 additions & 1 deletion aiida/orm/nodes/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from aiida.common import exceptions
from aiida.common.lang import override
from aiida.common.links import LinkType
from aiida.orm.entities import from_backend_entity

from ..node import Node

Expand Down Expand Up @@ -70,7 +71,7 @@ def clone(self):
import copy

backend_clone = self.backend_entity.clone()
clone = self.__class__.from_backend_entity(backend_clone)
clone = from_backend_entity(self.__class__, backend_clone)
clone.base.attributes.reset(copy.deepcopy(self.base.attributes.all))
clone.base.repository._clone(self.base.repository) # pylint: disable=protected-access

Expand Down
6 changes: 3 additions & 3 deletions aiida/orm/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from ..computers import Computer
from ..entities import Collection as EntityCollection
from ..entities import Entity
from ..entities import Entity, from_backend_entity
from ..extras import EntityExtras
from ..querybuilder import QueryBuilder
from ..users import User
Expand Down Expand Up @@ -358,7 +358,7 @@ def description(self, value: str) -> None:
def computer(self) -> Optional[Computer]:
"""Return the computer of this node."""
if self.backend_entity.computer:
return Computer.from_backend_entity(self.backend_entity.computer)
return from_backend_entity(Computer, self.backend_entity.computer)

return None

Expand All @@ -378,7 +378,7 @@ def computer(self, computer: Optional[Computer]) -> None:
@property
def user(self) -> User:
"""Return the user of this node."""
return User.from_backend_entity(self.backend_entity.user)
return from_backend_entity(User, self._backend_entity.user)

@user.setter
def user(self, user: User) -> None:
Expand Down