Skip to content
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

refactor: 升级 Pyright 并修复类型错误 #134

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions alicebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ async def get(
Raises:
GetEventTimeout: 超过最大事件数或超时。
"""
_func = wrap_get_func(func)
_func = wrap_get_func(func, event_type=event_type, adapter_type=adapter_type)

try_times = 0
start_time = time.time()
Expand All @@ -629,17 +629,6 @@ async def get(
if (
self._current_event is not None
and not self._current_event.__handled__
and (
event_type is None
or isinstance(self._current_event, event_type)
)
and (
adapter_type is None
or isinstance(
self._current_event.adapter, # pyright: ignore[reportUnknownMemberType]
adapter_type,
)
)
and await _func(self._current_event)
):
self._current_event.__handled__ = True
Expand Down Expand Up @@ -899,7 +888,7 @@ def get_adapter(
if _adapter.name == adapter:
return _adapter
elif isinstance(_adapter, adapter):
return _adapter
return _adapter # pyright: ignore[reportUnknownVariableType]
raise LookupError(f'Can not find adapter named "{adapter}"')

def get_plugin(self, name: str) -> type[Plugin[Any, Any, Any]]:
Expand Down
24 changes: 20 additions & 4 deletions alicebot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
if TYPE_CHECKING:
from os import PathLike

from alicebot.adapter import Adapter
from alicebot.event import Event

__all__ = [
"ModulePathFinder",
"is_config_class",
Expand Down Expand Up @@ -224,20 +227,33 @@ async def sync_ctx_manager_wrapper(

def wrap_get_func(
func: Optional[Callable[[EventT], Union[bool, Awaitable[bool]]]],
*,
event_type: Optional[type["Event[Any]"]] = None,
adapter_type: Optional[type["Adapter[Any, Any]"]] = None,
) -> Callable[[EventT], Awaitable[bool]]:
"""将 `get()` 函数接受的参数包装为一个异步函数。

Args:
func: `get()` 函数接受的参数。
event_type: 事件类型。
adapter_type: 适配器类型。

Returns:
异步函数。
"""
if func is None:
return sync_func_wrapper(lambda _: True)
if not asyncio.iscoroutinefunction(func):
return sync_func_wrapper(func) # type: ignore
return func
func = sync_func_wrapper(lambda _: True)
elif not asyncio.iscoroutinefunction(func):
func = sync_func_wrapper(cast(Callable[[EventT], bool], func))

async def _func(event: EventT) -> bool:
return (
(event_type is None or isinstance(event, event_type))
and (adapter_type is None or isinstance(event.adapter, adapter_type)) # pyright: ignore[reportUnknownMemberType]
and await func(event) # pyright: ignore[reportUnknownArgumentType]
)

return _func


if sys.version_info >= (3, 10): # pragma: no cover
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
},
"dependencies": {
"@iconify-json/mdi": "^1.1.67",
"unocss": "^0.58.9",
"unocss": "^0.61.5",
"vitepress": "^1.3.1",
"vue": "^3.4.33"
},
"devDependencies": {
"@antfu/eslint-config": "^2.23.0",
"@types/node": "^20.14.11",
"@unocss/eslint-plugin": "^0.58.9",
"conventional-changelog-cli": "^4.1.0",
"eslint": "^8.57.0",
"markdownlint-cli2": "^0.8.1",
"@antfu/eslint-config": "^2.23.2",
"@types/node": "^20.14.12",
"@unocss/eslint-plugin": "^0.61.5",
"conventional-changelog-cli": "^5.0.0",
"eslint": "^9.7.0",
"markdownlint-cli2": "^0.13.0",
"prettier": "^3.3.3",
"pyright": "^1.1.372",
"pyright": "^1.1.373",
"zhlint": "^0.8.1"
},
"pnpm": {
Expand Down
Loading
Loading