Merge pull request #191 from ProgVal/mypy
Fix type errors detected by 'mypy --ignore-missing-imports src'.
This commit is contained in:
commit
dde0dcc37b
10 changed files with 51 additions and 38 deletions
|
@ -12,7 +12,7 @@ class ControlClient(object):
|
||||||
def fileno(self) -> int:
|
def fileno(self) -> int:
|
||||||
return self._socket.fileno()
|
return self._socket.fileno()
|
||||||
|
|
||||||
def read_lines(self) -> typing.List[str]:
|
def read_lines(self) -> typing.Optional[typing.List[str]]:
|
||||||
try:
|
try:
|
||||||
data = self._socket.recv(2048)
|
data = self._socket.recv(2048)
|
||||||
except:
|
except:
|
||||||
|
@ -45,11 +45,11 @@ class Control(PollSource.PollSource):
|
||||||
|
|
||||||
self._filename = filename
|
self._filename = filename
|
||||||
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
self._clients = {}
|
self._clients: typing.Dict[int, ControlClient] = {}
|
||||||
|
|
||||||
def _on_log(self, levelno: int, line: str):
|
def _on_log(self, levelno: int, line: str):
|
||||||
for client in self._clients.values():
|
for client in self._clients.values():
|
||||||
if not client.log_level == None and client.log_level <= levelno:
|
if client.log_level is not None and client.log_level <= levelno:
|
||||||
self._send_action(client, "log", line)
|
self._send_action(client, "log", line)
|
||||||
|
|
||||||
def bind(self):
|
def bind(self):
|
||||||
|
@ -69,7 +69,7 @@ class Control(PollSource.PollSource):
|
||||||
elif fileno in self._clients:
|
elif fileno in self._clients:
|
||||||
client = self._clients[fileno]
|
client = self._clients[fileno]
|
||||||
lines = client.read_lines()
|
lines = client.read_lines()
|
||||||
if lines == None:
|
if lines is None:
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
del self._clients[fileno]
|
del self._clients[fileno]
|
||||||
else:
|
else:
|
||||||
|
@ -109,7 +109,7 @@ class Control(PollSource.PollSource):
|
||||||
if not keepalive:
|
if not keepalive:
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
def _send_action(self, client: ControlClient, action: str, data: str,
|
def _send_action(self, client: ControlClient, action: str,
|
||||||
id: int=None):
|
data: typing.Optional[str], id: typing.Optional[str]=None):
|
||||||
client.write_line(
|
client.write_line(
|
||||||
json.dumps({"action": action, "data": data, "id": id}))
|
json.dumps({"action": action, "data": data, "id": id}))
|
||||||
|
|
|
@ -37,8 +37,8 @@ class EventHook(object):
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
self.docstring = utils.parse.docstring(func.__doc__ or "")
|
self.docstring = utils.parse.docstring(func.__doc__ or "")
|
||||||
|
|
||||||
self._kwargs = {}
|
self._kwargs: typing.Dict[str, typing.Any] = {}
|
||||||
self._multi_kwargs = {}
|
self._multi_kwargs: typing.Dict[str, typing.List[typing.Any]] = {}
|
||||||
for key, value in kwargs:
|
for key, value in kwargs:
|
||||||
if key in self._multi_kwargs:
|
if key in self._multi_kwargs:
|
||||||
self._multi_kwargs[key].append(value)
|
self._multi_kwargs[key].append(value)
|
||||||
|
|
|
@ -67,7 +67,11 @@ class Buffer(object):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def find_from(self, nickname: str) -> typing.Optional[BufferLine]:
|
def find_from(self, nickname: str) -> typing.Optional[BufferLine]:
|
||||||
return (self.find_many_from(nickname, 1) or [None])[0]
|
lines = self.find_many_from(nickname, 1)
|
||||||
|
if lines:
|
||||||
|
return lines[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
def find_many_from(self, nickname: str, max: int
|
def find_many_from(self, nickname: str, max: int
|
||||||
) -> typing.List[BufferLine]:
|
) -> typing.List[BufferLine]:
|
||||||
nickname_lower = self.server.irc_lower(nickname)
|
nickname_lower = self.server.irc_lower(nickname)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import datetime, typing, uuid
|
import datetime, typing, uuid
|
||||||
from src import IRCObject, utils
|
from src import EventManager, IRCObject, utils
|
||||||
|
|
||||||
# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
|
# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
|
||||||
LINE_MAX = 470
|
LINE_MAX = 470
|
||||||
|
@ -130,7 +130,7 @@ class ParsedLine(object):
|
||||||
|
|
||||||
def _newline_truncate(self, line: str) -> typing.Tuple[str, str]:
|
def _newline_truncate(self, line: str) -> typing.Tuple[str, str]:
|
||||||
line, sep, overflow = line.partition("\n")
|
line, sep, overflow = line.partition("\n")
|
||||||
return [line, overflow]
|
return (line, overflow)
|
||||||
def _line_max(self, hostmask: str, margin: int) -> int:
|
def _line_max(self, hostmask: str, margin: int) -> int:
|
||||||
return LINE_MAX-len((":%s " % hostmask).encode("utf8"))-margin
|
return LINE_MAX-len((":%s " % hostmask).encode("utf8"))-margin
|
||||||
def truncate(self, hostmask: str, margin: int=0) -> typing.Tuple[str, str]:
|
def truncate(self, hostmask: str, margin: int=0) -> typing.Tuple[str, str]:
|
||||||
|
|
|
@ -166,7 +166,7 @@ class Server(IRCObject.Object):
|
||||||
|
|
||||||
def has_user(self, nickname: str) -> bool:
|
def has_user(self, nickname: str) -> bool:
|
||||||
return self.irc_lower(nickname) in self.users
|
return self.irc_lower(nickname) in self.users
|
||||||
def get_user(self, nickname: str, username: str=None, hostname: str=None,
|
def get_user(self, nickname: str, username: typing.Optional[str]=None, hostname: str=None,
|
||||||
create: bool=True) -> typing.Optional[IRCUser.User]:
|
create: bool=True) -> typing.Optional[IRCUser.User]:
|
||||||
new = False
|
new = False
|
||||||
if not self.has_user(nickname) and create:
|
if not self.has_user(nickname) and create:
|
||||||
|
@ -178,9 +178,9 @@ class Server(IRCObject.Object):
|
||||||
|
|
||||||
user = self.users.get(self.irc_lower(nickname), None)
|
user = self.users.get(self.irc_lower(nickname), None)
|
||||||
if user:
|
if user:
|
||||||
if not username == None:
|
if username is not None:
|
||||||
user.username = username
|
user.username = username
|
||||||
if not hostname == None:
|
if hostname is not None:
|
||||||
user.hostname = hostname
|
user.hostname = hostname
|
||||||
if new:
|
if new:
|
||||||
self.events.on("new.user").call(user=new_user, server=self)
|
self.events.on("new.user").call(user=new_user, server=self)
|
||||||
|
|
|
@ -11,11 +11,11 @@ class User(IRCObject.Object):
|
||||||
self.server = server
|
self.server = server
|
||||||
self.set_nickname(nickname)
|
self.set_nickname(nickname)
|
||||||
self._id = id
|
self._id = id
|
||||||
self.username = None
|
self.username: typing.Optional[str] = None
|
||||||
self.hostname = None
|
self.hostname: typing.Optional[str] = None
|
||||||
self.realname = None
|
self.realname: typing.Optional[str] = None
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.channels = set([]) # type: typing.Set[IRCChannel.Channel]
|
self.channels: typing.Set[IRCChannel.Channel] = set([])
|
||||||
|
|
||||||
self.identified_account = None
|
self.identified_account = None
|
||||||
self.identified_account_override = None
|
self.identified_account_override = None
|
||||||
|
@ -23,7 +23,7 @@ class User(IRCObject.Object):
|
||||||
self.identified_account_id = None
|
self.identified_account_id = None
|
||||||
self.identified_account_id_override = None
|
self.identified_account_id_override = None
|
||||||
self.away = False
|
self.away = False
|
||||||
self.away_message = None # type: typing.Optional[str]
|
self.away_message: typing.Optional[str] = None
|
||||||
|
|
||||||
self.buffer = IRCBuffer.Buffer(bot, server)
|
self.buffer = IRCBuffer.Buffer(bot, server)
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ class User(IRCObject.Object):
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.nickname
|
return self.nickname
|
||||||
|
|
||||||
def hostmask(self) -> str:
|
def hostmask(self) -> typing.Optional[str]:
|
||||||
if self.nickname and self.username and self.hostname:
|
if self.nickname and self.username and self.hostname:
|
||||||
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
|
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -24,6 +24,8 @@ class HookedHandler(logging.StreamHandler):
|
||||||
self._func(record.levelno, self.format(record))
|
self._func(record.levelno, self.format(record))
|
||||||
|
|
||||||
class Log(object):
|
class Log(object):
|
||||||
|
_hooks: typing.List[typing.Callable[[int, str], None]]
|
||||||
|
|
||||||
def __init__(self, to_file: bool, level: str, location: str):
|
def __init__(self, to_file: bool, level: str, location: str):
|
||||||
self._hooks = []
|
self._hooks = []
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import enum, gc, glob, importlib, io, inspect, os, sys, typing, uuid
|
import enum, gc, glob, importlib, importlib.util, io, inspect, os, sys
|
||||||
|
import typing, uuid
|
||||||
from src import Config, EventManager, Exports, IRCBot, Logging, Timers, utils
|
from src import Config, EventManager, Exports, IRCBot, Logging, Timers, utils
|
||||||
|
|
||||||
class ModuleException(Exception):
|
class ModuleException(Exception):
|
||||||
|
@ -155,7 +156,7 @@ class ModuleManager(object):
|
||||||
|
|
||||||
def _module_name(self, path: str) -> str:
|
def _module_name(self, path: str) -> str:
|
||||||
return os.path.basename(path).rsplit(".py", 1)[0].lower()
|
return os.path.basename(path).rsplit(".py", 1)[0].lower()
|
||||||
def _module_paths(self, name: str) -> str:
|
def _module_paths(self, name: str) -> typing.List[str]:
|
||||||
paths = []
|
paths = []
|
||||||
for directory in self.directories:
|
for directory in self.directories:
|
||||||
paths.append(os.path.join(directory, name))
|
paths.append(os.path.join(directory, name))
|
||||||
|
@ -180,7 +181,7 @@ class ModuleManager(object):
|
||||||
return getattr(obj, magic) if hasattr(obj, magic) else default
|
return getattr(obj, magic) if hasattr(obj, magic) else default
|
||||||
|
|
||||||
def _check_hashflags(self, bot: "IRCBot.Bot", definition: ModuleDefinition
|
def _check_hashflags(self, bot: "IRCBot.Bot", definition: ModuleDefinition
|
||||||
) -> bool:
|
) -> None:
|
||||||
for hashflag, value in definition.hashflags:
|
for hashflag, value in definition.hashflags:
|
||||||
if hashflag == "ignore":
|
if hashflag == "ignore":
|
||||||
# nope, ignore this module.
|
# nope, ignore this module.
|
||||||
|
@ -321,7 +322,7 @@ class ModuleManager(object):
|
||||||
|
|
||||||
def load_modules(self, bot: "IRCBot.Bot", whitelist: typing.List[str]=[],
|
def load_modules(self, bot: "IRCBot.Bot", whitelist: typing.List[str]=[],
|
||||||
blacklist: typing.List[str]=[]
|
blacklist: typing.List[str]=[]
|
||||||
) -> typing.Tuple[typing.List[str], typing.List[str]]:
|
) -> None:
|
||||||
loadable, nonloadable = self._list_valid_modules(bot, whitelist, blacklist)
|
loadable, nonloadable = self._list_valid_modules(bot, whitelist, blacklist)
|
||||||
|
|
||||||
for definition in nonloadable:
|
for definition in nonloadable:
|
||||||
|
@ -388,7 +389,7 @@ class ModuleManager(object):
|
||||||
failed = (definition, e)
|
failed = (definition, e)
|
||||||
break
|
break
|
||||||
|
|
||||||
if not failed == None:
|
if failed is not None:
|
||||||
for module in self.modules.values():
|
for module in self.modules.values():
|
||||||
self._unload_module(module)
|
self._unload_module(module)
|
||||||
self.modules = old_modules
|
self.modules = old_modules
|
||||||
|
|
|
@ -177,7 +177,7 @@ class BitBotMagic(object):
|
||||||
self._kwargs.append((key, value))
|
self._kwargs.append((key, value))
|
||||||
|
|
||||||
def get_hooks(self):
|
def get_hooks(self):
|
||||||
hooks: typing.List[typing.Tuple[str, typing.List[str, typing.Any]]] = []
|
hooks: typing.List[typing.Tuple[str, typing.List[Tuple[str, typing.Any]]]] = []
|
||||||
for hook, kwargs in self._hooks:
|
for hook, kwargs in self._hooks:
|
||||||
hooks.append((hook, self._kwargs.copy()+list(kwargs.items())))
|
hooks.append((hook, self._kwargs.copy()+list(kwargs.items())))
|
||||||
return hooks
|
return hooks
|
||||||
|
@ -254,8 +254,11 @@ class CaseInsensitiveDict(dict):
|
||||||
return dict.__getitem__(self, key.lower())
|
return dict.__getitem__(self, key.lower())
|
||||||
def __setitem__(self, key: str, value: typing.Any) -> typing.Any:
|
def __setitem__(self, key: str, value: typing.Any) -> typing.Any:
|
||||||
return dict.__setitem__(self, key.lower(), value)
|
return dict.__setitem__(self, key.lower(), value)
|
||||||
def __contains__(self, key: str):
|
def __contains__(self, key: typing.Any) -> bool:
|
||||||
return dict.__contains__(self, key.lower())
|
if isinstance(key, str):
|
||||||
|
return dict.__contains__(self, key.lower())
|
||||||
|
else:
|
||||||
|
raise TypeError('Expected string, not %r' % (key,))
|
||||||
def get(self, key: str, default: typing.Any=None):
|
def get(self, key: str, default: typing.Any=None):
|
||||||
return dict.get(self, key.lower(), default)
|
return dict.get(self, key.lower(), default)
|
||||||
|
|
||||||
|
@ -296,7 +299,7 @@ class Setting(object):
|
||||||
SETTING_TRUE = ["true", "yes", "on", "y"]
|
SETTING_TRUE = ["true", "yes", "on", "y"]
|
||||||
SETTING_FALSE = ["false", "no", "off", "n"]
|
SETTING_FALSE = ["false", "no", "off", "n"]
|
||||||
class BoolSetting(Setting):
|
class BoolSetting(Setting):
|
||||||
example = "on"
|
example: typing.Optional[str] = "on"
|
||||||
def parse(self, value: str) -> typing.Any:
|
def parse(self, value: str) -> typing.Any:
|
||||||
value_lower = value.lower()
|
value_lower = value.lower()
|
||||||
if value_lower in SETTING_TRUE:
|
if value_lower in SETTING_TRUE:
|
||||||
|
@ -306,7 +309,7 @@ class BoolSetting(Setting):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
class IntSetting(Setting):
|
class IntSetting(Setting):
|
||||||
example = "10"
|
example: typing.Optional[str] = "10"
|
||||||
def parse(self, value: str) -> typing.Any:
|
def parse(self, value: str) -> typing.Any:
|
||||||
if value == "0":
|
if value == "0":
|
||||||
return 0
|
return 0
|
||||||
|
@ -317,7 +320,7 @@ class IntSetting(Setting):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
class IntRangeSetting(IntSetting):
|
class IntRangeSetting(IntSetting):
|
||||||
example = None
|
example: typing.Optional[str] = None
|
||||||
def __init__(self, n_min: int, n_max: int, name: str, help: str=None,
|
def __init__(self, n_min: int, n_max: int, name: str, help: str=None,
|
||||||
example: str=None):
|
example: str=None):
|
||||||
self._n_min = n_min
|
self._n_min = n_min
|
||||||
|
@ -365,7 +368,7 @@ class FunctionSetting(Setting):
|
||||||
self._func = func
|
self._func = func
|
||||||
Setting.__init__(self, name, help, example)
|
Setting.__init__(self, name, help, example)
|
||||||
if not format == None:
|
if not format == None:
|
||||||
self.format = format
|
self.format = format # type: ignore
|
||||||
|
|
||||||
def parse(self, value: str) -> typing.Any:
|
def parse(self, value: str) -> typing.Any:
|
||||||
return self._func(value)
|
return self._func(value)
|
||||||
|
@ -399,8 +402,11 @@ def deadline(seconds: int=10):
|
||||||
signal.signal(signal.SIGALRM, old_handler)
|
signal.signal(signal.SIGALRM, old_handler)
|
||||||
signal.setitimer(signal.ITIMER_REAL, old_seconds, 0)
|
signal.setitimer(signal.ITIMER_REAL, old_seconds, 0)
|
||||||
|
|
||||||
def deadline_process(func: typing.Callable[[], None], seconds: int=10):
|
DeadlineProcessReturnType = typing.TypeVar('DeadlineProcessReturnType')
|
||||||
q = multiprocessing.Queue()
|
def deadline_process(func: typing.Callable[[], DeadlineProcessReturnType],
|
||||||
|
seconds: int=10) -> DeadlineProcessReturnType:
|
||||||
|
q: multiprocessing.Queue[typing.Tuple[bool, DeadlineProcessReturnType]] = \
|
||||||
|
multiprocessing.Queue()
|
||||||
def _wrap(func, q):
|
def _wrap(func, q):
|
||||||
try:
|
try:
|
||||||
q.put([True, func()])
|
q.put([True, func()])
|
||||||
|
@ -414,7 +420,7 @@ def deadline_process(func: typing.Callable[[], None], seconds: int=10):
|
||||||
try:
|
try:
|
||||||
success, out = q.get(block=True, timeout=seconds)
|
success, out = q.get(block=True, timeout=seconds)
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
p.kill()
|
p.kill() # type: ignore # to make mypy pass on Python 3.6
|
||||||
deadlined = True
|
deadlined = True
|
||||||
finally:
|
finally:
|
||||||
q.close()
|
q.close()
|
||||||
|
@ -425,4 +431,4 @@ def deadline_process(func: typing.Callable[[], None], seconds: int=10):
|
||||||
if success:
|
if success:
|
||||||
return out
|
return out
|
||||||
else:
|
else:
|
||||||
raise out
|
raise out # type: ignore
|
||||||
|
|
|
@ -162,13 +162,13 @@ def _find_encoding(soup: bs4.BeautifulSoup) -> typing.Optional[str]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def request(request_obj: typing.Union[str, Request], **kwargs) -> Response:
|
def request(request_obj: typing.Union[str, Request], **kwargs) -> Response:
|
||||||
if type(request_obj) == str:
|
if isinstance(request_obj, str):
|
||||||
request_obj = Request(request_obj, **kwargs)
|
request_obj = Request(request_obj, **kwargs)
|
||||||
return _request(request_obj)
|
return _request(request_obj)
|
||||||
|
|
||||||
def _request(request_obj: Request) -> Response:
|
def _request(request_obj: Request) -> Response:
|
||||||
|
|
||||||
def _wrap():
|
def _wrap() -> Response:
|
||||||
headers = request_obj.get_headers()
|
headers = request_obj.get_headers()
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
request_obj.method,
|
request_obj.method,
|
||||||
|
|
Loading…
Reference in a new issue