typing: fix a bunch of type errors

add more annotations
This commit is contained in:
mathieui 2021-04-11 19:35:16 +02:00
parent 0541bbb4bc
commit bd2aac6de2
4 changed files with 44 additions and 28 deletions

View file

@ -25,6 +25,7 @@ from typing import (
Tuple,
Type,
TypeVar,
Union,
)
from xml.etree import ElementTree as ET
@ -88,6 +89,17 @@ class Core:
custom_version: str
firstrun: bool
completion: CompletionCore
command: CommandCore
handler: HandlerCore
bookmarks: BookmarkList
status: Status
commands: Dict[str, Command]
room_number_jump: List[str]
initial_joins: List[JID]
pending_invites: Dict[str, str]
configuration_change_handlers: Dict[str, List[Callable[..., None]]]
own_nick: str
def __init__(self, custom_version: str, firstrun: bool):
self.completion = CompletionCore(self)
@ -101,7 +113,7 @@ class Core:
self.last_stream_error = None
self.stdscr = None
status = config.getstr('status')
status = POSSIBLE_SHOW.get(status, None)
status = POSSIBLE_SHOW.get(status) or ''
self.status = Status(show=status, message=config.getstr('status_message'))
self.running = True
self.custom_version = custom_version
@ -130,14 +142,13 @@ class Core:
self.events = events.EventHandler()
self.events.add_event_handler('tab_change', self.on_tab_change)
self.tabs = Tabs(self.events)
self.tabs = Tabs(self.events, tabs.GapTab())
self.previous_tab_nb = 0
own_nick = config.getstr('default_nick')
own_nick = own_nick or self.xmpp.boundjid.user
own_nick = own_nick or os.environ.get('USER')
own_nick = own_nick or 'poezio_user'
self.own_nick = own_nick
self.own_nick: str = (
config.getstr('default_nick') or self.xmpp.boundjid.user or
os.environ.get('USER') or 'poezio_user'
)
self.size = SizeManager(self)
@ -305,8 +316,6 @@ class Core:
self.initial_joins = []
self.connected_events = {}
self.pending_invites = {}
# a dict of the form {'config_option': [list, of, callbacks]}
@ -322,7 +331,7 @@ class Core:
# The callback takes two argument: the config option, and the new
# value
self.configuration_change_handlers = defaultdict(list)
config_handlers = [
config_handlers: List[Tuple[str, Callable[..., Any]]] = [
('', self.on_any_config_change),
('ack_message_receipts', self.on_ack_receipts_config_change),
('connection_check_interval', self.xmpp.set_keepalive_values),
@ -843,7 +852,7 @@ class Core:
or the default nickname
"""
bm = self.bookmarks[room_name]
if bm:
if bm and bm.nick:
return bm.nick
return self.own_nick
@ -993,10 +1002,8 @@ class Core:
### Tab getters ###
def get_tabs(self, cls: Type[T] = None) -> List[T]:
def get_tabs(self, cls: Type[T]) -> List[T]:
"Get all the tabs of a type"
if cls is None:
return self.tabs.get_tabs()
return self.tabs.by_class(cls)
def get_conversation_by_jid(self,
@ -1014,6 +1021,7 @@ class Core:
jid = JID(jid)
# We first check if we have a static conversation opened
# with this precise resource
conversation: Optional[tabs.ConversationTab]
conversation = self.tabs.by_name_and_class(jid.full,
tabs.StaticConversationTab)
if jid.bare == jid.full and not conversation:
@ -1303,7 +1311,7 @@ class Core:
tab.activate(reason=reason)
def on_user_changed_status_in_private(self, jid: JID, status: Status) -> None:
tab = self.tabs.by_name_and_class(jid, tabs.ChatTab)
tab = self.tabs.by_name_and_class(jid, tabs.OneToOneTab)
if tab is not None: # display the message in private
tab.update_status(status)

View file

@ -24,12 +24,13 @@ have become [0|1|2|3], with the tab "4" renumbered to "3" if gap tabs are
disabled.
"""
from typing import List, Dict, Type, Optional, Union, Tuple
from typing import List, Dict, Type, Optional, Union, Tuple, TypeVar, cast
from collections import defaultdict
from slixmpp import JID
from poezio import tabs
from poezio.events import EventHandler
from poezio.config import config
T = TypeVar('T', bound=tabs.Tab)
class Tabs:
@ -47,7 +48,7 @@ class Tabs:
'_events',
]
def __init__(self, events: EventHandler) -> None:
def __init__(self, events: EventHandler, initial_tab: tabs.Tab) -> None:
"""
Initialize the Tab List. Even though the list is initially
empty, all methods are only valid once append() has been called
@ -55,7 +56,7 @@ class Tabs:
"""
# cursor
self._current_index: int = 0
self._current_tab: Optional[tabs.Tab] = None
self._current_tab: tabs.Tab = initial_tab
self._previous_tab: Optional[tabs.Tab] = None
self._tabs: List[tabs.Tab] = []
@ -92,7 +93,7 @@ class Tabs:
return False
@property
def current_tab(self) -> Optional[tabs.Tab]:
def current_tab(self) -> tabs.Tab:
"""Current tab"""
return self._current_tab
@ -122,9 +123,9 @@ class Tabs:
"""Get a tab with a specific name"""
return self._tab_names.get(name)
def by_class(self, cls: Type[tabs.Tab]) -> List[tabs.Tab]:
def by_class(self, cls: Type[T]) -> List[T]:
"""Get all the tabs of a class"""
return self._tab_types.get(cls, [])
return cast(List[T], self._tab_types.get(cls, []))
def find_match(self, name: str) -> Optional[tabs.Tab]:
"""Get a tab using extended matching (tab.matching_name())"""
@ -171,12 +172,12 @@ class Tabs:
return any_matched, candidate
def by_name_and_class(self, name: str,
cls: Type[tabs.Tab]) -> Optional[tabs.Tab]:
cls: Type[T]) -> Optional[T]:
"""Get a tab with its name and class"""
cls_tabs = self._tab_types.get(cls, [])
for tab in cls_tabs:
if tab.name == name:
return tab
return cast(T, tab)
return None
def _rebuild(self):

View file

@ -13,9 +13,12 @@ from typing import (
List,
Optional,
TypeVar,
TYPE_CHECKING,
)
from poezio import common
if TYPE_CHECKING:
from poezio.core.core import Core
T = TypeVar('T', bound=Callable[..., Any])
@ -56,6 +59,8 @@ def wrap_generic(func: Callable, before: BeforeFunc = None, after: AfterFunc = N
class RefreshWrapper:
core: Optional[Core]
def __init__(self) -> None:
self.core = None

View file

@ -1507,10 +1507,12 @@ class MucTab(ChatTab):
self.leave_room(msg)
if config.getbool('synchronise_open_rooms'):
if self.jid in self.core.bookmarks:
self.core.bookmarks[self.jid].autojoin = False
asyncio.ensure_future(
self.core.bookmarks.save(self.core.xmpp)
)
bookmark = self.core.bookmarks[self.jid]
if bookmark:
bookmark.autojoin = False
asyncio.ensure_future(
self.core.bookmarks.save(self.core.xmpp)
)
self.core.close_tab(self)
def on_close(self) -> None: