Update linter dependencies and fix lints

Use t to rename f since it has the type for TextIOWrapper,
which is different from the original f being _TemporaryFileWrapper.
This commit is contained in:
JingMatrix 2023-06-17 09:44:29 +02:00
parent ba552df613
commit 9c34c88d4e
9 changed files with 29 additions and 28 deletions

View file

@ -14,10 +14,10 @@ python = "^3.8"
python-telegram = "0.18.0"
[tool.poetry.dev-dependencies]
black = "20.8b1"
flake8 = "3.8.4"
isort = "5.6.2"
mypy = "0.812"
black = "*"
flake8 = "*"
isort = "*"
mypy = "*"
[tool.poetry.scripts]
tg = "tg.__main__:main"

View file

@ -1,9 +1,10 @@
import logging.handlers
import signal
import threading
from curses import window, wrapper # type: ignore
from curses import window, wrapper
from functools import partial
from types import FrameType
from typing import Optional
from tg import config, update_handlers, utils
from tg.controllers import Controller
@ -15,9 +16,8 @@ log = logging.getLogger(__name__)
def run(tg: Tdlib, stdscr: window) -> None:
# handle ctrl+c, to avoid interrupting tg when subprocess is called
def interrupt_signal_handler(sig: int, frame: FrameType) -> None:
def interrupt_signal_handler(sig: int, frame: Optional[FrameType]) -> None:
# TODO: draw on status pane: to quite press <q>
log.info("Interrupt signal is handled and ignored on purpose.")

View file

@ -81,7 +81,7 @@ FILE_PICKER_CMD = "ranger --choosefile={file_path}"
DOWNLOAD_DIR = os.path.expanduser("~/Downloads/")
if os.path.isfile(CONFIG_FILE):
config_params = runpy.run_path(CONFIG_FILE) # type: ignore
config_params = runpy.run_path(CONFIG_FILE)
for param, value in config_params.items():
if param.isupper():
globals()[param] = value

View file

@ -5,6 +5,7 @@ from datetime import datetime
from functools import partial, wraps
from queue import Queue
from tempfile import NamedTemporaryFile
from types import FrameType
from typing import Any, Callable, Dict, List, Optional
from telegram.utils import AsyncResult
@ -303,8 +304,8 @@ class Controller:
f.write(insert_replied_msg(msg))
f.seek(0)
s.call(config.LONG_MSG_CMD.format(file_path=shlex.quote(f.name)))
with open(f.name) as f:
if replied_msg := strip_replied_msg(f.read().strip()):
with open(f.name) as t:
if replied_msg := strip_replied_msg(t.read().strip()):
self.model.view_all_msgs()
self.tg.reply_message(chat_id, reply_to_msg, replied_msg)
self.present_info("Message sent")
@ -336,8 +337,8 @@ class Controller:
) as s:
self.tg.send_chat_action(chat_id, ChatAction.chatActionTyping)
s.call(config.LONG_MSG_CMD.format(file_path=shlex.quote(f.name)))
with open(f.name) as f:
if msg := f.read().strip():
with open(f.name) as t:
if msg := t.read().strip():
self.model.send_message(text=msg)
self.present_info("Message sent")
else:
@ -364,8 +365,8 @@ class Controller:
try:
with NamedTemporaryFile("w") as f, suspend(self.view) as s:
s.call(config.FILE_PICKER_CMD.format(file_path=f.name))
with open(f.name) as f:
file_path = f.read().strip()
with open(f.name) as t:
file_path = t.read().strip()
except FileNotFoundError:
pass
if not file_path or not os.path.isfile(file_path):
@ -488,7 +489,7 @@ class Controller:
chat = self.model.chats.chats[self.model.current_chat]
return chat["permissions"]["can_send_messages"]
def _open_msg(self, msg: MsgProxy, cmd: str = None) -> None:
def _open_msg(self, msg: MsgProxy, cmd: Optional[str] = None) -> None:
if msg.is_text:
with NamedTemporaryFile("w", suffix=".txt") as f:
f.write(msg.text_content)
@ -544,8 +545,8 @@ class Controller:
f.write(msg.text_content)
f.flush()
s.call(f"{config.EDITOR} {f.name}")
with open(f.name) as f:
if text := f.read().strip():
with open(f.name) as t:
if text := t.read().strip():
self.model.edit_message(text=text)
self.present_info("Message edited")
@ -775,7 +776,7 @@ class Controller:
except Exception:
log.exception("Error happend in key handle loop")
def resize_handler(self, signum: int, frame: Any) -> None:
def resize_handler(self, signum: int, frame: Optional[FrameType]) -> None:
self.view.resize_handler()
self.resize()

View file

@ -403,7 +403,7 @@ class ChatModel:
"""
if self.have_full_chat_list:
return None
offset_order = 2 ** 63 - 1
offset_order = 2**63 - 1
offset_chat_id = 0
if len(self.chats):
offset_chat_id = self.chats[-1]["id"]
@ -655,7 +655,6 @@ User = namedtuple("User", ["id", "name", "status", "order"])
class UserModel:
types = {
"userTypeUnknown": "unknown",
"userTypeBot": "bot",

View file

@ -8,7 +8,6 @@ log = logging.getLogger(__name__)
class MsgProxy:
fields_mapping = {
"messageDocument": ("document", "document"),
"messageVoiceNote": ("voice_note", "voice"),

View file

@ -345,12 +345,12 @@ class Tdlib(Telegram):
return self._send_data(data)
def send_chat_action(
self, chat_id: int, action: ChatAction, progress: int = None
self, chat_id: int, action: ChatAction
) -> AsyncResult:
data = {
"@type": "sendChatAction",
"chat_id": chat_id,
"action": {"@type": action.name, "progress": progress},
"action": {"@type": action.name},
}
return self._send_data(data)

View file

@ -22,7 +22,7 @@ from typing import Any, Dict, Optional, Tuple, Type
from tg import config
log = logging.getLogger(__name__)
units = {"B": 1, "KB": 10 ** 3, "MB": 10 ** 6, "GB": 10 ** 9, "TB": 10 ** 12}
units = {"B": 1, "KB": 10**3, "MB": 10**6, "GB": 10**9, "TB": 10**12}
class LogWriter:
@ -85,7 +85,9 @@ def get_file_handler(file_path: str) -> str:
return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path))
caps = get_mailcap()
handler, view = mailcap.findmatch(caps, mtype, filename=shlex.quote(file_path))
handler, view = mailcap.findmatch(
caps, mtype, filename=shlex.quote(file_path)
)
if not handler:
return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path))
return handler
@ -230,7 +232,7 @@ class suspend:
if proc.returncode:
input(f"Command <{cmd}> failed: press <enter> to continue")
def open_file(self, file_path: str, cmd: str = None) -> None:
def open_file(self, file_path: str, cmd: Optional[str] = None) -> None:
if cmd:
cmd = cmd % shlex.quote(file_path)
else:

View file

@ -3,7 +3,7 @@ import logging
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from _curses import window # type: ignore
from _curses import window
from tg import config
from tg.colors import bold, cyan, get_color, magenta, reverse, white, yellow
@ -199,7 +199,7 @@ class ChatView:
self, current: int, chats: List[Dict[str, Any]], title: str = "Chats"
) -> None:
self.win.erase()
line = curses.ACS_VLINE # type: ignore
line = curses.ACS_VLINE
width = self.w - 1
self.win.vline(0, width, line, self.h)