Skip to content

Commit

Permalink
🎨 *mostly* type safe
Browse files Browse the repository at this point in the history
See #55

Co-authored-by: Elaina <[email protected]>
  • Loading branch information
BlueGlassBlock and GreyElaina committed Mar 23, 2022
1 parent 55ebdd5 commit 64bbc08
Show file tree
Hide file tree
Showing 25 changed files with 968 additions and 512 deletions.
144 changes: 92 additions & 52 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ exclude_lines = [
# Don't complain overload method / functions
"@(typing\\.)?overload"
]

[tool.pyright]
ignore = [
"docs/**",
"**/site-packages/**/*.py",
"**/test*/**/*.py",
"**/adapter/**"
]
8 changes: 5 additions & 3 deletions src/graia/ariadne/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def get_running(type: Type[T] = Ariadne) -> T:
from .context import context_map

if type in {Adapter, Ariadne, Broadcast, AbstractEventLoop}:
if val := context_map.get(type.__name__).get(None):
return val
if ctx := context_map.get(type.__name__):
if val := ctx.get(None):
return val
for ariadne_inst in Ariadne.running:
if type in ariadne_inst.info:
return ariadne_inst.info[type]
return ariadne_inst.info[type] # type: ignore
raise ValueError(f"{type.__name__} is not running")
12 changes: 5 additions & 7 deletions src/graia/ariadne/adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Ariadne 的适配器"""
import abc
import asyncio
import contextlib
from asyncio import Queue, Task
from typing import Any, Dict, FrozenSet, Optional, Union

Expand Down Expand Up @@ -42,7 +43,7 @@ async def call_api(
action: str,
method: CallMethod,
data: Optional[Union[Dict[str, Any], str, FormData]] = None,
) -> Union[dict, list]:
) -> Dict[Any, Any]:
"""调用 API
Args:
Expand All @@ -51,7 +52,7 @@ async def call_api(
data (Optional[Union[Dict[str, Any], str, FormData]], optional): 调用数据. Defaults to None.
Returns:
Union[dict, list]: API 返回的数据, 为 json 兼容格式
dict: API 返回的数据, 为 json 兼容格式
"""
...

Expand Down Expand Up @@ -86,8 +87,7 @@ def build_event(self, data: dict) -> MiraiEvent:
logger.error("An event is not recognized! Please report with your log to help us diagnose.")
raise ValueError(f"Unable to find event: {event_type}", data)
data = {k: v for k, v in data.items() if k != "type"}
event = event_class.parse_obj(data)
return event
return event_class.parse_obj(data)

@abc.abstractmethod
async def fetch_cycle(self):
Expand Down Expand Up @@ -118,7 +118,7 @@ def build_event(self, data: dict) -> MiraiEvent:
return event


try:
with contextlib.suppress(ImportError):
from .reverse import ( # noqa: F401, E402
ComposeReverseWebsocketAdapter as ComposeReverseWebsocketAdapter,
)
Expand All @@ -129,5 +129,3 @@ def build_event(self, data: dict) -> MiraiEvent:
from .reverse import ( # noqa: F401, E402
ReverseWebsocketAdapter as ReverseWebsocketAdapter,
)
except ImportError:
pass
Loading

0 comments on commit 64bbc08

Please sign in to comment.