From 7c7a1fcde2780ebb72defa874bcc01c8f502f1d5 Mon Sep 17 00:00:00 2001 From: ducklet Date: Sun, 8 Nov 2020 10:35:26 +0100 Subject: [PATCH] fix some mypy lint --- feeder/store.py | 5 ++++- hotdog/bot.py | 4 ++-- hotdog/functions.py | 4 ++-- mypy.ini | 5 +++++ postillon/store.py | 4 ++++ scripts/lint | 4 +++- 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 mypy.ini diff --git a/feeder/store.py b/feeder/store.py index eeb9d3b..04b3399 100644 --- a/feeder/store.py +++ b/feeder/store.py @@ -17,7 +17,7 @@ class Store: if path: self.dbpath = path if self.connection is not None: - return self.connection + return log.debug("Connecting to %s", self.dbpath) self.connection = sqlite3.connect( self.dbpath, isolation_level=None @@ -30,6 +30,7 @@ class Store: conn.close() def init(self) -> None: + assert self.connection is not None conn = self.connection conn.execute( """ @@ -57,6 +58,7 @@ class Store: def sync_feeds(self, feeds: Dict[str, Feed]) -> None: """Write the current state of feeds to store, and load existing info back.""" + assert self.connection is not None conn = self.connection conn.executemany( """ @@ -117,6 +119,7 @@ class Store: select id, content, date, link, title from post where feed_id=? and id in ({qs}) """ + assert self.connection is not None conn = self.connection posts = [Post(*row) for row in conn.execute(sql, (feed_id, *post_ids))] for post in posts: diff --git a/hotdog/bot.py b/hotdog/bot.py index f6ac1b9..bfa3957 100644 --- a/hotdog/bot.py +++ b/hotdog/bot.py @@ -46,7 +46,7 @@ class Bot: self.message_handlers.append(callback) def on_command(self, command: Union[str, Container[str]], callback: MessageHandler): - commands = command = {command} if type(command) is str else command + commands = {command} if type(command) is str else command async def guard(message): if message.command not in commands: @@ -161,7 +161,7 @@ class Bot: for h, t in tasks.items(): assert t.done() try: - err = t.exception() + err = t.exception() # type: ignore except asyncio.CancelledError: log.error("Message handler took too long to finished: %s", h) if err is not None: diff --git a/hotdog/functions.py b/hotdog/functions.py index f2e59bf..8b96471 100644 --- a/hotdog/functions.py +++ b/hotdog/functions.py @@ -3,7 +3,7 @@ import logging import unicodedata from collections import defaultdict from contextlib import contextmanager -from dataclasses import dataclass, fields +from dataclasses import fields from datetime import datetime, timedelta, timezone from html import escape as html_escape from html.parser import HTMLParser @@ -311,7 +311,7 @@ class ElementParser(HTMLParser): break -def escape_all(dc: dataclass, escape: Callable[[str], str] = html_escape) -> None: +def escape_all(dc, escape: Callable[[str], str] = html_escape) -> None: """Patch a dataclass to escape all strings.""" for f in fields(dc): if f.type is str: diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..29d3878 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,5 @@ +[mypy] +ignore_missing_imports = True +pretty = True +python_version = 3.8 +platform = linux \ No newline at end of file diff --git a/postillon/store.py b/postillon/store.py index 2ff8338..08e5a1a 100644 --- a/postillon/store.py +++ b/postillon/store.py @@ -15,6 +15,7 @@ class Store: self.dbpath = path if self.connection is not None: return self.connection + assert self.dbpath is not None self.connection = sqlite3.connect( self.dbpath, isolation_level=None ) # auto commit @@ -45,6 +46,7 @@ class Store: ) def add(self, posts: Iterable[Post]): + assert self.connection is not None sql = f""" insert into post(content, source, date) values (?, ?, ?) @@ -59,6 +61,7 @@ class Store: ) def _select(self, condition="", params=[]) -> Iterable[Post]: + assert self.connection is not None sql = f"select id, content, date, source from post {condition}" for row in self.connection.execute(sql, params): id, content, date, source = row @@ -71,6 +74,7 @@ class Store: cond = "where id in (select id from post order by random() limit 1)" for post in self._select(cond): return post + return None def search(self, term, skip: int = 0) -> Iterable[Post]: cond = "where content like ? order by date desc limit -1 offset ?" diff --git a/scripts/lint b/scripts/lint index c0a1e9d..e382088 100755 --- a/scripts/lint +++ b/scripts/lint @@ -6,6 +6,7 @@ if [ "$1" = '--fix' ]; then black . isort --profile black . autoflake --in-place --recursive . + mypy . || : # ignore ) exit fi @@ -14,6 +15,7 @@ error=0 (set -x; black --check .) || error=$? (set -x; isort --profile black --check .) || error=$? -(set -x; autoflake --check --recursive .) || error=$? +(set -x; autoflake --check --recursive . | uniq) || error=$? +(set -x; mypy .) || : # ignore exit "$error"