👽 update type check due to py3.10 UnionType

This commit is contained in:
yanyongyu 2022-01-28 14:27:54 +08:00
parent ea8a700f86
commit 1271a757c9
No known key found for this signature in database
GPG Key ID: 796D8A7FB73396EB

View File

@ -26,6 +26,8 @@ from typing import (
ContextManager,
)
from pydantic.typing import is_union, is_none_type
from nonebot.log import logger
from nonebot.typing import overrides
@ -50,14 +52,18 @@ def escape_tag(s: str) -> str:
def generic_check_issubclass(
cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]
) -> bool:
"""检查 cls 是否是 class_or_tuple 中的一个类型子类或"""
"""检查 cls 是否是 class_or_tuple 中的一个类型子类。
特别的如果 cls `typing.Union` `types.UnionType` 类型
则会检查其中的类型是否是 class_or_tuple 中的一个类型子类None 会被忽略
"""
try:
return issubclass(cls, class_or_tuple)
except TypeError:
origin = get_origin(cls)
if origin is Union:
if is_union(origin):
for type_ in get_args(cls):
if type_ is not type(None) and not generic_check_issubclass(
if not is_none_type(type_) and not generic_check_issubclass(
type_, class_or_tuple
):
return False