From a80f74505fb801d780f3928d7ce3510f4588891b Mon Sep 17 00:00:00 2001 From: uy_sun Date: Wed, 14 Feb 2024 19:16:20 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E5=B0=86=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E5=8F=96=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_saa/registries/meta.py | 35 ++++++++++++--------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/nonebot_plugin_saa/registries/meta.py b/nonebot_plugin_saa/registries/meta.py index 19e416a..1a54e5d 100644 --- a/nonebot_plugin_saa/registries/meta.py +++ b/nonebot_plugin_saa/registries/meta.py @@ -27,16 +27,7 @@ class SerializationMeta(BaseModel, ABC): @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") + cls._register_subclass() else: @@ -46,19 +37,23 @@ class Config: @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") + cls._register_subclass() super().__init_subclass__(*args, **kwargs) + @classmethod + def _register_subclass(cls) -> 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 deserialize(cls, source: Any) -> Self: if isinstance(source, str):