From 7a3d7c908c4983dcf2fe2f1b339edff0b8efcdcc Mon Sep 17 00:00:00 2001 From: shoucandanghehe Date: Thu, 2 May 2024 20:51:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=20override=20?= =?UTF-8?q?=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_tetris_stats/db/models.py | 10 +++++++--- .../game_data_processor/io_data_processor/processor.py | 6 ++++++ .../top_data_processor/processor.py | 7 +++++++ .../tos_data_processor/processor.py | 7 +++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/nonebot_plugin_tetris_stats/db/models.py b/nonebot_plugin_tetris_stats/db/models.py index cec2a5b..ab09654 100644 --- a/nonebot_plugin_tetris_stats/db/models.py +++ b/nonebot_plugin_tetris_stats/db/models.py @@ -8,6 +8,7 @@ from nonebot_plugin_orm import Model from pydantic import BaseModel, ValidationError from sqlalchemy import JSON, DateTime, Dialect, PickleType, String, TypeDecorator from sqlalchemy.orm import Mapped, MappedAsDataclass, mapped_column +from typing_extensions import override from ..game_data_processor.schemas import BaseProcessedData, BaseUser from ..utils.typing import CommandType, GameType @@ -16,17 +17,20 @@ from ..utils.typing import CommandType, GameType class PydanticType(TypeDecorator): impl = JSON - def __init__(self, get_model: Callable[[], Sequence[type[BaseModel]]], *args: Any, **kwargs: Any): # noqa: ANN401 + @override + def __init__(self, get_model: Callable[[], Sequence[type[BaseModel]]], *args: Any, **kwargs: Any): self.get_model = get_model super().__init__(*args, **kwargs) - def process_bind_param(self, value: Any | None, dialect: Dialect) -> str: # noqa: ANN401 + @override + def process_bind_param(self, value: Any | None, dialect: Dialect) -> str: # 将 Pydantic 模型实例转换为 JSON if isinstance(value, tuple(self.get_model())): return value.json() # type: ignore[union-attr] raise TypeError - def process_result_value(self, value: Any | None, dialect: Dialect) -> BaseModel: # noqa: ANN401 + @override + def process_result_value(self, value: Any | None, dialect: Dialect) -> BaseModel: # 将 JSON 转换回 Pydantic 模型实例 if isinstance(value, str | bytes): for i in self.get_model(): diff --git a/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/processor.py b/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/processor.py index 8be1c6a..a36c46b 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/processor.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/io_data_processor/processor.py @@ -18,6 +18,7 @@ from nonebot_plugin_localstore import get_data_file # type: ignore[import-untyp from nonebot_plugin_orm import get_session from nonebot_plugin_userinfo import UserInfo as NBUserInfo # type: ignore[import-untyped] from sqlalchemy import select +from typing_extensions import override from zstandard import ZstdCompressor from ...db import BindStatus, create_or_update_bind @@ -63,15 +64,18 @@ class Processor(ProcessorMeta): raw_response: RawResponse processed_data: ProcessedData + @override def __init__(self, event_id: int, user: User, command_args: list[str]) -> None: super().__init__(event_id, user, command_args) self.raw_response = RawResponse() self.processed_data = ProcessedData() @property + @override def game_platform(self) -> Literal['IO']: return GAME_TYPE + @override async def handle_bind(self, platform: str, account: str, bot_info: NBUserInfo) -> UniMessage: """处理绑定消息""" self.command_type = 'bind' @@ -110,6 +114,7 @@ class Processor(ProcessorMeta): ) return message + @override async def handle_query(self) -> str: """处理查询消息""" self.command_type = 'query' @@ -149,6 +154,7 @@ class Processor(ProcessorMeta): self.processed_data.user_records = user_records return self.processed_data.user_records + @override async def generate_message(self) -> str: """生成消息""" user_info = await self.get_user_info() diff --git a/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/processor.py b/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/processor.py index 2c3c1b8..37c8db3 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/processor.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/top_data_processor/processor.py @@ -10,6 +10,7 @@ from nonebot_plugin_alconna.uniseg import UniMessage from nonebot_plugin_orm import get_session from nonebot_plugin_userinfo import UserInfo # type: ignore[import-untyped] from pandas import read_html +from typing_extensions import override from ...db import BindStatus, create_or_update_bind from ...utils.avatar import get_avatar @@ -30,6 +31,7 @@ class User(BaseUser): name: str @property + @override def unique_identifier(self) -> str: return self.name @@ -57,15 +59,18 @@ class Processor(ProcessorMeta): raw_response: RawResponse processed_data: ProcessedData + @override def __init__(self, event_id: int, user: User, command_args: list[str]) -> None: super().__init__(event_id, user, command_args) self.raw_response = RawResponse() self.processed_data = ProcessedData() @property + @override def game_platform(self) -> Literal['TOP']: return GAME_TYPE + @override async def handle_bind(self, platform: str, account: str, bot_info: UserInfo, user_info: UserInfo) -> UniMessage: """处理绑定消息""" self.command_type = 'bind' @@ -99,6 +104,7 @@ class Processor(ProcessorMeta): ) return message + @override async def handle_query(self) -> str: """处理查询消息""" self.command_type = 'query' @@ -141,6 +147,7 @@ class Processor(ProcessorMeta): total = Data(lpm=dataframe['lpm'].mean(), apm=dataframe['apm'].mean()) if len(dataframe) != 0 else None return GameData(day=day, total=total) + @override async def generate_message(self) -> str: """生成消息""" game_data = await self.get_game_data() diff --git a/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/processor.py b/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/processor.py index 679ff31..a4da875 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/processor.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/tos_data_processor/processor.py @@ -8,6 +8,7 @@ from nonebot.compat import type_validate_json from nonebot_plugin_alconna.uniseg import UniMessage from nonebot_plugin_orm import get_session from nonebot_plugin_userinfo import UserInfo as NBUserInfo # type: ignore[import-untyped] +from typing_extensions import override from ...db import BindStatus, create_or_update_bind from ...utils.avatar import get_avatar @@ -32,6 +33,7 @@ class User(BaseUser): name: str | None = None @property + @override def unique_identifier(self) -> str: if self.teaid is None: raise ValueError('不完整的User!') @@ -70,15 +72,18 @@ class Processor(ProcessorMeta): raw_response: RawResponse processed_data: ProcessedData + @override def __init__(self, event_id: int, user: User, command_args: list[str]) -> None: super().__init__(event_id, user, command_args) self.raw_response = RawResponse(user_profile={}) self.processed_data = ProcessedData(user_profile={}) @property + @override def game_platform(self) -> Literal['TOS']: return GAME_TYPE + @override async def handle_bind(self, platform: str, account: str, bot_info: NBUserInfo) -> UniMessage: """处理绑定消息""" self.command_type = 'bind' @@ -113,6 +118,7 @@ class Processor(ProcessorMeta): ) return message + @override async def handle_query(self) -> str: """处理查询消息""" self.command_type = 'query' @@ -229,6 +235,7 @@ class Processor(ProcessorMeta): vs=round((adpm / 60 * 100), 2), ) + @override async def generate_message(self) -> str: """生成消息""" user_info = (await self.get_user_info()).data