summaryrefslogtreecommitdiff
path: root/poezio/core
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-03-14 22:31:22 +0100
committermathieui <mathieui@mathieui.net>2021-04-02 17:44:36 +0200
commit4b198be9771594a28824cc082e737fe15ab681ec (patch)
tree01de648647136734d176ba0fa33e96a61bbafc88 /poezio/core
parentbc4f4f1e0766aedb6b0e9f3df90fee9ea841786c (diff)
downloadpoezio-4b198be9771594a28824cc082e737fe15ab681ec.tar.gz
poezio-4b198be9771594a28824cc082e737fe15ab681ec.tar.bz2
poezio-4b198be9771594a28824cc082e737fe15ab681ec.tar.xz
poezio-4b198be9771594a28824cc082e737fe15ab681ec.zip
fix: tons of type errors
Diffstat (limited to 'poezio/core')
-rw-r--r--poezio/core/command_defs.py4
-rw-r--r--poezio/core/core.py35
-rw-r--r--poezio/core/structs.py10
3 files changed, 32 insertions, 17 deletions
diff --git a/poezio/core/command_defs.py b/poezio/core/command_defs.py
index fb5c21ee..e46b83a1 100644
--- a/poezio/core/command_defs.py
+++ b/poezio/core/command_defs.py
@@ -1,4 +1,4 @@
-from typing import Callable, List
+from typing import Callable, List, Optional
from poezio.core.commands import CommandCore
from poezio.core.completions import CompletionCore
@@ -22,7 +22,7 @@ CommandDict = TypedDict(
"shortdesc": str,
"desc": str,
"usage": str,
- "completion": Callable,
+ "completion": Optional[Callable],
},
total=False,
)
diff --git a/poezio/core/core.py b/poezio/core/core.py
index 93ea5c5b..91c9b3cc 100644
--- a/poezio/core/core.py
+++ b/poezio/core/core.py
@@ -16,6 +16,7 @@ import time
import uuid
from collections import defaultdict
from typing import (
+ Any,
cast,
Callable,
Dict,
@@ -233,9 +234,9 @@ class Core:
'_dnd': lambda: self.command.status('dnd'),
'_xa': lambda: self.command.status('xa'),
##### Custom actions ########
- '_exc_': self.try_execute,
}
self.key_func.update(key_func)
+ self.key_func.try_execute = self.try_execute
# Add handlers
xmpp_event_handlers = [
@@ -796,12 +797,14 @@ class Core:
def remove_timed_event(self, event: DelayedEvent) -> None:
"""Remove an existing timed event"""
- event.handler.cancel()
+ if event.handler is not None:
+ event.handler.cancel()
def add_timed_event(self, event: DelayedEvent) -> None:
"""Add a new timed event"""
event.handler = asyncio.get_event_loop().call_later(
- event.delay, event.callback, *event.args)
+ event.delay, event.callback, *event.args
+ )
####################### XMPP-related actions ##################################
@@ -1174,6 +1177,7 @@ class Core:
provided, we open a StaticConversationTab, else a
DynamicConversationTab
"""
+ new_tab: tabs.ConversationTab
if jid.resource:
new_tab = tabs.StaticConversationTab(self, jid)
else:
@@ -1196,19 +1200,19 @@ class Core:
self.tabs.set_current_tab(tab)
return tab
# create the new tab
- tab = self.tabs.by_name_and_class(room_name, tabs.MucTab)
- if not tab:
+ muc_tab = self.tabs.by_name_and_class(room_name, tabs.MucTab)
+ if not muc_tab:
return None
- new_tab = tabs.PrivateTab(self, complete_jid, tab.own_nick)
+ tab = tabs.PrivateTab(self, complete_jid, muc_tab.own_nick)
if hasattr(tab, 'directed_presence'):
- new_tab.directed_presence = tab.directed_presence
+ tab.directed_presence = tab.directed_presence
if not focus:
- new_tab.state = "private"
+ tab.state = "private"
# insert it in the tabs
- self.add_tab(new_tab, focus)
+ self.add_tab(tab, focus)
self.refresh_window()
- tab.privates.append(new_tab)
- return new_tab
+ muc_tab.privates.append(tab)
+ return tab
def open_new_room(self,
room: str,
@@ -1764,18 +1768,23 @@ class Core:
self.refresh_window()
-class KeyDict(dict):
+class KeyDict(Dict[str, Callable[[str], Any]]):
"""
A dict, with a wrapper for get() that will return a custom value
if the key starts with _exc_
"""
+ try_execute: Optional[Callable[[str], Any]]
def get(self, key: str, default: Optional[Callable] = None) -> Callable:
if isinstance(key, str) and key.startswith('_exc_') and len(key) > 5:
- return lambda: dict.get(self, '_exc_')(key[5:])
+ if self.try_execute is not None:
+ try_execute = self.try_execute
+ return lambda: try_execute(key[5:])
+ raise ValueError("KeyDict not initialized")
return dict.get(self, key, default)
+
def replace_key_with_bound(key: str) -> str:
"""
Replace an inputted key with the one defined as its replacement
diff --git a/poezio/core/structs.py b/poezio/core/structs.py
index e4d42551..31d31339 100644
--- a/poezio/core/structs.py
+++ b/poezio/core/structs.py
@@ -1,8 +1,12 @@
"""
Module defining structures useful to the core class and related methods
"""
+from __future__ import annotations
from dataclasses import dataclass
-from typing import Any, Callable, List, Dict
+from typing import Any, Callable, List, TYPE_CHECKING, Optional
+
+if TYPE_CHECKING:
+ from poezio import windows
__all__ = [
'Command',
@@ -34,6 +38,7 @@ class Completion:
A completion result essentially currying the input completion call.
"""
__slots__ = ['func', 'args', 'kwargs', 'comp_list']
+
def __init__(
self,
func: Callable[..., Any],
@@ -49,11 +54,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]
+ comp: Optional[Callable[['windows.Input'], Completion]]
short_desc: str
usage: str