From 7ee9a7a8c85d396b2a0e85b638a100d7c4347e7c Mon Sep 17 00:00:00 2001 From: KomoriDev Date: Sat, 14 Sep 2024 22:12:05 +0800 Subject: [PATCH] =?UTF-8?q?:wheelchair:=20=E5=85=BC=E5=AE=B9=20Pydantic=20?= =?UTF-8?q?v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_lxns_maimai/apis/request.py | 10 +++++----- nonebot_plugin_lxns_maimai/schema/score.py | 8 +++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nonebot_plugin_lxns_maimai/apis/request.py b/nonebot_plugin_lxns_maimai/apis/request.py index 939cd7c..fa7dea3 100644 --- a/nonebot_plugin_lxns_maimai/apis/request.py +++ b/nonebot_plugin_lxns_maimai/apis/request.py @@ -25,7 +25,7 @@ async def get_player_info(cls, friend_code: int) -> Player: ) if response.status_code != 200: raise FetchUserException(response.json()["message"]) - return Player.model_validate(response.json()["data"]) + return Player(**response.json()["data"]) @classmethod async def get_rating_trend(cls, friend_code: int) -> Trend: @@ -39,7 +39,7 @@ async def get_rating_trend(cls, friend_code: int) -> Trend: ) if response.status_code != 200: raise FetchUserException(response.json()["message"]) - return Trend.model_validate(response.json()["data"]) + return Trend(**response.json()["data"]) @classmethod async def get_bests( @@ -63,15 +63,15 @@ async def get_bests( if response.status_code != 200: raise FetchUserException(response.json()["message"]) data = response.json()["data"] - standard_scores = [Score.model_validate(score) for score in data["standard"]] - dx_scores = [Score.model_validate(score) for score in data["dx"]] + standard_scores = [Score(**score) for score in data["standard"]] + dx_scores = [Score(**score) for score in data["dx"]] return data["standard_total"], data["dx_total"], standard_scores, dx_scores @classmethod async def get_song_info(cls, song_id: int) -> Song: async with httpx.AsyncClient() as client: response = await client.get(f"{url}/song/{song_id}") - return Song.model_validate(response.json()) + return Song(**response.json()) @classmethod async def download_player_icon(cls, player: Player) -> BytesIO | bytes: diff --git a/nonebot_plugin_lxns_maimai/schema/score.py b/nonebot_plugin_lxns_maimai/schema/score.py index 5d157dc..f1338d9 100644 --- a/nonebot_plugin_lxns_maimai/schema/score.py +++ b/nonebot_plugin_lxns_maimai/schema/score.py @@ -1,3 +1,4 @@ +from nonebot.compat import PYDANTIC_V2 from pydantic import BaseModel, ConfigDict from .enum import FCType, FSType, RateType, SongType, LevelIndex @@ -33,4 +34,9 @@ class Score(BaseModel): upload_time: str """仅获取 `Score` 时返回,成绩被同步时的 UTC 时间""" - model_config = ConfigDict(extra="allow") + if PYDANTIC_V2: + model_config = ConfigDict(extra="allow") # type: ignore + else: + + class Config: + extra = "allow"