Skip to content

Commit

Permalink
✨ Feat: 添加 pydantic v2 兼容支持 (#94)
Browse files Browse the repository at this point in the history
* ✨ upgrade to pydantic v2

* ⬆️ bump dependencies

* 🚨 add pyright v2 constant

* 🚨 convert pydantic url to string before concat

---------

Co-authored-by: StarHeartHunt <[email protected]>
  • Loading branch information
he0119 and StarHeartHunt authored Feb 14, 2024
1 parent 78e2deb commit 6b8daa4
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 247 deletions.
15 changes: 8 additions & 7 deletions nonebot/adapters/feishu/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import Any, Dict, List, Type, Union, Callable, Optional, cast

from pygtrie import StringTrie
from pydantic import parse_obj_as
from nonebot.utils import escape_tag
from nonebot.compat import type_validate_python
from nonebot.drivers import (
URL,
Driver,
Expand All @@ -17,6 +17,7 @@
HTTPServerSetup,
)

from nonebot import get_plugin_config
from nonebot.adapters import Adapter as BaseAdapter

from . import event
Expand Down Expand Up @@ -44,7 +45,7 @@ class Adapter(BaseAdapter):
def __init__(self, driver: Driver, **kwargs: Any):
super().__init__(driver, **kwargs)
"""飞书适配器配置"""
self.feishu_config: Config = Config(**self.config.dict())
self.feishu_config: Config = get_plugin_config(Config)
self.bot_apps: Dict[str, BotConfig] = {}
self.setup()

Expand Down Expand Up @@ -107,7 +108,7 @@ def setup(self) -> None:
self.driver.on_startup(self.startup)

def get_api_url(self, bot_config: BotConfig, path: str):
api_base = (
api_base = str(
self.feishu_config.feishu_lark_api_base
if bot_config.is_lark
else self.feishu_config.feishu_api_base
Expand All @@ -127,7 +128,7 @@ async def get_bot_info(self, bot_config: BotConfig):
},
),
)
result = parse_obj_as(BotInfoResponse, response)
result = type_validate_python(BotInfoResponse, response)

return result

Expand All @@ -147,7 +148,7 @@ async def get_tenant_access_token(self, bot_config: BotConfig):
},
),
)
result = parse_obj_as(TenantAccessTokenResponse, response)
result = type_validate_python(TenantAccessTokenResponse, response)

expire = result.expire
if expire > 30 * 60:
Expand Down Expand Up @@ -290,12 +291,12 @@ def json_to_event(cls, json_data: Any) -> Optional[Event]:
models = cls.get_event_model(event_type)
for model in models:
try:
event = model.parse_obj(json_data)
event = type_validate_python(model, json_data)
break
except Exception as e:
log("DEBUG", "Event Parser Error", e)
else:
event = Event.parse_obj(json_data)
event = type_validate_python(Event, json_data)

return event

Expand Down
4 changes: 2 additions & 2 deletions nonebot/adapters/feishu/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing_extensions import override
from typing import TYPE_CHECKING, Any, Dict, List, Union, Literal, Callable, Optional

from pydantic import parse_obj_as
from nonebot.message import handle_event
from nonebot.compat import type_validate_python

from nonebot.adapters import Bot as BaseBot

Expand All @@ -30,7 +30,7 @@ async def _check_reply(bot: "Bot", event: "Event"):
f"im/v1/messages/{event.event.message.parent_id}",
method="GET",
)
result = parse_obj_as(
result = type_validate_python(
ReplyResponse,
response,
)
Expand Down
3 changes: 0 additions & 3 deletions nonebot/adapters/feishu/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,3 @@ class Config(BaseModel):
feishu_api_base: HttpUrl = Field("https://open.feishu.cn/open-apis/")
feishu_lark_api_base: HttpUrl = Field("https://open.larksuite.com/open-apis/")
feishu_bots: List[BotConfig] = Field(default_factory=list)

class Config:
extra = "ignore"
5 changes: 3 additions & 2 deletions nonebot/adapters/feishu/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pydantic import Field
from nonebot.utils import escape_tag
from nonebot.compat import model_dump

from nonebot.adapters import Event as BaseEvent

Expand Down Expand Up @@ -83,7 +84,7 @@ def get_event_name(self) -> str:

@override
def get_event_description(self) -> str:
return escape_tag(str(self.dict()))
return escape_tag(str(model_dump(self)))

@override
def get_message(self) -> Message:
Expand Down Expand Up @@ -214,7 +215,7 @@ def get_event_name(self) -> str:

@override
def get_event_description(self) -> str:
return escape_tag(str(self.dict()))
return escape_tag(str(model_dump(self)))

@override
def get_message(self) -> Message:
Expand Down
25 changes: 19 additions & 6 deletions nonebot/adapters/feishu/models/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List, Literal, Optional

from pydantic import BaseModel
from nonebot.compat import PYDANTIC_V2, ConfigDict


class EventHeader(BaseModel):
Expand All @@ -13,8 +14,12 @@ class EventHeader(BaseModel):
resource_id: Optional[str]
user_list: Optional[List[dict]]

class Config:
extra = "allow"
if PYDANTIC_V2:
model_config = ConfigDict(extra="allow")
else:

class Config(ConfigDict):
extra = "allow"


class UserId(BaseModel):
Expand Down Expand Up @@ -118,8 +123,12 @@ class Reply(BaseModel):
mentions: List[ReplyMention]
upper_message_id: Optional[str]

class Config:
extra = "allow"
if PYDANTIC_V2:
model_config = ConfigDict(extra="allow")
else:

class Config(ConfigDict):
extra = "allow"


class EventMessage(BaseModel):
Expand All @@ -133,8 +142,12 @@ class EventMessage(BaseModel):
content: str
mentions: Optional[List[Mention]]

class Config:
extra = "allow"
if PYDANTIC_V2:
model_config = ConfigDict(extra="allow")
else:

class Config(ConfigDict):
extra = "allow"


class GroupEventMessage(EventMessage):
Expand Down
Loading

0 comments on commit 6b8daa4

Please sign in to comment.