From f631728537946e9e746f35383c2f346656e81ee5 Mon Sep 17 00:00:00 2001 From: Stone Date: Sun, 19 Jan 2025 10:17:52 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E6=9B=B4=E6=96=B0=20Ruff=20=E5=B9=B6?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=A0=BC=E5=BC=8F=E5=8C=96=20(#160)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alicebot/adapter/utils.py | 6 +++--- alicebot/bot.py | 2 +- alicebot/config.py | 6 +++--- alicebot/exceptions.py | 8 ++++---- alicebot/message.py | 4 ++-- alicebot/typing.py | 18 ++++++++++-------- alicebot/utils.py | 8 ++++---- examples/plugins/global_state_test1.py | 2 +- examples/plugins/global_state_test2.py | 2 +- .../alicebot/adapter/cqhttp/exceptions.py | 4 ++-- .../alicebot/adapter/cqhttp/message.py | 2 +- .../alicebot/adapter/dingtalk/event.py | 2 +- .../alicebot/adapter/mirai/exceptions.py | 2 +- .../alicebot/adapter/onebot/exceptions.py | 2 +- .../alicebot/adapter/telegram/__init__.py | 2 +- .../alicebot/adapter/telegram/exceptions.py | 2 +- packages/alicebot-adapter-telegram/codegen.py | 2 +- tests/test_dependencies.py | 12 ++++++------ 18 files changed, 44 insertions(+), 42 deletions(-) diff --git a/alicebot/adapter/utils.py b/alicebot/adapter/utils.py index e00d075..9839c2e 100644 --- a/alicebot/adapter/utils.py +++ b/alicebot/adapter/utils.py @@ -17,12 +17,12 @@ from alicebot.typing import ConfigT, EventT __all__ = [ - "PollingAdapter", "HttpClientAdapter", - "WebSocketClientAdapter", "HttpServerAdapter", - "WebSocketServerAdapter", + "PollingAdapter", "WebSocketAdapter", + "WebSocketClientAdapter", + "WebSocketServerAdapter", ] logger = structlog.stdlib.get_logger() diff --git a/alicebot/bot.py b/alicebot/bot.py index 85adf1f..4552b24 100644 --- a/alicebot/bot.py +++ b/alicebot/bot.py @@ -709,7 +709,7 @@ def _load_plugins_from_module_name( else: for plugin_class, module in plugin_classes: self._load_plugin_class( - plugin_class, # type: ignore + plugin_class, # type: ignore[type-abstract] plugin_load_type, module.__file__, ) diff --git a/alicebot/config.py b/alicebot/config.py index 85c9d36..4c78e2c 100644 --- a/alicebot/config.py +++ b/alicebot/config.py @@ -8,12 +8,12 @@ from pydantic import BaseModel, ConfigDict, DirectoryPath, Field __all__ = [ + "AdapterConfig", + "BotConfig", "ConfigModel", "LogConfig", - "BotConfig", - "PluginConfig", - "AdapterConfig", "MainConfig", + "PluginConfig", ] diff --git a/alicebot/exceptions.py b/alicebot/exceptions.py index b706b6c..a0c1dfc 100644 --- a/alicebot/exceptions.py +++ b/alicebot/exceptions.py @@ -5,13 +5,13 @@ """ __all__ = [ - "EventException", - "SkipException", - "StopException", + "AdapterException", "AliceBotException", + "EventException", "GetEventTimeout", - "AdapterException", "LoadModuleError", + "SkipException", + "StopException", ] diff --git a/alicebot/message.py b/alicebot/message.py index 3ceabc6..413cc00 100644 --- a/alicebot/message.py +++ b/alicebot/message.py @@ -22,11 +22,11 @@ from pydantic_core import core_schema __all__ = [ - "MessageT", - "MessageSegmentT", "BuildMessageType", "Message", "MessageSegment", + "MessageSegmentT", + "MessageT", ] MessageT = TypeVar("MessageT", bound="Message[Any]") diff --git a/alicebot/typing.py b/alicebot/typing.py index 3a30a99..0a7f1cd 100644 --- a/alicebot/typing.py +++ b/alicebot/typing.py @@ -3,6 +3,8 @@ 此模块定义了部分 AliceBot 使用的类型。 """ +# ruff: noqa: A005 + from collections.abc import Awaitable from typing import TYPE_CHECKING, Callable, Optional, TypeVar @@ -18,17 +20,17 @@ from alicebot.plugin import Plugin __all__ = [ - "StateT", - "EventT", - "PluginT", + "AdapterHook", "AdapterT", - "ConfigT", - "MessageT", - "MessageSegmentT", - "BuildMessageType", "BotHook", - "AdapterHook", + "BuildMessageType", + "ConfigT", "EventHook", + "EventT", + "MessageSegmentT", + "MessageT", + "PluginT", + "StateT", ] StateT = TypeVar("StateT") diff --git a/alicebot/utils.py b/alicebot/utils.py index c313c9f..f9deba4 100644 --- a/alicebot/utils.py +++ b/alicebot/utils.py @@ -41,15 +41,15 @@ __all__ = [ "ModulePathFinder", - "is_config_class", + "PydanticEncoder", + "get_annotations", "get_classes_from_module", "get_classes_from_module_name", - "PydanticEncoder", + "is_config_class", "samefile", - "sync_func_wrapper", "sync_ctx_manager_wrapper", + "sync_func_wrapper", "wrap_get_func", - "get_annotations", ] _T = TypeVar("_T") diff --git a/examples/plugins/global_state_test1.py b/examples/plugins/global_state_test1.py index edb88e2..8adb45a 100644 --- a/examples/plugins/global_state_test1.py +++ b/examples/plugins/global_state_test1.py @@ -6,7 +6,7 @@ async def handle(self) -> None: if self.bot.global_state.get("count") is None: self.bot.global_state["count"] = 0 self.bot.global_state["count"] += 1 - await self.event.reply(f'add: {self.bot.global_state["count"]}') + await self.event.reply(f"add: {self.bot.global_state['count']}") async def rule(self) -> bool: if self.event.adapter.name != "cqhttp": diff --git a/examples/plugins/global_state_test2.py b/examples/plugins/global_state_test2.py index 6f19981..0e4f3b4 100644 --- a/examples/plugins/global_state_test2.py +++ b/examples/plugins/global_state_test2.py @@ -6,7 +6,7 @@ async def handle(self) -> None: if self.bot.global_state.get("count") is None: self.bot.global_state["count"] = 0 self.bot.global_state["count"] -= 1 - await self.event.reply(f'sub: {self.bot.global_state["count"]}') + await self.event.reply(f"sub: {self.bot.global_state['count']}") async def rule(self) -> bool: if self.event.adapter.name != "cqhttp": diff --git a/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/exceptions.py b/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/exceptions.py index 20b2d6b..91a3e2c 100644 --- a/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/exceptions.py +++ b/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/exceptions.py @@ -5,11 +5,11 @@ from alicebot.exceptions import AdapterException __all__ = [ - "CQHTTPException", - "NetworkError", "ActionFailed", "ApiNotAvailable", "ApiTimeout", + "CQHTTPException", + "NetworkError", ] diff --git a/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/message.py b/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/message.py index ec70c0a..de23082 100644 --- a/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/message.py +++ b/packages/alicebot-adapter-cqhttp/alicebot/adapter/cqhttp/message.py @@ -48,7 +48,7 @@ def get_cqcode(self) -> str: params = ",".join( [f"{k}={escape(str(v))}" for k, v in self.data.items() if v is not None] ) - return f'[CQ:{self.type}{"," if params else ""}{params}]' + return f"[CQ:{self.type}{',' if params else ''}{params}]" @classmethod def text(cls, text: str) -> Self: diff --git a/packages/alicebot-adapter-dingtalk/alicebot/adapter/dingtalk/event.py b/packages/alicebot-adapter-dingtalk/alicebot/adapter/dingtalk/event.py index caf0c8d..e05c165 100644 --- a/packages/alicebot-adapter-dingtalk/alicebot/adapter/dingtalk/event.py +++ b/packages/alicebot-adapter-dingtalk/alicebot/adapter/dingtalk/event.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: from . import DingTalkAdapter -__all__ = ["UserInfo", "Text", "DingTalkEvent"] +__all__ = ["DingTalkEvent", "Text", "UserInfo"] class UserInfo(BaseModel): diff --git a/packages/alicebot-adapter-mirai/alicebot/adapter/mirai/exceptions.py b/packages/alicebot-adapter-mirai/alicebot/adapter/mirai/exceptions.py index 3302c36..7ad6f60 100644 --- a/packages/alicebot-adapter-mirai/alicebot/adapter/mirai/exceptions.py +++ b/packages/alicebot-adapter-mirai/alicebot/adapter/mirai/exceptions.py @@ -4,7 +4,7 @@ from alicebot.exceptions import AdapterException -__all__ = ["MiraiException", "NetworkError", "ActionFailed", "ApiTimeout"] +__all__ = ["ActionFailed", "ApiTimeout", "MiraiException", "NetworkError"] class MiraiException(AdapterException): diff --git a/packages/alicebot-adapter-onebot/alicebot/adapter/onebot/exceptions.py b/packages/alicebot-adapter-onebot/alicebot/adapter/onebot/exceptions.py index 865b787..ceea5d2 100644 --- a/packages/alicebot-adapter-onebot/alicebot/adapter/onebot/exceptions.py +++ b/packages/alicebot-adapter-onebot/alicebot/adapter/onebot/exceptions.py @@ -4,7 +4,7 @@ from alicebot.exceptions import AdapterException -__all__ = ["OneBotException", "NetworkError", "ActionFailed", "ApiTimeout"] +__all__ = ["ActionFailed", "ApiTimeout", "NetworkError", "OneBotException"] class OneBotException(AdapterException): diff --git a/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/__init__.py b/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/__init__.py index 0ee0880..40e18aa 100644 --- a/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/__init__.py +++ b/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/__init__.py @@ -218,7 +218,7 @@ async def call_api( if response_type is None: return_type_adapter = TypeAdapter(Response[Any]) else: - return_type_adapter = TypeAdapter(Response[response_type]) # type: ignore + return_type_adapter = TypeAdapter(Response[response_type]) # type: ignore[valid-type] data = self._format_telegram_api_params(**params) if isinstance(data, aiohttp.FormData): diff --git a/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/exceptions.py b/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/exceptions.py index 2d9be36..077dcdf 100644 --- a/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/exceptions.py +++ b/packages/alicebot-adapter-telegram/alicebot/adapter/telegram/exceptions.py @@ -4,7 +4,7 @@ from alicebot.exceptions import AdapterException -__all__ = ["TelegramException", "NetworkError", "ActionFailed"] +__all__ = ["ActionFailed", "NetworkError", "TelegramException"] class TelegramException(AdapterException): diff --git a/packages/alicebot-adapter-telegram/codegen.py b/packages/alicebot-adapter-telegram/codegen.py index dfa8ade..f4eb46a 100644 --- a/packages/alicebot-adapter-telegram/codegen.py +++ b/packages/alicebot-adapter-telegram/codegen.py @@ -317,7 +317,7 @@ def gen_event(self) -> str: result = "\n\n".join( f""" class {snake_to_camel_case(field.name)}Event(TelegramEvent): - \"\"\"{field.description.removeprefix('Optional. ').split('. ')[0] + '.'}\"\"\" + \"\"\"{field.description.removeprefix("Optional. ").split(". ")[0] + "."}\"\"\" __event_type__ = "{field.name}" diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 15f14cd..0eacb79 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -82,9 +82,9 @@ def __enter__(self) -> Self: def __exit__( self, - __exc_type: Optional[type[BaseException]], - __exc_value: Optional[BaseException], - __traceback: Optional[TracebackType], + _exc_type: Optional[type[BaseException]], + _exc_value: Optional[BaseException], + _traceback: Optional[TracebackType], ) -> None: nonlocal exit_flag exit_flag = True @@ -125,9 +125,9 @@ async def __aenter__(self) -> Self: async def __aexit__( self, - __exc_type: Optional[type[BaseException]], - __exc_value: Optional[BaseException], - __traceback: Optional[TracebackType], + _exc_type: Optional[type[BaseException]], + _exc_value: Optional[BaseException], + _traceback: Optional[TracebackType], ) -> None: nonlocal exit_flag exit_flag = True