你可以用
issubclass
要检查这样的类型:
from typing import Dict, List, Type, TypeVar
IntList = List[int]
StrList = List[str]
IntStrDict = Dict[int, str]
TypeT = TypeVar('TypeT')
# todo: Solve without using string representation of type
def is_list_type(the_type: Type[TypeT]) -> bool:
return issubclass(the_type, List)
assert not is_list_type(IntStrDict)
assert not is_list_type(int)
assert not is_list_type(str)
assert is_list_type(IntList)
assert is_list_type(StrList)