mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 20:16:47 +00:00
✨ Feature: 依赖注入支持 Generic TypeVar 和 Matcher 重载 (#2089)
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from typing import Union
|
||||
from typing import Union, TypeVar
|
||||
|
||||
from nonebot.adapters import Bot
|
||||
|
||||
@ -31,5 +31,19 @@ async def union_bot(b: Union[FooBot, BarBot]) -> Union[FooBot, BarBot]:
|
||||
return b
|
||||
|
||||
|
||||
B = TypeVar("B", bound=Bot)
|
||||
|
||||
|
||||
async def generic_bot(b: B) -> B:
|
||||
return b
|
||||
|
||||
|
||||
CB = TypeVar("CB", Bot, None)
|
||||
|
||||
|
||||
async def generic_bot_none(b: CB) -> CB:
|
||||
return b
|
||||
|
||||
|
||||
async def not_bot(b: Union[int, Bot]):
|
||||
...
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import Union
|
||||
from typing import Union, TypeVar
|
||||
|
||||
from nonebot.adapters import Event, Message
|
||||
from nonebot.params import EventToMe, EventType, EventMessage, EventPlainText
|
||||
@ -32,6 +32,20 @@ async def union_event(e: Union[FooEvent, BarEvent]) -> Union[FooEvent, BarEvent]
|
||||
return e
|
||||
|
||||
|
||||
E = TypeVar("E", bound=Event)
|
||||
|
||||
|
||||
async def generic_event(e: E) -> E:
|
||||
return e
|
||||
|
||||
|
||||
CE = TypeVar("CE", Event, None)
|
||||
|
||||
|
||||
async def generic_event_none(e: CE) -> CE:
|
||||
return e
|
||||
|
||||
|
||||
async def not_event(e: Union[int, Event]):
|
||||
...
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
from typing import Union, TypeVar
|
||||
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Received, LastReceived
|
||||
@ -7,6 +9,50 @@ async def matcher(m: Matcher) -> Matcher:
|
||||
return m
|
||||
|
||||
|
||||
async def legacy_matcher(matcher):
|
||||
return matcher
|
||||
|
||||
|
||||
async def not_legacy_matcher(matcher: int):
|
||||
...
|
||||
|
||||
|
||||
class FooMatcher(Matcher):
|
||||
...
|
||||
|
||||
|
||||
async def sub_matcher(m: FooMatcher) -> FooMatcher:
|
||||
return m
|
||||
|
||||
|
||||
class BarMatcher(Matcher):
|
||||
...
|
||||
|
||||
|
||||
async def union_matcher(
|
||||
m: Union[FooMatcher, BarMatcher]
|
||||
) -> Union[FooMatcher, BarMatcher]:
|
||||
return m
|
||||
|
||||
|
||||
M = TypeVar("M", bound=Matcher)
|
||||
|
||||
|
||||
async def generic_matcher(m: M) -> M:
|
||||
return m
|
||||
|
||||
|
||||
CM = TypeVar("CM", Matcher, None)
|
||||
|
||||
|
||||
async def generic_matcher_none(m: CM) -> CM:
|
||||
return m
|
||||
|
||||
|
||||
async def not_matcher(m: Union[int, Matcher]):
|
||||
...
|
||||
|
||||
|
||||
async def receive(e: Event = Received("test")) -> Event:
|
||||
return e
|
||||
|
||||
|
Reference in New Issue
Block a user