Skip to content

Commit 651f6a2

Browse files
authored
🏷️ improve typing
1 parent 9a00f9b commit 651f6a2

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

nonebot/adapters/github/bot.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Dict,
99
List,
1010
Union,
11+
Generic,
12+
TypeVar,
1113
Callable,
1214
Optional,
1315
AsyncGenerator,
@@ -16,7 +18,13 @@
1618
from nonebot.typing import overrides
1719
from githubkit.utils import UNSET, Unset
1820
from nonebot.message import handle_event
19-
from githubkit import GitHub, AppAuthStrategy, TokenAuthStrategy, OAuthAppAuthStrategy
21+
from githubkit import (
22+
GitHub,
23+
AppAuthStrategy,
24+
BaseAuthStrategy,
25+
TokenAuthStrategy,
26+
OAuthAppAuthStrategy,
27+
)
2028

2129
from nonebot.adapters import Bot as BaseBot
2230

@@ -32,6 +40,9 @@
3240
from .adapter import Adapter
3341

3442

43+
A = TypeVar("A", bound=BaseAuthStrategy)
44+
45+
3546
def _check_at_me(bot: "GitHubBot", event: Event) -> None:
3647
try:
3748
message = event.get_message()
@@ -113,7 +124,7 @@ async def send(
113124
)
114125

115126

116-
class Bot(BaseBot):
127+
class Bot(BaseBot, Generic[A]):
117128
adapter: "Adapter"
118129

119130
send_handler: Callable[
@@ -132,7 +143,7 @@ async def async_graphql(
132143
def __init__(self, adapter: "Adapter", app: Union[GitHubApp, OAuthApp]):
133144
super().__init__(adapter, app.id)
134145
self.app = app
135-
self._github: GitHub
146+
self._github: GitHub[A]
136147
self._ctx_github: ContextVar[Optional[GitHub]] = ContextVar(
137148
"ctx_github", default=None
138149
)
@@ -154,11 +165,11 @@ async def send(
154165
return await self.__class__.send_handler(self, event, message)
155166

156167

157-
class OAuthBot(Bot):
168+
class OAuthBot(Bot[OAuthAppAuthStrategy]):
158169
@overrides(Bot)
159170
def __init__(self, adapter: "Adapter", app: OAuthApp):
160171
super().__init__(adapter, app)
161-
self._github: GitHub[OAuthAppAuthStrategy] = GitHub(
172+
self._github = GitHub(
162173
OAuthAppAuthStrategy(app.client_id, app.client_secret),
163174
base_url=self.adapter.github_config.github_base_url,
164175
accept_format=self.adapter.github_config.github_accept_format,
@@ -200,11 +211,11 @@ async def handle_event(self, event: Event) -> None:
200211
await super().handle_event(event)
201212

202213

203-
class GitHubBot(Bot):
214+
class GitHubBot(Bot[AppAuthStrategy]):
204215
@overrides(Bot)
205216
def __init__(self, adapter: "Adapter", app: GitHubApp):
206217
super().__init__(adapter, app)
207-
self._github: GitHub[AppAuthStrategy] = GitHub(
218+
self._github = GitHub(
208219
AppAuthStrategy(
209220
app.app_id, app.private_key, app.client_id, app.client_secret
210221
),

0 commit comments

Comments
 (0)