From 9a101c9a2436cf8ab10ab16bfdb850aeb5a531aa Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 20 Oct 2021 03:24:55 +0200 Subject: [PATCH] add more typing --- aiida/orm/implementation/logs.py | 10 +++--- aiida/orm/logs.py | 54 +++++++++++++------------------- aiida/orm/nodes/node.py | 12 ++----- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/aiida/orm/implementation/logs.py b/aiida/orm/implementation/logs.py index 172b0dcaa5..ef53c90fad 100644 --- a/aiida/orm/implementation/logs.py +++ b/aiida/orm/implementation/logs.py @@ -9,6 +9,7 @@ ########################################################################### """Backend group module""" import abc +from typing import List from .entities import BackendCollection, BackendEntity @@ -97,19 +98,18 @@ class BackendLogCollection(BackendCollection[BackendLog]): ENTITY_CLASS = BackendLog @abc.abstractmethod - def delete(self, log_id): + def delete(self, log_id: int) -> None: """ Remove a Log entry from the collection with the given id :param log_id: id of the Log to delete - :type log_id: int :raises TypeError: if ``log_id`` is not an `int` :raises `~aiida.common.exceptions.NotExistent`: if Log with ID ``log_id`` is not found """ @abc.abstractmethod - def delete_all(self): + def delete_all(self) -> None: """ Delete all Log entries. @@ -117,15 +117,13 @@ def delete_all(self): """ @abc.abstractmethod - def delete_many(self, filters): + def delete_many(self, filters) -> List[int]: """ Delete Logs based on ``filters`` :param filters: similar to QueryBuilder filter - :type filters: dict :return: (former) ``PK`` s of deleted Logs - :rtype: list :raises TypeError: if ``filters`` is not a `dict` :raises `~aiida.common.exceptions.ValidationError`: if ``filters`` is empty diff --git a/aiida/orm/logs.py b/aiida/orm/logs.py index 8a372f59ce..36b6c20ba2 100644 --- a/aiida/orm/logs.py +++ b/aiida/orm/logs.py @@ -8,7 +8,8 @@ # For further information please visit http://www.aiida.net # ########################################################################### """Module for orm logging abstract classes""" -from typing import Type +import logging +from typing import TYPE_CHECKING, List, Optional, Type from aiida.common import timezone from aiida.common.lang import classproperty @@ -16,6 +17,10 @@ from . import entities +if TYPE_CHECKING: + from aiida.orm import Node + from aiida.orm.querybuilder import FilterType, OrderByType + __all__ = ('Log', 'OrderSpecifier', 'ASCENDING', 'DESCENDING') ASCENDING = 'asc' @@ -36,16 +41,11 @@ class LogCollection(entities.Collection['Log']): def _entity_base_cls() -> Type['Log']: return Log - @staticmethod - def create_entry_from_record(record): - """ - Helper function to create a log entry from a record created as by the python logging library + def create_entry_from_record(self, record: logging.LogRecord) -> 'Log': + """Helper function to create a log entry from a record created as by the python logging library :param record: The record created by the logging module - :type record: :class:`logging.LogRecord` - - :return: An object implementing the log entry interface - :rtype: :class:`aiida.orm.logs.Log` + :return: A stored log instance """ from datetime import datetime @@ -76,21 +76,17 @@ def create_entry_from_record(record): levelname=record.levelname, dbnode_id=dbnode_id, message=message, - metadata=metadata + metadata=metadata, + backend=self.backend ) - def get_logs_for(self, entity, order_by=None): - """ - Get all the log messages for a given entity and optionally sort + def get_logs_for(self, entity: 'Node', order_by: Optional['OrderByType'] = None) -> List['Log']: + """Get all the log messages for a given node and optionally sort :param entity: the entity to get logs for - :type entity: :class:`aiida.orm.Entity` - :param order_by: a list of (key, direction) pairs specifying the sort order - :type order_by: list :return: the list of log entries - :rtype: list """ from . import nodes @@ -100,37 +96,31 @@ def get_logs_for(self, entity, order_by=None): return self.find({'dbnode_id': entity.pk}, order_by=order_by) def delete(self, pk: int) -> None: - """ - Remove a Log entry from the collection with the given id + """Remove a Log entry from the collection with the given id :param pk: id of the Log to delete :raises `~aiida.common.exceptions.NotExistent`: if Log with ID ``pk`` is not found """ - self._backend.logs.delete(pk) + return self._backend.logs.delete(pk) - def delete_all(self): - """ - Delete all Logs in the collection + def delete_all(self) -> None: + """Delete all Logs in the collection :raises `~aiida.common.exceptions.IntegrityError`: if all Logs could not be deleted """ - self._backend.logs.delete_all() - - def delete_many(self, filters): - """ - Delete Logs based on ``filters`` + return self._backend.logs.delete_all() - :param filters: similar to QueryBuilder filter - :type filters: dict + def delete_many(self, filters: 'FilterType') -> List[int]: + """Delete Logs based on ``filters`` + :param filters: filters to pass to the QueryBuilder :return: (former) ``PK`` s of deleted Logs - :rtype: list :raises TypeError: if ``filters`` is not a `dict` :raises `~aiida.common.exceptions.ValidationError`: if ``filters`` is empty """ - self._backend.logs.delete_many(filters) + return self._backend.logs.delete_many(filters) class Log(entities.Entity): diff --git a/aiida/orm/nodes/node.py b/aiida/orm/nodes/node.py index 7b40f22db5..66ab7b14fa 100644 --- a/aiida/orm/nodes/node.py +++ b/aiida/orm/nodes/node.py @@ -324,11 +324,7 @@ def repository_metadata(self, value): @property def computer(self) -> Optional[Computer]: - """Return the computer of this node. - - :return: the computer or None - :rtype: `Computer` or None - """ + """Return the computer of this node.""" if self.backend_entity.computer: return Computer.from_backend_entity(self.backend_entity.computer) @@ -352,11 +348,7 @@ def computer(self, computer: Optional[Computer]) -> None: @property def user(self) -> User: - """Return the user of this node. - - :return: the user - :rtype: `User` - """ + """Return the user of this node.""" return User.from_backend_entity(self.backend_entity.user) @user.setter