Skip to content

Commit c269b6b

Browse files
authored
feat(bot): 为 TypeVar 增加默认值,使用 Plugin 不指定范型参数时将提供合理的默认值 (#161)
1 parent f631728 commit c269b6b

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

alicebot/adapter/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
Callable,
1313
Generic,
1414
Optional,
15-
TypeVar,
1615
Union,
1716
final,
1817
overload,
1918
)
19+
from typing_extensions import TypeVar
2020

2121
import structlog
2222

@@ -36,7 +36,7 @@
3636
__import__("pkg_resources").declare_namespace(__name__)
3737

3838

39-
_EventT = TypeVar("_EventT", bound="Event[Any]")
39+
_EventT = TypeVar("_EventT", bound="Event[Any]", default="Event[Any]")
4040

4141

4242
class Adapter(Generic[EventT, ConfigT], ABC):

alicebot/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ async def get(
601601
func: Optional[Callable[[EventT], Union[bool, Awaitable[bool]]]] = None,
602602
*,
603603
event_type: type[EventT],
604-
adapter_type: Optional[type[AdapterT]] = None,
604+
adapter_type: Optional[type[Adapter[Any, Any]]] = None,
605605
max_try_times: Optional[int] = None,
606606
timeout: Optional[Union[int, float]] = None,
607607
) -> EventT: ...

alicebot/typing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# ruff: noqa: A005
77

88
from collections.abc import Awaitable
9-
from typing import TYPE_CHECKING, Callable, Optional, TypeVar
9+
from typing import TYPE_CHECKING, Callable, Optional
10+
from typing_extensions import TypeVar
1011

1112
from alicebot.message import BuildMessageType, MessageSegmentT, MessageT
1213

@@ -33,11 +34,11 @@
3334
"StateT",
3435
]
3536

36-
StateT = TypeVar("StateT")
37-
EventT = TypeVar("EventT", bound="Event[Any]")
37+
EventT = TypeVar("EventT", bound="Event[Any]", default="Event[Any]")
38+
StateT = TypeVar("StateT", default=None)
39+
ConfigT = TypeVar("ConfigT", bound=Optional["ConfigModel"], default=None)
3840
PluginT = TypeVar("PluginT", bound="Plugin[Any, Any, Any]")
3941
AdapterT = TypeVar("AdapterT", bound="Adapter[Any, Any]")
40-
ConfigT = TypeVar("ConfigT", bound=Optional["ConfigModel"])
4142

4243
BotHook = Callable[["Bot"], Awaitable[None]]
4344
AdapterHook = Callable[["Adapter[Any, Any]"], Awaitable[None]]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies = [
2323
"aiohttp>=3.8.0,<4.0.0",
2424
"structlog>=25.1.0,<26.0.0",
2525
"rich>=13.7.0,<14.0.0",
26-
"typing-extensions>=4.5.0",
26+
"typing-extensions>=4.12.0,<5.0.0",
2727
"anyio>=4.4.0,<5.0.0",
2828
]
2929

0 commit comments

Comments
 (0)