跳转至

typing

Ariadne 的类型标注

SendMessageAction 🔗

Bases: Generic[T, R]

表示 SendMessage 的 action

Source code in src/graia/ariadne/typing.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class SendMessageAction(Generic[T, R]):
    """表示 SendMessage 的 action"""

    @staticmethod
    async def param(item: SendMessageDict) -> SendMessageDict:
        """传入 SendMessageDict 作为参数, 传出 SendMessageDict 作为结果

        Args:
            item (SendMessageDict): 调用参数

        Returns:
            SendMessageDict: 修改后的调用参数
        """
        return item

    @staticmethod
    async def result(item: "ActiveMessage") -> R:
        """处理返回结果

        Args:
            item (ActiveMessage): SendMessage 成功时的结果

        Returns:
            R: 要实际由 SendMessage 返回的数据
        """
        return item  # type: ignore

    @staticmethod
    async def exception(item: SendMessageException) -> Optional[T]:
        """发生异常时进行处理,可以选择不返回而是直接引发异常

        Args:
            item (SendMessageException): 发生的异常

        Returns:
            T: 将作为 sendMessage 的返回值
        """
        raise item

exception async staticmethod 🔗

exception(item: SendMessageException) -> Optional[T]

发生异常时进行处理,可以选择不返回而是直接引发异常

Parameters:

Returns:

  • T( Optional[T] ) –

    将作为 sendMessage 的返回值

Source code in src/graia/ariadne/typing.py
104
105
106
107
108
109
110
111
112
113
114
@staticmethod
async def exception(item: SendMessageException) -> Optional[T]:
    """发生异常时进行处理,可以选择不返回而是直接引发异常

    Args:
        item (SendMessageException): 发生的异常

    Returns:
        T: 将作为 sendMessage 的返回值
    """
    raise item

param async staticmethod 🔗

param(item: SendMessageDict) -> SendMessageDict

传入 SendMessageDict 作为参数, 传出 SendMessageDict 作为结果

Parameters:

Returns:

Source code in src/graia/ariadne/typing.py
80
81
82
83
84
85
86
87
88
89
90
@staticmethod
async def param(item: SendMessageDict) -> SendMessageDict:
    """传入 SendMessageDict 作为参数, 传出 SendMessageDict 作为结果

    Args:
        item (SendMessageDict): 调用参数

    Returns:
        SendMessageDict: 修改后的调用参数
    """
    return item

result async staticmethod 🔗

result(item: ActiveMessage) -> R

处理返回结果

Parameters:

Returns:

  • R( R ) –

    要实际由 SendMessage 返回的数据

Source code in src/graia/ariadne/typing.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@staticmethod
async def result(item: "ActiveMessage") -> R:
    """处理返回结果

    Args:
        item (ActiveMessage): SendMessage 成功时的结果

    Returns:
        R: 要实际由 SendMessage 返回的数据
    """
    return item  # type: ignore

SendMessageDict 🔗

Bases: TypedDict

使用 SendMessage 时, 对 action 传入的字典

Source code in src/graia/ariadne/typing.py
58
59
60
61
62
63
class SendMessageDict(TypedDict):
    """使用 SendMessage 时, 对 action 传入的字典"""

    message: "MessageChain"
    target: "Union[Group, Friend, Member]"
    quote: Optional[int]

SendMessageException 🔗

Bases: Exception

携带了 SendMessageDict 的 Exception

Source code in src/graia/ariadne/typing.py
68
69
70
71
class SendMessageException(Exception):
    """携带了 SendMessageDict 的 Exception"""

    send_data: SendMessageDict

class_property 🔗

Bases: Generic[T]

Class-level property. Link: https://stackoverflow.com/a/13624858/1280629

Source code in src/graia/ariadne/typing.py
208
209
210
211
212
213
214
215
216
217
class class_property(Generic[T]):
    """Class-level property.
    Link: https://stackoverflow.com/a/13624858/1280629
    """

    def __init__(self, fget: Callable[[Any], T]):
        self.fget = fget

    def __get__(self, _, owner_cls) -> T:
        return self.fget(owner_cls)

generic_isinstance 🔗

generic_isinstance(obj: Any, par: Union[type, Any, Tuple[type, ...]]) -> bool

检查 obj 是否是 args 中的一个类型, 支持泛型, Any, Union

Parameters:

Returns:

  • bool( bool ) –

    是否是类型

Source code in src/graia/ariadne/typing.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def generic_isinstance(obj: Any, par: Union[type, Any, Tuple[type, ...]]) -> bool:
    """检查 obj 是否是 args 中的一个类型, 支持泛型, Any, Union

    Args:
        obj (Any): 要检查的对象
        par (Union[type, Any, Tuple[type, ...]]): 要检查的对象的类型

    Returns:
        bool: 是否是类型
    """
    if par is Any:
        return True
    with contextlib.suppress(TypeError):
        if isinstance(par, AnnotatedType):
            return generic_isinstance(obj, get_args(par)[0])
        if isinstance(par, (type, tuple)):
            return isinstance(obj, par)
        if get_origin(par) in Unions:
            return any(generic_isinstance(obj, p) for p in get_args(par))
        if isinstance(par, TypeVar):
            if par.__constraints__:
                return any(generic_isinstance(obj, p) for p in par.__constraints__)
            if par.__bound__:
                return generic_isinstance(obj, par.__bound__)
    return False

generic_issubclass 🔗

generic_issubclass(cls: Any, par: Union[type, Any, Tuple[type, ...]]) -> bool

检查 cls 是否是 args 中的一个子类, 支持泛型, Any, Union

Parameters:

Returns:

  • bool( bool ) –

    是否是父类

Source code in src/graia/ariadne/typing.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def generic_issubclass(cls: Any, par: Union[type, Any, Tuple[type, ...]]) -> bool:
    """检查 cls 是否是 args 中的一个子类, 支持泛型, Any, Union

    Args:
        cls (type): 要检查的类
        par (Union[type, Any, Tuple[type, ...]]): 要检查的类的父类

    Returns:
        bool: 是否是父类
    """
    if par is Any:
        return True
    with contextlib.suppress(TypeError):
        if isinstance(par, AnnotatedType):
            return generic_issubclass(cls, get_args(par)[0])
        if isinstance(par, (type, tuple)):
            return issubclass(cls, par)
        if get_origin(par) in Unions:
            return any(generic_issubclass(cls, p) for p in get_args(par))
        if isinstance(par, TypeVar):
            if par.__constraints__:
                return any(generic_issubclass(cls, p) for p in par.__constraints__)
            if par.__bound__:
                return generic_issubclass(cls, par.__bound__)
    return False