@@ -97,7 +97,18 @@ def generic_check_issubclass(
9797 则会检查其 `__bound__` 或 `__constraints__`
9898 是否是 class_or_tuple 中一个类型的子类或 None。
9999 """
100- if not type_has_args (cls ):
100+ # if the target is a TypeVar, we check it first
101+ if isinstance (cls , TypeVar ):
102+ if cls .__constraints__ :
103+ return all (
104+ is_none_type (type_ ) or generic_check_issubclass (type_ , class_or_tuple )
105+ for type_ in cls .__constraints__
106+ )
107+ elif cls .__bound__ :
108+ return generic_check_issubclass (cls .__bound__ , class_or_tuple )
109+ return False
110+ # elif the target is not a generic type, we check it directly
111+ elif not type_has_args (cls ):
101112 with contextlib .suppress (TypeError ):
102113 return issubclass (cls , class_or_tuple )
103114
@@ -119,14 +130,6 @@ def generic_check_issubclass(
119130 return issubclass (origin , class_or_tuple )
120131 except TypeError :
121132 return False
122- elif isinstance (cls , TypeVar ):
123- if cls .__constraints__ :
124- return all (
125- is_none_type (type_ ) or generic_check_issubclass (type_ , class_or_tuple )
126- for type_ in cls .__constraints__
127- )
128- elif cls .__bound__ :
129- return generic_check_issubclass (cls .__bound__ , class_or_tuple )
130133 return False
131134
132135
0 commit comments