Skip to content

Commit

Permalink
👌 IMPROVE: Add typing to QueryBuilder
Browse files Browse the repository at this point in the history
In preparation for migration to sqlalchemy 1.4
  • Loading branch information
chrisjsewell committed Aug 12, 2021
1 parent 6f93253 commit f39c192
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 251 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ repos:
aiida/engine/.*py|
aiida/manage/manager.py|
aiida/manage/database/delete/nodes.py|
aiida/orm/querybuilder.py|
aiida/orm/nodes/node.py|
aiida/orm/nodes/process/.*py|
aiida/repository/.*py|
Expand Down
93 changes: 29 additions & 64 deletions aiida/orm/implementation/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
###########################################################################
"""Generic backend related objects"""
import abc
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from sqlalchemy.orm.session import Session

from aiida.orm.implementation import (
BackendAuthInfoCollection, BackendCommentCollection, BackendComputerCollection, BackendGroupCollection,
BackendLogCollection, BackendNodeCollection, BackendQueryBuilder, BackendUserCollection
)
from aiida.backends.general.abstractqueries import AbstractQueryManager

__all__ = ('Backend',)

Expand All @@ -21,85 +31,40 @@ def migrate(self):
"""Migrate the database to the latest schema generation or version."""

@abc.abstractproperty
def authinfos(self):
"""
Return the collection of authorisation information objects
:return: the authinfo collection
:rtype: :class:`aiida.orm.implementation.BackendAuthInfoCollection`
"""
def authinfos(self) -> 'BackendAuthInfoCollection':
"""Return the collection of authorisation information objects"""

@abc.abstractproperty
def comments(self):
"""
Return the collection of comments
:return: the comment collection
:rtype: :class:`aiida.orm.implementation.BackendCommentCollection`
"""
def comments(self) -> 'BackendCommentCollection':
"""Return the collection of comments"""

@abc.abstractproperty
def computers(self):
"""
Return the collection of computers
:return: the computers collection
:rtype: :class:`aiida.orm.implementation.BackendComputerCollection`
"""
def computers(self) -> 'BackendComputerCollection':
"""Return the collection of computers"""

@abc.abstractproperty
def groups(self):
"""
Return the collection of groups
:return: the groups collection
:rtype: :class:`aiida.orm.implementation.BackendGroupCollection`
"""
def groups(self) -> 'BackendGroupCollection':
"""Return the collection of groups"""

@abc.abstractproperty
def logs(self):
"""
Return the collection of logs
:return: the log collection
:rtype: :class:`aiida.orm.implementation.BackendLogCollection`
"""
def logs(self) -> 'BackendLogCollection':
"""Return the collection of logs"""

@abc.abstractproperty
def nodes(self):
"""
Return the collection of nodes
:return: the nodes collection
:rtype: :class:`aiida.orm.implementation.BackendNodeCollection`
"""
def nodes(self) -> 'BackendNodeCollection':
"""Return the collection of nodes"""

@abc.abstractproperty
def query_manager(self):
"""
Return the query manager for the objects stored in the backend
:return: The query manger
:rtype: :class:`aiida.backends.general.abstractqueries.AbstractQueryManager`
"""
def query_manager(self) -> 'AbstractQueryManager':
"""Return the query manager for the objects stored in the backend"""

@abc.abstractmethod
def query(self):
"""
Return an instance of a query builder implementation for this backend
:return: a new query builder instance
:rtype: :class:`aiida.orm.implementation.BackendQueryBuilder`
"""
def query(self) -> 'BackendQueryBuilder':
"""Return an instance of a query builder implementation for this backend"""

@abc.abstractproperty
def users(self):
"""
Return the collection of users
:return: the users collection
:rtype: :class:`aiida.orm.implementation.BackendUserCollection`
"""
def users(self) -> 'BackendUserCollection':
"""Return the collection of users"""

@abc.abstractmethod
def transaction(self):
Expand All @@ -112,7 +77,7 @@ def transaction(self):
"""

@abc.abstractmethod
def get_session(self):
def get_session(self) -> 'Session':
"""Return a database session that can be used by the `QueryBuilder` to perform its query.
:return: an instance of :class:`sqlalchemy.orm.session.Session`
Expand Down
6 changes: 5 additions & 1 deletion aiida/orm/implementation/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
likely be moved to a `SqlAlchemyBasedQueryBuilder` class and restore this abstract class to being a pure agnostic one.
"""
import abc
from typing import TYPE_CHECKING
import uuid

# pylint: disable=no-name-in-module,import-error
Expand All @@ -25,6 +26,9 @@

from aiida.common.lang import type_check

if TYPE_CHECKING:
from sqlalchemy.orm.session import Session # pylint: disable=ungrouped-imports

__all__ = ('BackendQueryBuilder',)


Expand Down Expand Up @@ -111,7 +115,7 @@ def AiidaNode(self):
from aiida.orm import Node
return Node

def get_session(self):
def get_session(self) -> 'Session':
"""
:returns: a valid session, an instance of :class:`sqlalchemy.orm.session.Session`
"""
Expand Down
Loading

0 comments on commit f39c192

Please sign in to comment.