1
0
Fork 0
forked from s3lph/matemat

Merge branch 'mypy-type-fixes' into 'master'

Fixed some minor type annotation issues.

See merge request s3lph/matemat!16
This commit is contained in:
s3lph 2018-07-14 22:56:43 +00:00
commit f4685114b2
4 changed files with 19 additions and 10 deletions

View file

@ -208,7 +208,7 @@ class MatematDatabase(object):
if verify_password and not compare_digest(crypt.crypt(password, row[0]), row[0]): if verify_password and not compare_digest(crypt.crypt(password, row[0]), row[0]):
raise AuthenticationError('Password does not match.') raise AuthenticationError('Password does not match.')
# Hash the new touchkey and write it to the database. # 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(''' c.execute('''
UPDATE users SET touchkey = :tkhash, lastchange = STRFTIME('%s', 'now') WHERE user_id = :user_id UPDATE users SET touchkey = :tkhash, lastchange = STRFTIME('%s', 'now') WHERE user_id = :user_id
''', { ''', {

View file

@ -1,5 +1,5 @@
from typing import Any from typing import Any, Optional
import sqlite3 import sqlite3
@ -10,7 +10,7 @@ class Transaction(object):
def __init__(self, db: sqlite3.Connection, exclusive: bool = True) -> None: def __init__(self, db: sqlite3.Connection, exclusive: bool = True) -> None:
self._db: sqlite3.Connection = db self._db: sqlite3.Connection = db
self._cursor = None self._cursor: Optional[sqlite3.Cursor] = None
self._excl = exclusive self._excl = exclusive
self._is_dummy: bool = False self._is_dummy: bool = False
@ -74,7 +74,7 @@ class DatabaseWrapper(object):
def __init__(self, filename: str) -> None: def __init__(self, filename: str) -> None:
self._filename: str = filename self._filename: str = filename
self._sqlite_db: sqlite3.Connection = None self._sqlite_db: Optional[sqlite3.Connection] = None
def __enter__(self) -> 'DatabaseWrapper': def __enter__(self) -> 'DatabaseWrapper':
self.connect() self.connect()
@ -84,6 +84,8 @@ class DatabaseWrapper(object):
self.close() self.close()
def transaction(self, exclusive: bool = True) -> Transaction: 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) return Transaction(self._sqlite_db, exclusive)
def _setup(self) -> None: def _setup(self) -> None:
@ -113,13 +115,15 @@ class DatabaseWrapper(object):
self._sqlite_db = None self._sqlite_db = None
def in_transaction(self) -> bool: 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: def is_connected(self) -> bool:
return self._sqlite_db is not None return self._sqlite_db is not None
@property @property
def _user_version(self) -> int: 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 = self._sqlite_db.cursor()
cursor.execute('PRAGMA user_version') cursor.execute('PRAGMA user_version')
version = int(cursor.fetchone()[0]) version = int(cursor.fetchone()[0])
@ -127,5 +131,7 @@ class DatabaseWrapper(object):
@_user_version.setter @_user_version.setter
def _user_version(self, version: int) -> None: 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 = self._sqlite_db.cursor()
cursor.execute(f'PRAGMA user_version = {version}') cursor.execute(f'PRAGMA user_version = {version}')

View file

@ -1,11 +1,14 @@
from typing import Optional
class HttpException(Exception): class HttpException(Exception):
def __init__(self, status: int = 500, title: str = 'An error occurred', message: str = None) -> None: def __init__(self, status: int = 500, title: str = 'An error occurred', message: str = None) -> None:
super().__init__() super().__init__()
self.__status: int = status self.__status: int = status
self.__title: str = title self.__title: str = title
self.__message: str = message self.__message: Optional[str] = message
@property @property
def status(self) -> int: def status(self) -> int:
@ -16,5 +19,5 @@ class HttpException(Exception):
return self.__title return self.__title
@property @property
def message(self) -> str: def message(self) -> Optional[str]:
return self.__message return self.__message

View file

@ -11,7 +11,7 @@ class PageletResponse:
An instance of this base class will result in an empty 200 OK response. 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. Create an empty response.
@ -26,7 +26,7 @@ class RedirectResponse(PageletResponse):
response status, and a Location header. 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. 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. 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. Create a template response with the given template name and arguments.