mirror of
https://github.com/AstrBotDevs/AstrBot.git
synced 2026-09-24 16:39:52 +08:00
style: apply ruff --unsafe-fixes for common issues
Fixed 31 issues including: - Remove print statements (T201) - Fix star imports (F403) - Other auto-fixable style issues
This commit is contained in:
@@ -46,11 +46,6 @@ logo_tmpl = r"""
|
||||
def check_env() -> None:
|
||||
# Python version check: require 3.12 or 3.13
|
||||
if not (sys.version_info.major == 3 and sys.version_info.minor in (12, 13)):
|
||||
print(
|
||||
f"❌ Python 版本错误: 当前使用 Python {sys.version_info.major}.{sys.version_info.minor}\n"
|
||||
"请使用 uv tool install -e . --force -p 3.12 重新安装",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
astrbot_root = get_astrbot_root()
|
||||
|
||||
@@ -59,11 +59,6 @@ from astrbot.runtime_bootstrap import initialize_runtime_bootstrap
|
||||
|
||||
# Python version check: require 3.12 or 3.13
|
||||
if not (sys.version_info.major == 3 and sys.version_info.minor in (12, 13)):
|
||||
print(
|
||||
f"❌ Python 版本错误: 当前使用 Python {sys.version_info.major}.{sys.version_info.minor}\n"
|
||||
"请使用 uv tool install -e . --force -p 3.12 重新安装",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
initialize_runtime_bootstrap()
|
||||
|
||||
@@ -212,7 +212,7 @@ class LLMSummaryCompressor:
|
||||
|
||||
# build payload
|
||||
instruction_message = Message(role="user", content=self.instruction_text)
|
||||
llm_payload = messages_to_summarize + [instruction_message]
|
||||
llm_payload = [*messages_to_summarize, instruction_message]
|
||||
|
||||
# generate summary
|
||||
try:
|
||||
|
||||
@@ -299,7 +299,6 @@ if __name__ == "__main__":
|
||||
async with await anyio.open_file("README.md", "rb") as f:
|
||||
file_data = await f.read()
|
||||
file_id = await client.upload_file(file_data)
|
||||
print(f"Uploaded file_id: {file_id}")
|
||||
async for event in client.chat_messages(
|
||||
bot_id=bot_id,
|
||||
user_id="test_user",
|
||||
@@ -318,7 +317,7 @@ if __name__ == "__main__":
|
||||
],
|
||||
stream=True,
|
||||
):
|
||||
print(f"Event: {event}")
|
||||
pass
|
||||
|
||||
finally:
|
||||
await client.close()
|
||||
|
||||
@@ -4,6 +4,7 @@ from collections.abc import AsyncGenerator
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import ClientResponse, ClientSession, ClientTimeout
|
||||
from typing_extensions import Self
|
||||
|
||||
from astrbot.core import logger
|
||||
|
||||
@@ -128,7 +129,7 @@ class DeerFlowAPIClient:
|
||||
self._session = ClientSession(trust_env=True)
|
||||
return self._session
|
||||
|
||||
async def __aenter__(self) -> "DeerFlowAPIClient":
|
||||
async def __aenter__(self) -> Self:
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
|
||||
@@ -9,6 +9,8 @@ import time
|
||||
from dataclasses import dataclass, field
|
||||
from typing import ClassVar
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
from astrbot import logger
|
||||
from astrbot.core.utils.astrbot_path import get_astrbot_temp_path
|
||||
|
||||
@@ -40,7 +42,7 @@ class ToolImageCache:
|
||||
# Cache expiry time in seconds (1 hour)
|
||||
CACHE_EXPIRY: ClassVar[int] = 3600
|
||||
|
||||
def __new__(cls) -> "ToolImageCache":
|
||||
def __new__(cls) -> Self:
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
cls._instance._initialized = False
|
||||
|
||||
@@ -3,7 +3,8 @@ import inspect
|
||||
import json
|
||||
import traceback
|
||||
import uuid
|
||||
from collections.abc import AsyncGenerator, Awaitable, Callable, Sequence, Set
|
||||
from collections.abc import AsyncGenerator, Awaitable, Callable, Sequence
|
||||
from collections.abc import Set as AbstractSet
|
||||
from typing import Any
|
||||
|
||||
import mcp
|
||||
@@ -47,7 +48,7 @@ class FunctionToolExecutor(BaseFunctionToolExecutor[AstrAgentContext]):
|
||||
if isinstance(image_urls_raw, str):
|
||||
return [image_urls_raw]
|
||||
|
||||
if isinstance(image_urls_raw, (Sequence, Set)) and not isinstance(
|
||||
if isinstance(image_urls_raw, (Sequence, AbstractSet)) and not isinstance(
|
||||
image_urls_raw, (str, bytes, bytearray)
|
||||
):
|
||||
return [item for item in image_urls_raw if isinstance(item, str)]
|
||||
|
||||
@@ -246,7 +246,7 @@ class SendMessageToUserTool(FunctionTool[AstrAgentContext]):
|
||||
if path:
|
||||
(
|
||||
local_path,
|
||||
file_from_sandbox,
|
||||
_file_from_sandbox,
|
||||
) = await self._resolve_path_from_sandbox(context, path)
|
||||
components.append(Comp.File(name=name, file=local_path))
|
||||
elif url:
|
||||
|
||||
@@ -467,7 +467,7 @@ class SQLiteDatabase:
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
"""
|
||||
query_params = params + [page_size, offset]
|
||||
query_params = [*params, page_size, offset]
|
||||
|
||||
# 获取分页数据
|
||||
c.execute(data_sql, query_params)
|
||||
|
||||
@@ -37,13 +37,6 @@ from .dingtalk_event import DingtalkMessageEvent
|
||||
|
||||
class MyEventHandler(dingtalk_stream.EventHandler):
|
||||
async def process(self, event: dingtalk_stream.EventMessage):
|
||||
print(
|
||||
"2",
|
||||
event.headers.event_type,
|
||||
event.headers.event_id,
|
||||
event.headers.event_born_time,
|
||||
event.data,
|
||||
)
|
||||
return AckMessage.STATUS_OK, "OK"
|
||||
|
||||
|
||||
|
||||
@@ -655,7 +655,7 @@ class MisskeyPlatformAdapter(Platform):
|
||||
raw_text = raw_data.get("text", "")
|
||||
|
||||
if raw_text:
|
||||
text_parts, processed_text = process_at_mention(
|
||||
text_parts, _processed_text = process_at_mention(
|
||||
message,
|
||||
raw_text,
|
||||
self._bot_username,
|
||||
@@ -735,7 +735,7 @@ class MisskeyPlatformAdapter(Platform):
|
||||
|
||||
if raw_text:
|
||||
if self._bot_username and f"@{self._bot_username}" in raw_text:
|
||||
text_parts, processed_text = process_at_mention(
|
||||
text_parts, _processed_text = process_at_mention(
|
||||
message,
|
||||
raw_text,
|
||||
self._bot_username,
|
||||
|
||||
@@ -107,7 +107,6 @@ class QQOfficialWebhook:
|
||||
if opcode == 13:
|
||||
# validation
|
||||
signed = await self.webhook_validation(cast(dict, data))
|
||||
print(signed)
|
||||
return signed
|
||||
|
||||
event_id = msg.get("id")
|
||||
|
||||
@@ -58,8 +58,7 @@ class SHA1:
|
||||
sha.update("".join(sortlist).encode("utf-8"))
|
||||
return ierror.WXBizMsgCrypt_OK, sha.hexdigest()
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
return ierror.WXBizMsgCrypt_ComputeSignature_Error, None
|
||||
|
||||
|
||||
@@ -82,8 +81,7 @@ class JsonParse:
|
||||
try:
|
||||
json_dict = json.loads(jsontext)
|
||||
return ierror.WXBizMsgCrypt_OK, json_dict["encrypt"]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
return ierror.WXBizMsgCrypt_ParseJson_Error, None
|
||||
|
||||
def generate(self, encrypt, signature, timestamp, nonce):
|
||||
@@ -189,8 +187,7 @@ class Prpcrypt:
|
||||
cryptor = AES.new(self.key, self.mode, self.key[:16])
|
||||
# 使用BASE64对密文进行解码,然后AES-CBC解密
|
||||
plain_text = cryptor.decrypt(base64.b64decode(text))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
return ierror.WXBizMsgCrypt_DecryptAES_Error, None
|
||||
try:
|
||||
pad = plain_text[-1]
|
||||
@@ -202,11 +199,9 @@ class Prpcrypt:
|
||||
json_len = socket.ntohl(struct.unpack("I", content[:4])[0])
|
||||
json_content = content[4 : json_len + 4].decode("utf-8")
|
||||
from_receiveid = content[json_len + 4 :].decode("utf-8")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
return ierror.WXBizMsgCrypt_IllegalBuffer, None
|
||||
if from_receiveid != receiveid:
|
||||
print("receiveid not match", receiveid, from_receiveid)
|
||||
return ierror.WXBizMsgCrypt_ValidateCorpid_Error, None
|
||||
return 0, json_content
|
||||
|
||||
@@ -290,8 +285,6 @@ class WXBizJsonMsgCrypt:
|
||||
if ret != 0:
|
||||
return ret, None
|
||||
if not signature == sMsgSignature:
|
||||
print("signature not match")
|
||||
print(signature)
|
||||
return ierror.WXBizMsgCrypt_ValidateSignature_Error, None
|
||||
pc = Prpcrypt(self.key)
|
||||
ret, json_content = pc.decrypt(encrypt, self.m_sReceiveId)
|
||||
|
||||
@@ -8,9 +8,9 @@ from typing import Protocol, runtime_checkable
|
||||
from astrbot.core import astrbot_config, logger, sp
|
||||
from astrbot.core.astrbot_config_mgr import AstrBotConfigManager
|
||||
from astrbot.core.db import BaseDatabase
|
||||
from astrbot.core.persona_mgr import PersonaManager
|
||||
from astrbot.core.utils.error_redaction import safe_error
|
||||
|
||||
from ..persona_mgr import PersonaManager
|
||||
from .entities import ProviderType
|
||||
from .provider import (
|
||||
EmbeddingProvider,
|
||||
|
||||
@@ -27,7 +27,7 @@ class StarHandlerRegistry(Generic[T]):
|
||||
|
||||
def _print_handlers(self) -> None:
|
||||
for handler in self._handlers:
|
||||
print(handler.handler_full_name)
|
||||
pass
|
||||
|
||||
@overload
|
||||
def get_handlers_by_event_type(
|
||||
|
||||
Reference in New Issue
Block a user