From c9e219c1405604f2ef81b3e446cc33a0492af42d Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 28 May 2020 21:31:18 +0200 Subject: mypy: Reduce errors on muctab.py by a lot --- poezio/core/core.py | 29 +++++++++++++++++++++-------- poezio/core/structs.py | 38 +++++++++++++++++++++----------------- poezio/core/tabs.py | 8 ++++---- 3 files changed, 46 insertions(+), 29 deletions(-) (limited to 'poezio/core') diff --git a/poezio/core/core.py b/poezio/core/core.py index 3ad15719..3a13d4c3 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -15,7 +15,17 @@ import shutil import time import uuid from collections import defaultdict -from typing import Callable, Dict, List, Optional, Set, Tuple, Type +from typing import ( + cast, + Callable, + Dict, + List, + Optional, + Set, + Tuple, + Type, + TypeVar, +) from xml.etree import ElementTree as ET from functools import partial @@ -65,6 +75,7 @@ from poezio.ui.types import Message, InfoMessage log = logging.getLogger(__name__) +T = TypeVar('T', bound=tabs.Tab) class Core: """ @@ -99,8 +110,10 @@ class Core: # that are displayed in almost all tabs, in an # information window. self.information_buffer = TextBuffer() - self.information_win_size = config.get( - 'info_win_height', section='var') + self.information_win_size = cast( + int, + config.get('info_win_height', section='var'), + ) self.information_win = windows.TextWin(300) self.information_buffer.add_window(self.information_win) self.left_tab_win = None @@ -813,7 +826,7 @@ class Core: ####################### XMPP-related actions ################################## - def get_status(self) -> str: + def get_status(self) -> Status: """ Get the last status that was previously set """ @@ -1016,7 +1029,7 @@ class Core: ### Tab getters ### - def get_tabs(self, cls: Type[tabs.Tab] = None) -> List[tabs.Tab]: + def get_tabs(self, cls: Type[T] = None) -> List[T]: "Get all the tabs of a type" if cls is None: return self.tabs.get_tabs() @@ -1324,7 +1337,7 @@ class Core: if tab.name.startswith(room_name): tab.activate(reason=reason) - def on_user_changed_status_in_private(self, jid: JID, status: str) -> None: + def on_user_changed_status_in_private(self, jid: JID, status: Status) -> None: tab = self.tabs.by_name_and_class(jid, tabs.ChatTab) if tab is not None: # display the message in private tab.update_status(status) @@ -1652,7 +1665,7 @@ class Core: return else: scr = self.stdscr - tabs.Tab.resize(scr) + tabs.Tab.initial_resize(scr) self.resize_global_info_bar() self.resize_global_information_win() for tab in self.tabs: @@ -2105,7 +2118,7 @@ class Core: self.bookmarks.get_remote(self.xmpp, self.information, _join_remote_only) - def room_error(self, error, room_name): + def room_error(self, error: IqError, room_name: str) -> None: """ Display the error in the tab """ diff --git a/poezio/core/structs.py b/poezio/core/structs.py index 72c9628a..a75f1e94 100644 --- a/poezio/core/structs.py +++ b/poezio/core/structs.py @@ -1,6 +1,8 @@ """ Module defining structures useful to the core class and related methods """ +from dataclasses import dataclass +from typing import Any, Callable, List, Dict __all__ = [ 'ERROR_AND_STATUS_CODES', 'DEPRECATED_ERRORS', 'POSSIBLE_SHOW', 'Status', @@ -51,23 +53,11 @@ POSSIBLE_SHOW = { } +@dataclass class Status: __slots__ = ('show', 'message') - - def __init__(self, show, message): - self.show = show - self.message = message - - -class Command: - __slots__ = ('func', 'desc', 'comp', 'short_desc', 'usage') - - def __init__(self, func, desc, comp, short_desc, usage): - self.func = func - self.desc = desc - self.comp = comp - self.short_desc = short_desc - self.usage = usage + show: str + message: str class Completion: @@ -75,8 +65,13 @@ class Completion: A completion result essentially currying the input completion call. """ __slots__ = ['func', 'args', 'kwargs', 'comp_list'] - - def __init__(self, func, comp_list, *args, **kwargs): + def __init__( + self, + func: Callable[..., Any], + comp_list: List[str], + *args: Any, + **kwargs: Any + ) -> None: self.func = func self.comp_list = comp_list self.args = args @@ -84,3 +79,12 @@ class Completion: def run(self): return self.func(self.comp_list, *self.args, **self.kwargs) + +@dataclass +class Command: + __slots__ = ('func', 'desc', 'comp', 'short_desc', 'usage') + func: Callable[..., Any] + desc: str + comp: Callable[['windows.Input'], Completion] + short_desc: str + usage: str diff --git a/poezio/core/tabs.py b/poezio/core/tabs.py index d5909d39..61bad6f2 100644 --- a/poezio/core/tabs.py +++ b/poezio/core/tabs.py @@ -347,16 +347,16 @@ class Tabs: if new_pos < len(self._tabs): old_tab = self._tabs[old_pos] self._tabs[new_pos], self._tabs[ - old_pos] = old_tab, tabs.GapTab(self) + old_pos] = old_tab, tabs.GapTab(None) else: self._tabs.append(self._tabs[old_pos]) - self._tabs[old_pos] = tabs.GapTab(self) + self._tabs[old_pos] = tabs.GapTab(None) else: if new_pos > old_pos: self._tabs.insert(new_pos, tab) - self._tabs[old_pos] = tabs.GapTab(self) + self._tabs[old_pos] = tabs.GapTab(None) elif new_pos < old_pos: - self._tabs[old_pos] = tabs.GapTab(self) + self._tabs[old_pos] = tabs.GapTab(None) self._tabs.insert(new_pos, tab) else: return False -- cgit v1.2.3