1import typing 2from typing import ( 3 Callable, 4 Dict, 5 Generic, 6 List, 7 Sequence, 8 Tuple, 9 Type, 10 TypeVar, 11) 12 13T = TypeVar('T') 14 15 16def foo(x: T) -> T: 17 return x 18 19 20class CustomGeneric(Generic[T]): 21 def __init__(self, val: T) -> None: 22 self.val = val 23 24 25class PlainClass(object): 26 pass 27 28 29tpl = ("1", 2) 30tpl_typed = ("2", 3) # type: Tuple[str, int] 31 32collection = {"a": 1} 33collection_typed = {"a": 1} # type: Dict[str, int] 34 35list_of_ints = [42] # type: List[int] 36list_of_funcs = [foo] # type: List[Callable[[T], T]] 37 38custom_generic = CustomGeneric(123.45) 39 40plain_instance = PlainClass() 41 42 43# Test that simple parameters are handled 44def list_t_to_list_t(the_list: List[T]) -> List[T]: 45 return the_list 46 47x0 = list_t_to_list_t("abc")[0] 48#? 49x0 50 51x1 = list_t_to_list_t(foo)[0] 52#? 53x1 54 55x1 = list_t_to_list_t(typing)[0] 56#? 57x1 58 59x2 = list_t_to_list_t(tpl)[0] 60#? 61x2 62 63x3 = list_t_to_list_t(tpl_typed)[0] 64#? 65x3 66 67x4 = list_t_to_list_t(collection)[0] 68#? 69x4 70 71x5 = list_t_to_list_t(collection_typed)[0] 72#? 73x5 74 75x6 = list_t_to_list_t(custom_generic)[0] 76#? 77x6 78 79x7 = list_t_to_list_t(plain_instance)[0] 80#? 81x7 82 83for a in list_t_to_list_t(12): 84 #? 85 a 86 87 88# Test that simple parameters are handled 89def list_type_t_to_list_t(the_list: List[Type[T]]) -> List[T]: 90 return [x() for x in the_list] 91 92x0 = list_type_t_to_list_t("abc")[0] 93#? 94x0 95 96x1 = list_type_t_to_list_t(foo)[0] 97#? 98x1 99 100x2 = list_type_t_to_list_t(tpl)[0] 101#? 102x2 103 104x3 = list_type_t_to_list_t(tpl_typed)[0] 105#? 106x3 107 108x4 = list_type_t_to_list_t(collection)[0] 109#? 110x4 111 112x5 = list_type_t_to_list_t(collection_typed)[0] 113#? 114x5 115 116x6 = list_type_t_to_list_t(custom_generic)[0] 117#? 118x6 119 120x7 = list_type_t_to_list_t(plain_instance)[0] 121#? 122x7 123 124for a in list_type_t_to_list_t(12): 125 #? 126 a 127 128 129x0 = list_type_t_to_list_t(["abc"])[0] 130#? 131x0 132 133x1 = list_type_t_to_list_t([foo])[0] 134#? 135x1 136 137x2 = list_type_t_to_list_t([tpl])[0] 138#? 139x2 140 141x3 = list_type_t_to_list_t([tpl_typed])[0] 142#? 143x3 144 145x4 = list_type_t_to_list_t([collection])[0] 146#? 147x4 148 149x5 = list_type_t_to_list_t([collection_typed])[0] 150#? 151x5 152 153x6 = list_type_t_to_list_t([custom_generic])[0] 154#? 155x6 156 157x7 = list_type_t_to_list_t([plain_instance])[0] 158#? 159x7 160 161for a in list_type_t_to_list_t([12]): 162 #? 163 a 164 165 166def list_func_t_to_list_t(the_list: List[Callable[[T], T]]) -> List[T]: 167 # Not actually a viable signature, but should be enough to test our handling 168 # of the generic parameters. 169 pass 170 171 172x0 = list_func_t_to_list_t("abc")[0] 173#? 174x0 175 176x1 = list_func_t_to_list_t(foo)[0] 177#? 178x1 179 180x2 = list_func_t_to_list_t(tpl)[0] 181#? 182x2 183 184x3 = list_func_t_to_list_t(tpl_typed)[0] 185#? 186x3 187 188x4 = list_func_t_to_list_t(collection)[0] 189#? 190x4 191 192x5 = list_func_t_to_list_t(collection_typed)[0] 193#? 194x5 195 196x6 = list_func_t_to_list_t(custom_generic)[0] 197#? 198x6 199 200x7 = list_func_t_to_list_t(plain_instance)[0] 201#? 202x7 203 204for a in list_func_t_to_list_t(12): 205 #? 206 a 207 208 209x0 = list_func_t_to_list_t(["abc"])[0] 210#? 211x0 212 213x2 = list_func_t_to_list_t([tpl])[0] 214#? 215x2 216 217x3 = list_func_t_to_list_t([tpl_typed])[0] 218#? 219x3 220 221x4 = list_func_t_to_list_t([collection])[0] 222#? 223x4 224 225x5 = list_func_t_to_list_t([collection_typed])[0] 226#? 227x5 228 229x6 = list_func_t_to_list_t([custom_generic])[0] 230#? 231x6 232 233x7 = list_func_t_to_list_t([plain_instance])[0] 234#? 235x7 236 237for a in list_func_t_to_list_t([12]): 238 #? 239 a 240 241 242def tuple_t(tuple_in: Tuple[T]]) -> Sequence[T]: 243 return tuple_in 244 245 246x0 = list_t_to_list_t("abc")[0] 247#? 248x0 249 250x1 = list_t_to_list_t(foo)[0] 251#? 252x1 253 254x2 = list_t_to_list_t(tpl)[0] 255#? 256x2 257 258x3 = list_t_to_list_t(tpl_typed)[0] 259#? 260x3 261 262x4 = list_t_to_list_t(collection)[0] 263#? 264x4 265 266x5 = list_t_to_list_t(collection_typed)[0] 267#? 268x5 269 270x6 = list_t_to_list_t(custom_generic)[0] 271#? 272x6 273 274x7 = list_t_to_list_t(plain_instance)[0] 275#? 276x7 277 278for a in list_t_to_list_t(12): 279 #? 280 a 281 282 283def tuple_t_elipsis(tuple_in: Tuple[T, ...]]) -> Sequence[T]: 284 return tuple_in 285 286 287x0 = list_t_to_list_t("abc")[0] 288#? 289x0 290 291x1 = list_t_to_list_t(foo)[0] 292#? 293x1 294 295x2 = list_t_to_list_t(tpl)[0] 296#? 297x2 298 299x3 = list_t_to_list_t(tpl_typed)[0] 300#? 301x3 302 303x4 = list_t_to_list_t(collection)[0] 304#? 305x4 306 307x5 = list_t_to_list_t(collection_typed)[0] 308#? 309x5 310 311x6 = list_t_to_list_t(custom_generic)[0] 312#? 313x6 314 315x7 = list_t_to_list_t(plain_instance)[0] 316#? 317x7 318 319for a in list_t_to_list_t(12): 320 #? 321 a 322 323 324def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]: 325 return tuple(list(x) for x in the_list) 326 327 328for b in list_tuple_t_to_tuple_list_t(list_of_ints): 329 #? 330 b[0] 331 332 333def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple[List[T], ...]: 334 return tuple(list(x) for x in the_list) 335 336 337for b in list_tuple_t_to_tuple_list_t(list_of_ints): 338 #? 339 b[0] 340