type-annotate all functions outside the tests themselves.

This commit is contained in:
2021-02-28 17:08:27 +01:00
committed by Valentin Lorentz
parent ac2a37362c
commit 62a87b5957
22 changed files with 545 additions and 313 deletions

View File

@ -1,16 +1,17 @@
import datetime
import re
import secrets
from typing import Dict
# thanks jess!
IRCV3_FORMAT_STRFTIME = "%Y-%m-%dT%H:%M:%S.%f%z"
def ircv3_timestamp_to_unixtime(timestamp):
def ircv3_timestamp_to_unixtime(timestamp: str) -> float:
return datetime.datetime.strptime(timestamp, IRCV3_FORMAT_STRFTIME).timestamp()
def random_name(base):
def random_name(base: str) -> str:
return base + "-" + secrets.token_hex(8)
@ -26,16 +27,16 @@ class MultipleReplacer:
# We use an object instead of a lambda function because it avoids the
# need for using the staticmethod() on the lambda function if assigning
# it to a class in Python 3.
def __init__(self, dict_):
def __init__(self, dict_: Dict[str, str]):
self._dict = dict_
dict_ = dict([(re.escape(key), val) for key, val in dict_.items()])
self._matcher = re.compile("|".join(dict_.keys()))
def __call__(self, s):
def __call__(self, s: str) -> str:
return self._matcher.sub(lambda m: self._dict[m.group(0)], s)
def normalizeWhitespace(s, removeNewline=True):
def normalizeWhitespace(s: str, removeNewline: bool = True) -> str:
r"""Normalizes the whitespace in a string; \s+ becomes one space."""
if not s:
return str(s) # not the same reference