Merge pull request #44 from paul-nameless/add-mypy

Add mypy to CI
This commit is contained in:
Nameless 2020-05-20 18:18:32 +08:00 committed by GitHub
commit afcc945416
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 22 deletions

View file

@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]
python-version: [3.8]
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
@ -30,11 +30,15 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install black
- name: Install tools
run: |
pip install black==19.10b0
pip install black==19.10b0 mypy==0.770
- name: Check formatting with black
run: |
black --check .
black --check tg
- name: Check types with mypy
run: |
mypy tg --warn-redundant-casts --warn-unused-ignores --no-warn-no-return --warn-unreachable --strict-equality --ignore-missing-imports

View file

@ -340,7 +340,6 @@ class Controller:
self.view.status.draw()
def refresh_msgs(self) -> None:
self.view.msgs.users = self.model.users
current_msg_idx = self.model.get_current_chat_msg_idx()
if current_msg_idx is None:
return

View file

@ -2,7 +2,7 @@ import logging
import logging.handlers
import signal
import threading
from curses import window, wrapper
from curses import window, wrapper # type: ignore
from functools import partial
from telegram.client import Telegram
@ -19,6 +19,7 @@ def run(tg: Telegram, stdscr: window) -> None:
# run this function in thread?
view = View(stdscr)
model = Model(tg)
view.msgs.users = model.users
controller = Controller(model, view, tg)
for msg_type, handler in controller.handlers.items():
tg.add_update_handler(msg_type, handler)

View file

@ -1,11 +1,9 @@
import logging
from collections import defaultdict
from typing import Any, Dict, List, Optional, Set, Union, Tuple
from typing import Any, Dict, List, Optional, Set, Tuple
from telegram.client import Telegram
from tg.msg import MsgProxy
log = logging.getLogger(__name__)
@ -325,8 +323,6 @@ class MsgModel:
log.info(f"message has been sent: {result.update}")
def delete_msg(self, chat_id: int) -> bool:
if chat_id is None:
return False
selected_msg = self.current_msgs[chat_id]
msg_item = self.msgs[chat_id].pop(selected_msg)
self.current_msgs[chat_id] = min(

View file

@ -1,10 +1,10 @@
import base64
import re
import curses
import logging
import math
import os
import random
import re
import shlex
import struct
import subprocess

View file

@ -1,10 +1,11 @@
import curses
import logging
from _curses import window
from _curses import window # type: ignore
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, cast
from tg.colors import blue, cyan, get_color, magenta, reverse, white
from tg.models import UserModel
from tg.msg import MsgProxy
from tg.utils import emoji_pattern, num, truncate_to_len
@ -62,7 +63,7 @@ class View:
):
break
return num(repeat_factor, default=1), keys or "UNKNOWN"
return cast(int, num(repeat_factor, default=1)), keys or "UNKNOWN"
class StatusView:
@ -88,7 +89,7 @@ class StatusView:
self.win.addstr(0, 0, msg[: self.w - 1])
self._refresh()
def get_input(self, msg="") -> Optional[str]:
def get_input(self, msg="") -> str:
self.draw(msg)
curses.curs_set(1)
@ -103,7 +104,7 @@ class StatusView:
if buff:
buff = buff[:-1]
elif key == 7: # ^G cancel
buff = None
buff = ""
break
elif chr(key).isprintable():
buff += chr(key)
@ -143,7 +144,7 @@ class ChatView:
return color | reverse
return color
def _chat_attributes(self, is_selected: bool = False) -> Tuple[int]:
def _chat_attributes(self, is_selected: bool = False) -> Tuple[int, ...]:
attrs = (
get_color(cyan, -1),
get_color(blue, -1),
@ -155,7 +156,8 @@ class ChatView:
def draw(self, current: int, chats: List[Dict[str, Any]]) -> None:
self.win.erase()
self.win.vline(0, self.w - 1, curses.ACS_VLINE, self.h)
line = curses.ACS_VLINE # type: ignore
self.win.vline(0, self.w - 1, line, self.h)
for i, chat in enumerate(chats):
is_selected = i == current
unread_count = chat["unread_count"]
@ -218,6 +220,9 @@ class ChatView:
class MsgView:
# FIXME: should be assigned from constructor
users: UserModel
def __init__(self, stdscr: window, p: float = 0.5) -> None:
self.stdscr = stdscr
self.h = 0
@ -247,7 +252,7 @@ class MsgView:
function will remove message one by one from the start until selected
message could be visible on the screen.
"""
selected_item_idx = None
selected_item_idx: Optional[int] = None
collected_items: List[Tuple[Tuple[str, ...], bool, int]] = []
for ignore_before in range(len(msgs)):
if selected_item_idx is not None:
@ -276,6 +281,7 @@ class MsgView:
if (
# ignore first and last msg
selected_item_idx not in (0, len(msgs) - 1, None)
and selected_item_idx is not None
and len(collected_items) - 1 - selected_item_idx
< min_msg_padding
):
@ -307,7 +313,7 @@ class MsgView:
self._refresh()
def _msg_attributes(self, is_selected: bool) -> Tuple[int]:
def _msg_attributes(self, is_selected: bool) -> Tuple[int, ...]:
attrs = (
get_color(cyan, -1),
get_color(blue, -1),
@ -315,7 +321,7 @@ class MsgView:
)
if is_selected:
return (attr | reverse for attr in attrs)
return tuple(attr | reverse for attr in attrs)
return attrs
def _get_user_by_id(self, user_id: int) -> str: