|
17 | 17 | import re |
18 | 18 | import warnings |
19 | 19 | from pathlib import Path |
20 | | -from typing import Any, Callable, Dict, List, Optional, Union |
| 20 | +from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union, get_args, get_origin |
21 | 21 |
|
22 | 22 | import requests |
23 | 23 | import torch |
@@ -1059,3 +1059,76 @@ def _maybe_raise_error_for_incorrect_transformers(config_dict): |
1059 | 1059 | break |
1060 | 1060 | if has_transformers_component and not is_transformers_version(">", "4.47.1"): |
1061 | 1061 | raise ValueError("Please upgrade your `transformers` installation to the latest version to use DDUF.") |
| 1062 | + |
| 1063 | + |
| 1064 | +def _is_valid_type(obj: Any, class_or_tuple: Union[Type, Tuple[Type, ...]]) -> bool: |
| 1065 | + """ |
| 1066 | + Checks if an object is an instance of any of the provided types. For collections, it checks if every element is of |
| 1067 | + the correct type as well. |
| 1068 | + """ |
| 1069 | + if not isinstance(class_or_tuple, tuple): |
| 1070 | + class_or_tuple = (class_or_tuple,) |
| 1071 | + |
| 1072 | + # Unpack unions |
| 1073 | + unpacked_class_or_tuple = [] |
| 1074 | + for t in class_or_tuple: |
| 1075 | + if get_origin(t) is Union: |
| 1076 | + unpacked_class_or_tuple.extend(get_args(t)) |
| 1077 | + else: |
| 1078 | + unpacked_class_or_tuple.append(t) |
| 1079 | + class_or_tuple = tuple(unpacked_class_or_tuple) |
| 1080 | + |
| 1081 | + if Any in class_or_tuple: |
| 1082 | + return True |
| 1083 | + |
| 1084 | + obj_type = type(obj) |
| 1085 | + # Classes with obj's type |
| 1086 | + class_or_tuple = {t for t in class_or_tuple if isinstance(obj, get_origin(t) or t)} |
| 1087 | + |
| 1088 | + # Singular types (e.g. int, ControlNet, ...) |
| 1089 | + # Untyped collections (e.g. List, but not List[int]) |
| 1090 | + elem_class_or_tuple = {get_args(t) for t in class_or_tuple} |
| 1091 | + if () in elem_class_or_tuple: |
| 1092 | + return True |
| 1093 | + # Typed lists or sets |
| 1094 | + elif obj_type in (list, set): |
| 1095 | + return any(all(_is_valid_type(x, t) for x in obj) for t in elem_class_or_tuple) |
| 1096 | + # Typed tuples |
| 1097 | + elif obj_type is tuple: |
| 1098 | + return any( |
| 1099 | + # Tuples with any length and single type (e.g. Tuple[int, ...]) |
| 1100 | + (len(t) == 2 and t[-1] is Ellipsis and all(_is_valid_type(x, t[0]) for x in obj)) |
| 1101 | + or |
| 1102 | + # Tuples with fixed length and any types (e.g. Tuple[int, str]) |
| 1103 | + (len(obj) == len(t) and all(_is_valid_type(x, tt) for x, tt in zip(obj, t))) |
| 1104 | + for t in elem_class_or_tuple |
| 1105 | + ) |
| 1106 | + # Typed dicts |
| 1107 | + elif obj_type is dict: |
| 1108 | + return any( |
| 1109 | + all(_is_valid_type(k, kt) and _is_valid_type(v, vt) for k, v in obj.items()) |
| 1110 | + for kt, vt in elem_class_or_tuple |
| 1111 | + ) |
| 1112 | + |
| 1113 | + else: |
| 1114 | + return False |
| 1115 | + |
| 1116 | + |
| 1117 | +def _get_detailed_type(obj: Any) -> Type: |
| 1118 | + """ |
| 1119 | + Gets a detailed type for an object, including nested types for collections. |
| 1120 | + """ |
| 1121 | + obj_type = type(obj) |
| 1122 | + |
| 1123 | + if obj_type in (list, set): |
| 1124 | + obj_origin_type = List if obj_type is list else Set |
| 1125 | + elems_type = Union[tuple({_get_detailed_type(x) for x in obj})] |
| 1126 | + return obj_origin_type[elems_type] |
| 1127 | + elif obj_type is tuple: |
| 1128 | + return Tuple[tuple(_get_detailed_type(x) for x in obj)] |
| 1129 | + elif obj_type is dict: |
| 1130 | + keys_type = Union[tuple({_get_detailed_type(k) for k in obj.keys()})] |
| 1131 | + values_type = Union[tuple({_get_detailed_type(k) for k in obj.values()})] |
| 1132 | + return Dict[keys_type, values_type] |
| 1133 | + else: |
| 1134 | + return obj_type |
0 commit comments