diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 0f5d984..a94e420 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -222,7 +222,7 @@ class MatematDatabase(object): if verify_password and not compare_digest(crypt.crypt(password, row[0]), row[0]): raise AuthenticationError('Password does not match.') # Hash the new touchkey and write it to the database. - tkhash: str = crypt.crypt(touchkey, crypt.mksalt(_CRYPT_METHOD)) if touchkey is not None else None + tkhash: Optional[str] = crypt.crypt(touchkey, crypt.mksalt(_CRYPT_METHOD)) if touchkey is not None else None c.execute(''' UPDATE users SET touchkey = :tkhash, lastchange = STRFTIME('%s', 'now') WHERE user_id = :user_id ''', { diff --git a/matemat/db/wrapper.py b/matemat/db/wrapper.py index e930d17..34af430 100644 --- a/matemat/db/wrapper.py +++ b/matemat/db/wrapper.py @@ -1,5 +1,5 @@ -from typing import Any +from typing import Any, Optional import sqlite3 @@ -10,7 +10,7 @@ class Transaction(object): def __init__(self, db: sqlite3.Connection, exclusive: bool = True) -> None: self._db: sqlite3.Connection = db - self._cursor = None + self._cursor: Optional[sqlite3.Cursor] = None self._excl = exclusive self._is_dummy: bool = False @@ -74,7 +74,7 @@ class DatabaseWrapper(object): def __init__(self, filename: str) -> None: self._filename: str = filename - self._sqlite_db: sqlite3.Connection = None + self._sqlite_db: Optional[sqlite3.Connection] = None def __enter__(self) -> 'DatabaseWrapper': self.connect() @@ -84,6 +84,8 @@ class DatabaseWrapper(object): self.close() def transaction(self, exclusive: bool = True) -> Transaction: + if self._sqlite_db is None: + raise RuntimeError(f'Database connection to {self._filename} is not established.') return Transaction(self._sqlite_db, exclusive) def _setup(self) -> None: @@ -113,13 +115,15 @@ class DatabaseWrapper(object): self._sqlite_db = None def in_transaction(self) -> bool: - return self._sqlite_db.in_transaction + return self._sqlite_db is not None and self._sqlite_db.in_transaction def is_connected(self) -> bool: return self._sqlite_db is not None @property def _user_version(self) -> int: + if self._sqlite_db is None: + raise RuntimeError(f'Database connection to {self._filename} is not established.') cursor = self._sqlite_db.cursor() cursor.execute('PRAGMA user_version') version = int(cursor.fetchone()[0]) @@ -127,5 +131,7 @@ class DatabaseWrapper(object): @_user_version.setter def _user_version(self, version: int) -> None: + if self._sqlite_db is None: + raise RuntimeError(f'Database connection to {self._filename} is not established.') cursor = self._sqlite_db.cursor() cursor.execute(f'PRAGMA user_version = {version}') diff --git a/matemat/exceptions/HttpException.py b/matemat/exceptions/HttpException.py index 4f792ce..35fefd3 100644 --- a/matemat/exceptions/HttpException.py +++ b/matemat/exceptions/HttpException.py @@ -1,11 +1,14 @@ +from typing import Optional + + class HttpException(Exception): def __init__(self, status: int = 500, title: str = 'An error occurred', message: str = None) -> None: super().__init__() self.__status: int = status self.__title: str = title - self.__message: str = message + self.__message: Optional[str] = message @property def status(self) -> int: @@ -16,5 +19,5 @@ class HttpException(Exception): return self.__title @property - def message(self) -> str: + def message(self) -> Optional[str]: return self.__message diff --git a/matemat/webserver/responses.py b/matemat/webserver/responses.py index 6f0308f..cf9dbf8 100644 --- a/matemat/webserver/responses.py +++ b/matemat/webserver/responses.py @@ -11,7 +11,7 @@ class PageletResponse: An instance of this base class will result in an empty 200 OK response. """ - def __init__(self, status: int = 200): + def __init__(self, status: int = 200) -> None: """ Create an empty response. @@ -26,7 +26,7 @@ class RedirectResponse(PageletResponse): response status, and a Location header. """ - def __init__(self, location: str): + def __init__(self, location: str) -> None: """ Create a redirection response with the given redirection location. @@ -42,7 +42,7 @@ class TemplateResponse(PageletResponse): sending the result as response body, with a 200 OK response status. """ - def __init__(self, name: str, **kwargs): + def __init__(self, name: str, **kwargs) -> None: """ Create a template response with the given template name and arguments.