Skip to content

Commit

Permalink
🐛 修复 model_dump 缺少 by_alias 参数的问题 #18 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
CMHopeSunshine committed Mar 24, 2024
1 parent 8869bb0 commit 35af1e0
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 53 deletions.
2 changes: 1 addition & 1 deletion nonebot/adapters/discord/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def get_authorization(bot_info: BotInfo) -> str:
async def receive_payload(self, ws: WebSocket) -> Payload:
data = await ws.receive()
data = decompress_data(data, self.discord_config.discord_compress)
return type_validate_python(PayloadType, json.loads(data))
return type_validate_python(PayloadType, json.loads(data)) # type: ignore

@classmethod
def payload_to_event(cls, payload: Dispatch) -> Event:
Expand Down
5 changes: 3 additions & 2 deletions nonebot/adapters/discord/api/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,8 @@ async def _get_stage_instance(
url=adapter.base_url / f"stage-instances/{channel_id}",
)
return type_validate_python(
Optional[StageInstance], await _request(adapter, bot, request)
Optional[StageInstance],
await _request(adapter, bot, request), # type: ignore
)


Expand Down Expand Up @@ -3300,4 +3301,4 @@ async def _get_current_authorization_information(
"get_gateway_bot": _get_gateway_bot,
"get_current_bot_application_information": _get_current_bot_application_information,
"get_current_authorization_information": _get_current_authorization_information,
}
} # type: ignore
4 changes: 2 additions & 2 deletions nonebot/adapters/discord/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class Snowflake(int):

@classmethod
def __get_pydantic_core_schema__(cls, source, handler) -> CoreSchema:
return core_schema.with_info_plain_validator_function(cls.validate)
return core_schema.with_info_plain_validator_function(cls.validate) # type: ignore

@classmethod
def validate(cls, value: Any, _):
def validate(cls, value: Any, _): # type: ignore
if isinstance(value, str) and value.isdigit():
value = int(value)
if not isinstance(value, int):
Expand Down
2 changes: 1 addition & 1 deletion nonebot/adapters/discord/commands/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, *args, key: str, **kwargs: Any) -> None:
self.key = key

def __repr__(self) -> str:
return f"OptionParam(key={self.extra['key']!r})"
return f"OptionParam(key={self.key!r})"

@classmethod
@override
Expand Down
2 changes: 1 addition & 1 deletion nonebot/adapters/discord/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ class WebhooksUpdateEvent(NoticeEvent, WebhooksUpdate):
EventType.VOICE_STATE_UPDATE.value: VoiceStateUpdateEvent,
EventType.VOICE_SERVER_UPDATE.value: VoiceServerUpdateEvent,
EventType.WEBHOOKS_UPDATE.value: WebhooksUpdateEvent,
}
} # type: ignore

__all__ = [
"EventType",
Expand Down
2 changes: 1 addition & 1 deletion nonebot/adapters/discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def from_guild_message(cls, message: MessageGet) -> "Message":
if message.mention_everyone:
msg.append(MessageSegment.mention_everyone())
if message.content:
msg.extend(Message(message.content))
msg.append(MessageSegment.text(message.content))
if message.attachments:
msg.extend(
MessageSegment.attachment(
Expand Down
47 changes: 2 additions & 45 deletions nonebot/adapters/discord/payload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import IntEnum
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from typing import Optional, Union
from typing_extensions import Annotated, Literal

from nonebot.compat import PYDANTIC_V2, ConfigDict
Expand All @@ -12,12 +12,6 @@
Resume as ResumeData,
)

if TYPE_CHECKING:
if PYDANTIC_V2:
from pydantic.main import IncEx
else:
from pydantic.typing import AbstractSetIntStr, DictStrAny, MappingIntStrAny


class Opcode(IntEnum):
DISPATCH = 0
Expand All @@ -32,51 +26,14 @@ class Opcode(IntEnum):

class Payload(BaseModel):
if PYDANTIC_V2:
model_config = ConfigDict(extra="allow", populate_by_name=True)

def model_dump(
self,
*,
include: "IncEx" = None,
exclude: "IncEx" = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> Dict[str, Any]:
return super().model_dump(
include=include,
exclude=exclude,
by_alias=True,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)
model_config = ConfigDict(extra="allow", populate_by_name=True) # type: ignore

else:

class Config(ConfigDict):
extra = "allow"
allow_population_by_field_name = True

def dict(
self,
*,
include: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
exclude: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
**kwargs: Any,
) -> "DictStrAny":
return super().dict(
include=include,
exclude=exclude,
by_alias=True,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)


class Dispatch(Payload):
opcode: Literal[Opcode.DISPATCH] = Field(Opcode.DISPATCH, alias="op")
Expand Down

0 comments on commit 35af1e0

Please sign in to comment.