summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonah BrĂ¼chert <jbb@kaidan.im>2024-03-29 03:34:45 +0100
committerJonah BrĂ¼chert <jbb@kaidan.im>2024-03-29 03:34:45 +0100
commit0cb93b99f31a0954eccbe412faf210ca70e8d228 (patch)
tree665fdad8d69fb1d4539d758f4056e90d383ab9ae
parent7d5b1e5b1f845534d565803412eef5916ce2824d (diff)
downloadweb-0cb93b99f31a0954eccbe412faf210ca70e8d228.tar.gz
web-0cb93b99f31a0954eccbe412faf210ca70e8d228.tar.bz2
web-0cb93b99f31a0954eccbe412faf210ca70e8d228.zip
utils: Improve typing
-rw-r--r--accounts/utils/__init__.py10
-rw-r--r--accounts/utils/confirmation.py3
-rw-r--r--accounts/utils/console.py36
-rw-r--r--accounts/utils/login.py5
4 files changed, 33 insertions, 21 deletions
diff --git a/accounts/utils/__init__.py b/accounts/utils/__init__.py
index 1f79953..dfb02af 100644
--- a/accounts/utils/__init__.py
+++ b/accounts/utils/__init__.py
@@ -4,14 +4,16 @@ from functools import wraps
from flask import render_template, request, Flask
from wtforms.validators import Regexp, ValidationError
-from typing import Optional
+from typing import Optional, Callable, Any
# using http://flask.pocoo.org/docs/patterns/viewdecorators/
-def templated(template: Optional[str] = None):
- def templated_(f):
+def templated(
+ template: Optional[str] = None,
+) -> Callable[..., Callable[..., str]]:
+ def templated_(f: Callable[..., str]) -> Callable[..., str]:
@wraps(f)
- def templated__(*args, **kwargs):
+ def templated__(*args: list[Any], **kwargs: dict[str, Any]) -> str:
template_name = template
if template_name is None:
if request.endpoint:
diff --git a/accounts/utils/confirmation.py b/accounts/utils/confirmation.py
index 62f14ad..bed3453 100644
--- a/accounts/utils/confirmation.py
+++ b/accounts/utils/confirmation.py
@@ -8,7 +8,8 @@ from typing import Union, Optional, Any
class Confirmation(URLSafeTimedSerializer):
- def __init__(self, realm: str, key=None, **kwargs):
+ def __init__(self, realm: str, key: Optional[str] = None,
+ **kwargs) -> None:
if key is None:
key = accounts_app.config["SECRET_KEY"]
super(Confirmation, self).__init__(key, salt=realm, **kwargs)
diff --git a/accounts/utils/console.py b/accounts/utils/console.py
index 1b539bd..cabfc08 100644
--- a/accounts/utils/console.py
+++ b/accounts/utils/console.py
@@ -1,10 +1,18 @@
# -*- coding: utf-8 -*-
+from typing import Any, Optional, Callable
+
+from wtforms import Form
+
class TablePrinter:
separator: str
+ headers: Optional[list[str]]
+ widths: list[int]
- def __init__(self, headers=None, separator="|"):
+ def __init__(
+ self, headers: Optional[list[str]] = None, separator: str = "|"
+ ) -> None:
self.headers = headers
self.separator = separator
@@ -14,11 +22,11 @@ class TablePrinter:
if headers is not None:
self._calulate_widths([headers])
- def _calulate_widths(self, rows) -> None:
- def _get_column_count(rows: list):
+ def _calulate_widths(self, rows: list[list[str]]) -> None:
+ def _get_column_count(rows: list[list[str]]) -> int:
return min([len(row) for row in rows])
- def _column_width(column: tuple, width: int) -> int:
+ def _column_width(column: tuple[str, ...], width: int) -> int:
widths = [len(str(elem)) for elem in column]
widths.append(width)
return max(widths)
@@ -41,7 +49,7 @@ class TablePrinter:
self.separator,
)
- def output(self, rows):
+ def output(self, rows: list[list[str]]) -> None:
if len(rows) > 0:
self._calulate_widths(rows)
@@ -66,25 +74,27 @@ class TablePrinter:
)
)
- def _print_row(self, row) -> None:
+ def _print_row(self, row: list[str]) -> None:
print((self.format_string % tuple(row)))
-class ConsoleForm(object):
+class ConsoleForm:
_ready = False
- def __init__(self, formcls, **kwargs):
+ def __init__(
+ self, formcls: Callable[..., Form], **kwargs: dict[str, Any]
+ ) -> None:
self.form = formcls(meta={"csrf": False})
self._fill(kwargs)
self._ready = True
- def _fill(self, data) -> None:
+ def _fill(self, data: dict[str, Any]) -> None:
for key, value in list(data.items()):
field = getattr(self.form, key, None)
if field is not None:
field.data = value
- def print_errors(self):
+ def print_errors(self) -> None:
for field, errors in list(self.form.errors.items()):
if len(errors) > 1:
print(("%s:" % field))
@@ -93,16 +103,16 @@ class ConsoleForm(object):
else:
print(("%s: %s" % (field, errors[0])))
- def __getattr__(self, name):
+ def __getattr__(self, name: str) -> Any:
return getattr(self.form, name)
- def __setattr__(self, name, value):
+ def __setattr__(self, name: str, value: Any) -> None:
if self._ready and name not in self.__dict__:
setattr(self.form, name, value)
else:
super(ConsoleForm, self).__setattr__(name, value)
- def __delattr__(self, name):
+ def __delattr__(self, name: str) -> None:
if self._ready and name not in self.__dict__:
delattr(self.form, name)
else:
diff --git a/accounts/utils/login.py b/accounts/utils/login.py
index 938268f..ec940ad 100644
--- a/accounts/utils/login.py
+++ b/accounts/utils/login.py
@@ -4,7 +4,6 @@ from functools import wraps
from werkzeug.exceptions import Forbidden
from itsdangerous import base64_decode, base64_encode
import json
-import flask_login.login_manager
from accounts.app import accounts_app
from typing import Union, Any, Optional
@@ -16,13 +15,13 @@ class _compact_json:
return json.loads(payload)
@staticmethod
- def dumps(obj: Union[list, dict, tuple], **kwargs):
+ def dumps(obj: Union[list, dict, tuple], **kwargs) -> str:
kwargs.setdefault("ensure_ascii", False)
kwargs.setdefault("separators", (",", ":"))
return json.dumps(obj, **kwargs)
-def create_login_manager() -> flask_login.login_manager.LoginManager:
+def create_login_manager() -> LoginManager:
login_manager = LoginManager()
login_manager.login_message = "Bitte einloggen"
login_manager.login_view = "login.login"