generated from yanyongyu/python-poetry-template
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ 为 MessageFactory 和 MessageSegmentFactory 添加类型提示 #127
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
736899d
:bug: 允许创建空的MessageFactory
AzideCupric 5dac323
:sparkles: 类型体操!
AzideCupric 35050c6
:sparkles: 保持原来的append方法返回自身的行为
AzideCupric 451d526
:bug: 为飞书测试补充一些字段
AzideCupric 763d15b
:white_check_mark: 补充 iadd 的测试
AzideCupric 59c5979
:construction: 统一Reply的data类型
AzideCupric 2037e7f
:white_check_mark: 完善测试
AzideCupric 9204b9d
:white_check_mark: 忽略一些不需要测试的地方
AzideCupric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
from inspect import signature | ||
from typing_extensions import Self | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Dict, | ||
List, | ||
Type, | ||
|
@@ -17,6 +19,7 @@ | |
Optional, | ||
Awaitable, | ||
cast, | ||
overload, | ||
) | ||
|
||
from nonebot.adapters import Bot, Event, Message, MessageSegment | ||
|
@@ -32,7 +35,11 @@ | |
extract_adapter_type, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from .types import Text | ||
|
||
TMSF = TypeVar("TMSF", bound="MessageSegmentFactory") | ||
TMSFO = TypeVar("TMSFO", bound="MessageSegmentFactory") | ||
TMF = TypeVar("TMF", bound="MessageFactory") | ||
BuildFunc = Union[ | ||
Callable[[TMSF], Union[MessageSegment, Awaitable[MessageSegment]]], | ||
|
@@ -105,7 +112,7 @@ | |
] | ||
] | ||
|
||
data: dict | ||
data: Dict[str, Any] | ||
_custom_builders: Dict[SupportedAdapters, CustomBuildFunc] | ||
|
||
def _register_custom_builder( | ||
|
@@ -131,8 +138,21 @@ | |
cls._builders = {} | ||
return super().__init_subclass__() | ||
|
||
def __eq__(self, other: Self) -> bool: | ||
return self.data == other.data | ||
def __eq__(self, other: object) -> bool: | ||
if isinstance(other, MessageSegmentFactory): | ||
return self.data == other.data | ||
elif isinstance(other, str): | ||
return self.data["text"] == other | ||
else: | ||
return False | ||
|
||
def __str__(self) -> str: | ||
kvstr = ",".join([f"{k}={v!r}" for k, v in self.data.items()]) | ||
return f"[SAA:{self.__class__.__name__}|{kvstr}]" | ||
|
||
def __repr__(self) -> str: | ||
attrs = ", ".join([f"{k}={v!r}" for k, v in self.data.items()]) | ||
return f"{self.__class__.__name__}({attrs})" | ||
|
||
def overwrite( | ||
self, | ||
|
@@ -151,11 +171,67 @@ | |
return await do_build(self, builder, bot) | ||
raise AdapterNotInstalled(adapter_name) | ||
|
||
def __add__(self: TMSF, other: Union[str, TMSF, Iterable[TMSF]]): | ||
return MessageFactory(self) + other | ||
|
||
def __radd__(self: TMSF, other: Union[str, TMSF, Iterable[TMSF]]): | ||
return MessageFactory(other) + self | ||
@overload | ||
def __add__( | ||
self: Self, other: Union[str, Iterable[str]] | ||
) -> "MessageFactory[Union[Self, Text]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __add__( | ||
self: Self, other: Union[TMSFO, Iterable[TMSFO]] | ||
) -> "MessageFactory[Union[Self, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __add__( | ||
self: Self, other: Iterable[Union[str, TMSFO]] | ||
) -> "MessageFactory[Union[Self, Text, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
def __add__( | ||
self: Self, other: Union[str, TMSFO, Iterable[Union[str, TMSFO]]] | ||
) -> "MessageFactory": | ||
if isinstance(other, str): | ||
text = MessageFactory.get_text_factory()(other) | ||
return MessageFactory([self, text]) | ||
elif isinstance(other, MessageSegmentFactory): | ||
return MessageFactory([self, other]) | ||
elif isinstance(other, Iterable): | ||
return MessageFactory([self, *other]) | ||
else: | ||
raise TypeError(f"unsupported type {type(other)}") | ||
|
||
@overload | ||
def __radd__( | ||
self: Self, other: Union[str, Iterable[str]] | ||
) -> "MessageFactory[Union[Self, Text]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __radd__( | ||
self: Self, other: Union[TMSFO, Iterable[TMSFO]] | ||
) -> "MessageFactory[Union[Self, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __radd__( | ||
self: Self, other: Iterable[Union[str, TMSFO]] | ||
) -> "MessageFactory[Union[Self, Text, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
def __radd__( | ||
self: Self, other: Union[str, TMSFO, Iterable[Union[str, TMSFO]]] | ||
) -> "MessageFactory": | ||
if isinstance(other, str): | ||
text = MessageFactory.get_text_factory()(other) | ||
return MessageFactory([text, self]) | ||
elif isinstance(other, MessageSegmentFactory): | ||
return MessageFactory([other, self]) | ||
elif isinstance(other, Iterable): | ||
return MessageFactory([*other, self]) | ||
else: | ||
raise TypeError(f"unsupported type {type(other)}") | ||
|
||
async def send(self, *, at_sender=False, reply=False): | ||
"回复消息,仅能用在事件响应器中" | ||
|
@@ -241,52 +317,159 @@ | |
return message_type(ms) | ||
raise AdapterNotInstalled(adapter_name) | ||
|
||
def __init__(self, message: Union[str, Iterable[TMSF], TMSF]): | ||
super().__init__() | ||
@overload | ||
def __init__(self: "MessageFactory[Text]", ms: Union[str, Iterable[str]]) -> None: | ||
... # pragma: no cover | ||
|
||
if message is None: | ||
return | ||
@overload | ||
def __init__( | ||
self: "MessageFactory[TMSFO]", ms: Union[TMSFO, Iterable[TMSFO]] | ||
) -> None: | ||
... # pragma: no cover | ||
|
||
if isinstance(message, str): | ||
self.append(self.get_text_factory()(message)) | ||
elif isinstance(message, MessageSegmentFactory): | ||
self.append(message) | ||
elif isinstance(message, Iterable): | ||
self.extend(message) | ||
@overload | ||
def __init__( | ||
self: "MessageFactory[Text | TMSFO]", | ||
ms: Iterable[Union[str, TMSFO]], | ||
) -> None: | ||
... # pragma: no cover | ||
|
||
def __add__(self: TMF, other: Union[str, TMSF, Iterable[TMSF]]) -> TMF: | ||
result = self.copy() | ||
result += other | ||
return result | ||
@overload | ||
def __init__(self: "MessageFactory") -> None: | ||
... # pragma: no cover | ||
|
||
def __radd__(self: TMF, other: Union[str, TMSF, Iterable[TMSF]]) -> TMF: | ||
result = self.__class__(other) | ||
return result + self | ||
def __init__(self, ms: Union[str, TMSFO, Iterable[Union[str, TMSFO]], None] = None): | ||
super().__init__() | ||
if ms is None: | ||
return | ||
|
||
def __iadd__(self: TMF, other: Union[str, TMSF, Iterable[TMSF]]) -> TMF: | ||
if isinstance(ms, (str, MessageSegmentFactory)): | ||
self.__iadd__(ms) | ||
elif isinstance(ms, Iterable): | ||
for i in ms: | ||
self.__iadd__(i) | ||
|
||
@overload | ||
def __add__( | ||
self: "MessageFactory[TMSF]", other: Union[str, Iterable[str]] | ||
) -> "MessageFactory[Union[TMSF, Text]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __add__( | ||
self: "MessageFactory[TMSF]", other: Union[TMSFO, Iterable[TMSFO]] | ||
) -> "MessageFactory[Union[TMSF, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __add__( | ||
self: "MessageFactory[TMSF]", | ||
other: Iterable[Union[str, TMSFO]], | ||
) -> "MessageFactory[Union[TMSF, Text, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
def __add__( | ||
self: "MessageFactory[TMSF]", | ||
other: Union[str, TMSFO, Iterable[Union[str, TMSFO]]], | ||
) -> "MessageFactory": | ||
copied = self.copy() | ||
if isinstance(other, str): | ||
copied.append(self.get_text_factory()(other)) | ||
return copied | ||
elif isinstance(other, MessageSegmentFactory): | ||
copied.append(other) | ||
return copied | ||
elif isinstance(other, Iterable): | ||
for i in other: | ||
copied += i | ||
return copied | ||
else: | ||
raise TypeError( | ||
f"unsupported operand type(s) for +: '{self.__class__.__name__}' and '{type(other)}'" # noqa: E501 | ||
) | ||
|
||
@overload | ||
def __radd__( | ||
self: "MessageFactory[TMSF]", other: Union[str, Iterable[str]] | ||
) -> "MessageFactory[Union[TMSF, Text]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __radd__( | ||
self: "MessageFactory[TMSF]", other: Union[TMSFO, Iterable[TMSFO]] | ||
) -> "MessageFactory[Union[TMSF, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __radd__( | ||
self: "MessageFactory[TMSF]", | ||
other: Iterable[Union[str, TMSFO]], | ||
) -> "MessageFactory[Union[TMSF, Text, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
def __radd__( | ||
self: "MessageFactory[TMSF]", | ||
other: Union[str, TMSFO, Iterable[Union[str, TMSFO]]], | ||
) -> "MessageFactory": | ||
if isinstance(other, (str, MessageSegmentFactory)): | ||
return MessageFactory(other) + self | ||
elif isinstance(other, Iterable): | ||
return MessageFactory(other) + self # type: ignore | ||
else: | ||
raise TypeError( | ||
f"unsupported operand type(s) for +: '{type(other)}' and '{self.__class__.__name__}'" # noqa: E501 | ||
) | ||
|
||
@overload | ||
def __iadd__( | ||
self: "MessageFactory[TMSF]", other: Union[str, Iterable[str]] | ||
) -> "MessageFactory[Union[TMSF, Text]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __iadd__( | ||
self: "MessageFactory[TMSF]", other: Union[TMSFO, Iterable[TMSFO]] | ||
) -> "MessageFactory[Union[TMSF, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
@overload | ||
def __iadd__( | ||
self: "MessageFactory[TMSF]", | ||
other: Iterable[Union[str, TMSFO]], | ||
) -> "MessageFactory[Union[TMSF, Text, TMSFO]]": | ||
... # pragma: no cover | ||
|
||
def __iadd__( | ||
self: "MessageFactory[TMSF]", | ||
other: Union[str, TMSFO, Iterable[Union[str, TMSFO]]], | ||
) -> "MessageFactory": | ||
if isinstance(other, str): | ||
self.append(self.get_text_factory()(other)) | ||
return self | ||
elif isinstance(other, MessageSegmentFactory): | ||
self.append(other) | ||
return self | ||
elif isinstance(other, Iterable): | ||
self.extend(other) | ||
return self | ||
else: | ||
raise TypeError( | ||
f"unsupported operand type(s) for +=: '{self.__class__.__name__}' and '{type(other)}'" # noqa: E501 | ||
) | ||
|
||
return self | ||
|
||
def append(self: TMF, obj: Union[str, TMSF]) -> TMF: | ||
if isinstance(obj, MessageSegmentFactory): | ||
super().append(obj) | ||
elif isinstance(obj, str): | ||
def append(self, obj: Union[str, TMSFO]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里可能会break |
||
if isinstance(obj, str): | ||
super().append(self.get_text_factory()(obj)) | ||
|
||
elif isinstance(obj, MessageSegmentFactory): | ||
super().append(obj) # type: ignore | ||
else: | ||
raise TypeError(f"unsupported type {type(obj)}") | ||
return self | ||
|
||
def extend(self: TMF, obj: Union[TMF, Iterable[TMSF]]) -> TMF: | ||
def extend(self: TMF, obj: Union[TMF, Iterable[Union[str, TMSFO]]]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同样 |
||
for message_segment_factory in obj: | ||
self.append(message_segment_factory) | ||
|
||
return self | ||
|
||
def copy(self: TMF) -> TMF: | ||
return deepcopy(self) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
作用?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
表明是两个可能不同的子类
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有两个都用到的地方吗