From 23409165707ed463538019727315309ad236a2f5 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sun, 9 Jun 2024 20:01:44 +0800 Subject: [PATCH] remove some TYPE_CHECKING --- ica-rs/ica_typing.py | 476 +++++++++++++++++++------------------ ica-rs/plugins/base.py | 12 +- ica-rs/plugins/namerena.py | 11 +- 3 files changed, 250 insertions(+), 249 deletions(-) diff --git a/ica-rs/ica_typing.py b/ica-rs/ica_typing.py index b9998ae..c087c51 100644 --- a/ica-rs/ica_typing.py +++ b/ica-rs/ica_typing.py @@ -1,6 +1,6 @@ # Python 兼容版本 3.8+ -from typing import Callable, Tuple +from typing import Callable, Tuple, NewType, TYPE_CHECKING, TypeVar """ ica.rs @@ -9,9 +9,9 @@ pub type UserId = i64; pub type MessageId = String; """ class IcaType: - RoomId = int - UserId = int - MessageId = str + RoomId = NewType('RoomId', int) + UserId = NewType('UserId', int) + MessageId = NewType('MessageId', str) """ tailchat.rs @@ -21,248 +21,266 @@ pub type UserId = String; pub type MessageId = String; """ class TailchatType: - GroupId = str - ConverseId = str - UserId = str - MessageId = str + GroupId = NewType('GroupId', str) + ConverseId = NewType('ConverseId', str) + UserId = NewType('UserId', str) + MessageId = NewType('MessageId', str) -class IcaStatus: - """ - ica状态信息 - 此类并不存储信息, 所有方法都是实时获取 - """ - @property - def qq_login(self) -> bool: - ... - @property - def online(self) -> bool: - ... - @property - def self_id(self) -> IcaType.UserId: - ... - @property - def nick_name(self) -> str: - ... - @property - def ica_version(self) -> str: - ... - @property - def os_info(self) -> str: - ... - @property - def resident_set_size(self) -> str: - ... - @property - def head_used(self) -> str: - ... - @property - def load(self) -> str: - ... +if TYPE_CHECKING: - -class IcaReplyMessage: - ... - - -class IcaSendMessage: - @property - def content(self) -> str: - ... - @content.setter - def content(self, value: str) -> None: - ... - def with_content(self, content: str) -> "IcaSendMessage": + class IcaStatus: """ - 为了链式调用, 返回自身 + ica状态信息 + 此类并不存储信息, 所有方法都是实时获取 """ - self.content = content - return self - def set_img(self, file: bytes, file_type: str, as_sticker: bool): + @property + def qq_login(self) -> bool: + ... + @property + def online(self) -> bool: + ... + @property + def self_id(self) -> IcaType.UserId: + ... + @property + def nick_name(self) -> str: + ... + @property + def ica_version(self) -> str: + ... + @property + def os_info(self) -> str: + ... + @property + def resident_set_size(self) -> str: + ... + @property + def head_used(self) -> str: + ... + @property + def load(self) -> str: + ... + + + class IcaReplyMessage: + ... + + + class IcaSendMessage: + @property + def content(self) -> str: + ... + @content.setter + def content(self, value: str) -> None: + ... + def with_content(self, content: str) -> "IcaSendMessage": + """ + 为了链式调用, 返回自身 + """ + self.content = content + return self + def set_img(self, file: bytes, file_type: str, as_sticker: bool): + """ + 设置消息的图片 + @param file: 图片文件 (实际上是 vec) + @param file_type: 图片类型 (MIME) (image/png; image/jpeg) + @param as_sticker: 是否作为贴纸发送 + """ + + + class IcaDeleteMessage: + def __str__(self) -> str: + ... + + + class IcaNewMessage: """ - 设置消息的图片 - @param file: 图片文件 (实际上是 vec) - @param file_type: 图片类型 (MIME) (image/png; image/jpeg) - @param as_sticker: 是否作为贴纸发送 + Icalingua 接收到新消息 """ + def reply_with(self, message: str) -> IcaSendMessage: + """回复这条消息""" + ... + def as_deleted(self) -> IcaDeleteMessage: + ... + def __str__(self) -> str: + ... + @property + def id(self) -> IcaType.MessageId: + ... + @property + def content(self) -> str: + ... + @property + def sender_id(self) -> IcaType.UserId: + ... + @property + def is_from_self(self) -> bool: + ... + @property + def is_reply(self) -> bool: + ... + @property + def is_room_msg(self) -> bool: + """是否是群聊消息""" + ... + @property + def is_chat_msg(self) -> bool: + """是否是私聊消息""" + ... + @property + def room_id(self) -> IcaType.RoomId: + """ + 如果是群聊消息, 返回 (-群号) + 如果是私聊消息, 返回 对面qq + """ + ... -class IcaDeleteMessage: - def __str__(self) -> str: - ... - - -class IcaNewMessage: - """ - Icalingua 接收到新消息 - """ - def reply_with(self, message: str) -> IcaSendMessage: - """回复这条消息""" - ... - def as_deleted(self) -> IcaDeleteMessage: - ... - def __str__(self) -> str: - ... - @property - def id(self) -> IcaType.MessageId: - ... - @property - def content(self) -> str: - ... - @property - def sender_id(self) -> IcaType.UserId: - ... - @property - def is_from_self(self) -> bool: - ... - @property - def is_reply(self) -> bool: - ... - @property - def is_room_msg(self) -> bool: - """是否是群聊消息""" - ... - @property - def is_chat_msg(self) -> bool: - """是否是私聊消息""" - ... - @property - def room_id(self) -> IcaType.RoomId: + class IcaClient: """ - 如果是群聊消息, 返回 (-群号) - 如果是私聊消息, 返回 对面qq + Icalingua 的客户端 """ - ... + # @staticmethod + # async def send_message_a(client: "IcaClient", message: SendMessage) -> bool: + # """ + # 仅作占位, 不能使用 + # (因为目前来说, rust调用 Python端没法启动一个异步运行时 + # 所以只能 tokio::task::block_in_place 转换成同步调用) + # """ + def send_message(self, message: IcaSendMessage) -> bool: + ... + def send_and_warn(self, message: IcaSendMessage) -> bool: + """发送消息, 并在日志中输出警告信息""" + self.warn(message.content) + return self.send_message(message) + def delete_message(self, message: IcaDeleteMessage) -> bool: + ... + + @property + def status(self) -> IcaStatus: + ... + @property + def version(self) -> str: + ... + @property + def ica_version(self) -> str: + """shenbot ica 的版本号""" + ... + def debug(self, message: str) -> None: + """向日志中输出调试信息""" + ... + def info(self, message: str) -> None: + """向日志中输出信息""" + ... + def warn(self, message: str) -> None: + """向日志中输出警告信息""" + ... -class IcaClient: - """ - Icalingua 的客户端 - """ - # @staticmethod - # async def send_message_a(client: "IcaClient", message: SendMessage) -> bool: - # """ - # 仅作占位, 不能使用 - # (因为目前来说, rust调用 Python端没法启动一个异步运行时 - # 所以只能 tokio::task::block_in_place 转换成同步调用) - # """ - def send_message(self, message: IcaSendMessage) -> bool: - ... - def send_and_warn(self, message: IcaSendMessage) -> bool: - """发送消息, 并在日志中输出警告信息""" - self.warn(message.content) - return self.send_message(message) - def delete_message(self, message: IcaDeleteMessage) -> bool: - ... - - @property - def status(self) -> IcaStatus: - ... - @property - def version(self) -> str: - ... - @property - def ica_version(self) -> str: - """shenbot ica 的版本号""" - ... - def debug(self, message: str) -> None: - """向日志中输出调试信息""" - ... - def info(self, message: str) -> None: - """向日志中输出信息""" - ... - def warn(self, message: str) -> None: - """向日志中输出警告信息""" - ... - - -class TailchatReciveMessage: - """ - Tailchat 接收到的新消息 - """ - @property - def id(self) -> TailchatType.MessageId: - ... - @property - def content(self) -> str: - ... - @property - def sender_id(self) -> TailchatType.UserId: - ... - # @property - # def is_from_self(self) -> bool: - # ... - @property - def is_reply(self) -> bool: - ... - @property - def group_id(self) -> TailchatType.GroupId: - ... - @property - def converse_id(self) -> TailchatType.ConverseId: - ... - def reply_with(self, message: str) -> "TailchatSendingMessage": - """回复这条消息""" - ... - def as_reply(self, message: str) -> "TailchatSendingMessage": - """回复这条消息""" - ... - - -class TailchatSendingMessage: - """ - Tailchat 将要发送的信息 - """ - @property - def content(self) -> str: - ... - @content.setter - def content(self, value: str) -> None: - ... - def with_content(self, content: str) -> "TailchatSendingMessage": + class TailchatReciveMessage: """ - 为了链式调用, 返回自身 + Tailchat 接收到的新消息 """ - self.content = content - return self - # def set_img(self, file: bytes, file_type: str, as_sticker: bool): - # """ - # 设置消息的图片 - # @param file: 图片文件 (实际上是 vec) - # @param file_type: 图片类型 (MIME) (image/png; image/jpeg) - # @param as_sticker: 是否作为贴纸发送 - # """ + @property + def id(self) -> TailchatType.MessageId: + ... + @property + def content(self) -> str: + ... + @property + def sender_id(self) -> TailchatType.UserId: + ... + # @property + # def is_from_self(self) -> bool: + # ... + @property + def is_reply(self) -> bool: + ... + @property + def group_id(self) -> TailchatType.GroupId: + ... + @property + def converse_id(self) -> TailchatType.ConverseId: + ... + def reply_with(self, message: str) -> "TailchatSendingMessage": + """回复这条消息""" + ... + def as_reply(self, message: str) -> "TailchatSendingMessage": + """回复这条消息""" + ... -class TailchatClient: + class TailchatSendingMessage: + """ + Tailchat 将要发送的信息 + """ + @property + def content(self) -> str: + ... + @content.setter + def content(self, value: str) -> None: + ... + def with_content(self, content: str) -> "TailchatSendingMessage": + """ + 为了链式调用, 返回自身 + """ + self.content = content + return self + # def set_img(self, file: bytes, file_type: str, as_sticker: bool): + # """ + # 设置消息的图片 + # @param file: 图片文件 (实际上是 vec) + # @param file_type: 图片类型 (MIME) (image/png; image/jpeg) + # @param as_sticker: 是否作为贴纸发送 + # """ + + + class TailchatClient: + """ + Tailchat 的客户端 + """ + def send_message(self, message: TailchatSendingMessage) -> bool: + ... + def send_and_warn(self, message: TailchatSendingMessage) -> bool: + """发送消息, 并在日志中输出警告信息""" + self.warn(message.content) + return self.send_message(message) + @property + def version(self) -> str: + ... + @property + def tailchat_version(self) -> str: + """tailchat 的版本号""" + ... + def debug(self, message: str) -> None: + """向日志中输出调试信息""" + def info(self, message: str) -> None: + """向日志中输出信息""" + def warn(self, message: str) -> None: + """向日志中输出警告信息""" + + + class ConfigData: + def __getitem__(self, key: str): + ... + def have_key(self, key: str) -> bool: + ... + + CONFIG_DATA: ConfigData = ConfigData() +else: """ - Tailchat 的客户端 + 正常 Import 的时候使用的类型定义 """ - def send_message(self, message: TailchatSendingMessage) -> bool: - ... - def send_and_warn(self, message: TailchatSendingMessage) -> bool: - """发送消息, 并在日志中输出警告信息""" - self.warn(message.content) - return self.send_message(message) - @property - def version(self) -> str: - ... - @property - def tailchat_version(self) -> str: - """tailchat 的版本号""" - ... - def debug(self, message: str) -> None: - """向日志中输出调试信息""" - def info(self, message: str) -> None: - """向日志中输出信息""" - def warn(self, message: str) -> None: - """向日志中输出警告信息""" - - -class ConfigData: - def __getitem__(self, key: str): - ... - def have_key(self, key: str) -> bool: - ... + IcaStatus = TypeVar("IcaStatus") + IcaReplyMessage = TypeVar("IcaReplyMessage") + IcaNewMessage = TypeVar("IcaNewMessage") + IcaSendMessage = TypeVar("IcaSendMessage") + IcaDeleteMessage = TypeVar("IcaDeleteMessage") + IcaClient = TypeVar("IcaClient") + TailchatReciveMessage = TypeVar("TailchatReciveMessage") + TailchatSendingMessage = TypeVar("TailchatSendingMessage") + TailchatClient = TypeVar("TailchatClient") + ConfigData = TypeVar("ConfigData") on_load = Callable[[IcaClient], None] @@ -282,5 +300,3 @@ on_tailchat_message = Callable[[TailchatClient, TailchatReciveMessage], None] # ... on_config = Callable[[None], Tuple[str, str]] - -CONFIG_DATA: ConfigData = ConfigData() diff --git a/ica-rs/plugins/base.py b/ica-rs/plugins/base.py index 952d456..ef85ddb 100644 --- a/ica-rs/plugins/base.py +++ b/ica-rs/plugins/base.py @@ -1,13 +1,5 @@ -from typing import TYPE_CHECKING, TypeVar - -if TYPE_CHECKING: - from ica_typing import IcaNewMessage, IcaClient - from ica_typing import TailchatReciveMessage, TailchatClient -else: - IcaNewMessage = TypeVar("NewMessage") - IcaClient = TypeVar("IcaClient") - TailchatReciveMessage = TypeVar("TailchatReciveMessage") - TailchatClient = TypeVar("TailchatClient") +from ica_typing import IcaNewMessage, IcaClient +from ica_typing import TailchatReciveMessage, TailchatClient def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: if not (msg.is_from_self or msg.is_reply): diff --git a/ica-rs/plugins/namerena.py b/ica-rs/plugins/namerena.py index e34a1d2..9afa8d1 100644 --- a/ica-rs/plugins/namerena.py +++ b/ica-rs/plugins/namerena.py @@ -4,16 +4,9 @@ import subprocess from pathlib import Path -from typing import TYPE_CHECKING, TypeVar +from ica_typing import IcaNewMessage, IcaClient, ConfigData -if TYPE_CHECKING: - from ica_typing import IcaNewMessage, IcaClient, ConfigData - - CONFIG_DATA: ConfigData -else: - CONFIG_DATA = None # type: ignore - IcaNewMessage = TypeVar("NewMessage") - IcaClient = TypeVar("IcaClient") +CONFIG_DATA: ConfigData _version_ = "0.4.2"