From 30b017389bf63f5a212802f3694951c4bd824ddf Mon Sep 17 00:00:00 2001 From: uy_sun Date: Tue, 13 Feb 2024 18:23:21 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20SerializationMeta=20=E9=80=82=E9=85=8D?= =?UTF-8?q?=20Pydantic=20V2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_saa/registries/meta.py | 39 ++++++++++++++++++--------- poetry.lock | 2 +- tests/test_platform_target.py | 4 +++ tests/test_send.py | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/nonebot_plugin_saa/registries/meta.py b/nonebot_plugin_saa/registries/meta.py index 00c4545..19e416a 100644 --- a/nonebot_plugin_saa/registries/meta.py +++ b/nonebot_plugin_saa/registries/meta.py @@ -24,25 +24,40 @@ class SerializationMeta(BaseModel, ABC): frozen=True, orm_mode=True, ) + + @classmethod + def __pydantic_init_subclass__(cls, *args, **kwargs) -> None: + if cls._level == Level.MetaBase: + cls._level = Level.Base + cls._deserializer_dict = {} + elif cls._level == Level.Base: + cls._level = Level.Normal + cls._deserializer_dict[cls.model_fields[cls._index_key].default] = cls + elif cls._level == Level.Normal: + pass + else: + raise RuntimeError("SerializationMeta init error") + else: class Config: frozen = True orm_mode = True - def __init_subclass__(cls, *args, **kwargs) -> None: - if cls._level == Level.MetaBase: - cls._level = Level.Base - cls._deserializer_dict = {} - elif cls._level == Level.Base: - cls._level = Level.Normal - cls._deserializer_dict[cls.__fields__[cls._index_key].default] = cls - elif cls._level == Level.Normal: - pass - else: - raise RuntimeError("SerializationMeta init error") + @classmethod + def __init_subclass__(cls, *args, **kwargs) -> None: + if cls._level == Level.MetaBase: + cls._level = Level.Base + cls._deserializer_dict = {} + elif cls._level == Level.Base: + cls._level = Level.Normal + cls._deserializer_dict[cls.__fields__[cls._index_key].default] = cls + elif cls._level == Level.Normal: + pass + else: + raise RuntimeError("SerializationMeta init error") - super().__init_subclass__(*args, **kwargs) + super().__init_subclass__(*args, **kwargs) @classmethod def deserialize(cls, source: Any) -> Self: diff --git a/poetry.lock b/poetry.lock index cf3b98d..417f105 100644 --- a/poetry.lock +++ b/poetry.lock @@ -877,7 +877,7 @@ typing-extensions = "^4.8.0" type = "git" url = "https://github.com/he0119/nonebot-adapter-kaiheila.git" reference = "feat/pyd2" -resolved_reference = "460e6da9191c37678a28235d97bb620ff18bd6cb" +resolved_reference = "f10fb29b6cf5d26d24f2a12e40f621e058f0576a" [[package]] name = "nonebot-adapter-onebot" diff --git a/tests/test_platform_target.py b/tests/test_platform_target.py index f4dc0f5..f0231e9 100644 --- a/tests/test_platform_target.py +++ b/tests/test_platform_target.py @@ -13,7 +13,11 @@ class MySendTarget(PlatformTarget): platform_type: Literal[SupportedPlatform.qq_group] = SupportedPlatform.qq_group my_field: int + def test(self): + return self.my_field + send_target = MySendTarget(my_field=123) + send_target.test() serialized_target = send_target.json() deserialized_target = PlatformTarget.deserialize(serialized_target) diff --git a/tests/test_send.py b/tests/test_send.py index cd0c544..8b779f1 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -53,6 +53,6 @@ async def test_matcher(): "detail_type": "private", "user_id": event.user_id, }, - result={"message_id": 12451}, + result={"message_id": "12451"}, ) assertion(ctx)