mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 03:36:52 +08:00
👽 update type check due to py3.10 UnionType
This commit is contained in:
parent
ea8a700f86
commit
1271a757c9
@ -26,6 +26,8 @@ from typing import (
|
|||||||
ContextManager,
|
ContextManager,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from pydantic.typing import is_union, is_none_type
|
||||||
|
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
|
|
||||||
@ -50,14 +52,18 @@ def escape_tag(s: str) -> str:
|
|||||||
def generic_check_issubclass(
|
def generic_check_issubclass(
|
||||||
cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]
|
cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""检查 cls 是否是 class_or_tuple 中的一个类型子类或"""
|
"""检查 cls 是否是 class_or_tuple 中的一个类型子类。
|
||||||
|
|
||||||
|
特别的,如果 cls 是 `typing.Union` 或 `types.UnionType` 类型,
|
||||||
|
则会检查其中的类型是否是 class_or_tuple 中的一个类型子类。(None 会被忽略)
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return issubclass(cls, class_or_tuple)
|
return issubclass(cls, class_or_tuple)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
origin = get_origin(cls)
|
origin = get_origin(cls)
|
||||||
if origin is Union:
|
if is_union(origin):
|
||||||
for type_ in get_args(cls):
|
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
|
type_, class_or_tuple
|
||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user