Skip to content

Commit

Permalink
add more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Oct 20, 2021
1 parent 3886b71 commit 9a101c9
Showing 3 changed files with 28 additions and 48 deletions.
10 changes: 4 additions & 6 deletions aiida/orm/implementation/logs.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
###########################################################################
"""Backend group module"""
import abc
from typing import List

from .entities import BackendCollection, BackendEntity

@@ -97,35 +98,32 @@ 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.
:raises `~aiida.common.exceptions.IntegrityError`: if all Logs could not be deleted
"""

@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
54 changes: 22 additions & 32 deletions aiida/orm/logs.py
Original file line number Diff line number Diff line change
@@ -8,14 +8,19 @@
# 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
from aiida.manage.manager import get_manager

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):
12 changes: 2 additions & 10 deletions aiida/orm/nodes/node.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9a101c9

Please sign in to comment.