跳转至

typing

Ariadne 的类型标注

SendMessageAction 🔗

Bases: Generic[T, R]

表示 SendMessage 的 action

Source code in graia/ariadne/typing.py
 74
 75
 76
 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
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 staticmethod async 🔗

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

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

Parameters:

Returns:

  • T( Optional[T] ) –

    将作为 sendMessage 的返回值

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

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

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

param staticmethod async 🔗

param(item: SendMessageDict) -> SendMessageDict

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

Parameters:

Returns:

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

    Args:
        item (SendMessageDict): 调用参数

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

result staticmethod async 🔗

result(item: ActiveMessage) -> R

处理返回结果

Parameters:

Returns:

  • R( R ) –

    要实际由 SendMessage 返回的数据

Source code in graia/ariadne/typing.py
89
90
91
92
93
94
95
96
97
98
99
@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 graia/ariadne/typing.py
55
56
57
58
59
60
class SendMessageDict(TypedDict):
    """使用 SendMessage 时, 对 action 传入的字典"""

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

SendMessageException 🔗

Bases: Exception

携带了 SendMessageDict 的 Exception

Source code in graia/ariadne/typing.py
65
66
67
68
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 graia/ariadne/typing.py
202
203
204
205
206
207
208
209
210
211
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 graia/ariadne/typing.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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 graia/ariadne/typing.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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