| 1 |
from typing import ( |
| 2 |
Callable, |
| 3 |
Dict, |
| 4 |
Generic, |
| 5 |
Iterable, |
| 6 |
List, |
| 7 |
Mapping, |
| 8 |
Optional, |
| 9 |
Tuple, |
| 10 |
Type, |
| 11 |
TypeVar, |
| 12 |
Union, |
| 13 |
Sequence, |
| 14 |
Self, |
| 15 |
) |
| 16 |
|
| 17 |
K = TypeVar('K') |
| 18 |
T = TypeVar('T') |
| 19 |
T_co = TypeVar('T_co', covariant=True) |
| 20 |
V = TypeVar('V') |
| 21 |
|
| 22 |
|
| 23 |
just_float: float = 42. |
| 24 |
optional_float: Optional[float] = 42. |
| 25 |
list_of_ints: List[int] = [42] |
| 26 |
list_of_floats: List[float] = [42.] |
| 27 |
list_of_optional_floats: List[Optional[float]] = [x or None for x in list_of_floats] |
| 28 |
list_of_ints_and_strs: List[Union[int, str]] = [42, 'abc'] |
| 29 |
|
| 30 |
|
| 31 |
def list_t_to_list_t(the_list: List[T]) -> List[T]: |
| 32 |
return the_list |
| 33 |
|
| 34 |
x0 = list_t_to_list_t(list_of_ints)[0] |
| 35 |
|
| 36 |
x0 |
| 37 |
|
| 38 |
for a in list_t_to_list_t(list_of_ints): |
| 39 |
|
| 40 |
a |
| 41 |
|
| 42 |
|
| 43 |
x2 = list_t_to_list_t(list_of_ints_and_strs)[0] |
| 44 |
|
| 45 |
x2 |
| 46 |
|
| 47 |
for z in list_t_to_list_t(list_of_ints_and_strs): |
| 48 |
|
| 49 |
z |
| 50 |
|
| 51 |
|
| 52 |
list_of_int_type: List[Type[int]] = [int] |
| 53 |
|
| 54 |
|
| 55 |
def list_optional_t_to_list_t(the_list: List[Optional[T]]) -> List[T]: |
| 56 |
return [x for x in the_list if x is not None] |
| 57 |
|
| 58 |
|
| 59 |
for xa in list_optional_t_to_list_t(list_of_optional_floats): |
| 60 |
|
| 61 |
xa |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
for xa1 in list_optional_t_to_list_t(list_of_floats): |
| 67 |
|
| 68 |
xa1 |
| 69 |
|
| 70 |
|
| 71 |
def optional_t_to_list_t(x: Optional[T]) -> List[T]: |
| 72 |
return [x] if x is not None else [] |
| 73 |
|
| 74 |
|
| 75 |
for xb in optional_t_to_list_t(optional_float): |
| 76 |
|
| 77 |
xb |
| 78 |
|
| 79 |
|
| 80 |
for xb2 in optional_t_to_list_t(just_float): |
| 81 |
|
| 82 |
xb2 |
| 83 |
|
| 84 |
|
| 85 |
def optional_list_t_to_list_t(x: Optional[List[T]]) -> List[T]: |
| 86 |
return x if x is not None else [] |
| 87 |
|
| 88 |
|
| 89 |
optional_list_float: Optional[List[float]] = None |
| 90 |
for xc in optional_list_t_to_list_t(optional_list_float): |
| 91 |
|
| 92 |
xc |
| 93 |
|
| 94 |
for xc2 in optional_list_t_to_list_t(list_of_floats): |
| 95 |
|
| 96 |
xc2 |
| 97 |
|
| 98 |
|
| 99 |
def list_type_t_to_list_t(the_list: List[Type[T]]) -> List[T]: |
| 100 |
return [x() for x in the_list] |
| 101 |
|
| 102 |
|
| 103 |
x1 = list_type_t_to_list_t(list_of_int_type)[0] |
| 104 |
|
| 105 |
x1 |
| 106 |
|
| 107 |
|
| 108 |
for b in list_type_t_to_list_t(list_of_int_type): |
| 109 |
|
| 110 |
b |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
def list_t_to_list_tuple_t(the_list: List[T]) -> List[Tuple[T]]: |
| 115 |
return [(x,) for x in the_list] |
| 116 |
|
| 117 |
|
| 118 |
x1t = list_t_to_list_tuple_t(list_of_ints)[0][0] |
| 119 |
|
| 120 |
x1t |
| 121 |
|
| 122 |
|
| 123 |
for c1 in list_t_to_list_tuple_t(list_of_ints): |
| 124 |
|
| 125 |
c1[0] |
| 126 |
|
| 127 |
|
| 128 |
for c2, in list_t_to_list_tuple_t(list_of_ints): |
| 129 |
|
| 130 |
c2 |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]: |
| 135 |
return tuple(list(x) for x in the_list) |
| 136 |
|
| 137 |
|
| 138 |
list_of_int_tuples: List[Tuple[int]] = [(x,) for x in list_of_ints] |
| 139 |
|
| 140 |
for b in list_tuple_t_to_tuple_list_t(list_of_int_tuples): |
| 141 |
|
| 142 |
b[0] |
| 143 |
|
| 144 |
|
| 145 |
def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple[List[T], ...]: |
| 146 |
return tuple(list(x) for x in the_list) |
| 147 |
|
| 148 |
|
| 149 |
list_of_int_tuple_elipsis: List[Tuple[int, ...]] = [tuple(list_of_ints)] |
| 150 |
|
| 151 |
for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis): |
| 152 |
|
| 153 |
b[0] |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
def foo(x: int) -> int: |
| 158 |
return x |
| 159 |
|
| 160 |
|
| 161 |
list_of_funcs: List[Callable[[int], int]] = [foo] |
| 162 |
|
| 163 |
def list_func_t_to_list_func_type_t(the_list: List[Callable[[T], T]]) -> List[Callable[[Type[T]], T]]: |
| 164 |
def adapt(func: Callable[[T], T]) -> Callable[[Type[T]], T]: |
| 165 |
def wrapper(typ: Type[T]) -> T: |
| 166 |
return func(typ()) |
| 167 |
return wrapper |
| 168 |
return [adapt(x) for x in the_list] |
| 169 |
|
| 170 |
|
| 171 |
for b in list_func_t_to_list_func_type_t(list_of_funcs): |
| 172 |
|
| 173 |
b(int) |
| 174 |
|
| 175 |
|
| 176 |
def bar(*a, **k) -> int: |
| 177 |
return len(a) + len(k) |
| 178 |
|
| 179 |
|
| 180 |
list_of_funcs_2: List[Callable[..., int]] = [bar] |
| 181 |
|
| 182 |
def list_func_t_passthrough(the_list: List[Callable[..., T]]) -> List[Callable[..., T]]: |
| 183 |
return the_list |
| 184 |
|
| 185 |
|
| 186 |
for b in list_func_t_passthrough(list_of_funcs_2): |
| 187 |
|
| 188 |
b(None, x="x") |
| 189 |
|
| 190 |
|
| 191 |
mapping_int_str: Dict[int, str] = {42: 'a'} |
| 192 |
|
| 193 |
|
| 194 |
def invert_mapping(mapping: Mapping[K, V]) -> Mapping[V, K]: |
| 195 |
return {v: k for k, v in mapping.items()} |
| 196 |
|
| 197 |
|
| 198 |
invert_mapping(mapping_int_str)['a'] |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
def first(iterable: Iterable[T]) -> T: |
| 205 |
return next(iter(iterable)) |
| 206 |
|
| 207 |
|
| 208 |
first(mapping_int_str) |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
first("abc") |
| 213 |
|
| 214 |
some_str: str = NotImplemented |
| 215 |
|
| 216 |
first(some_str) |
| 217 |
|
| 218 |
annotated: List[ Callable[[Sequence[float]], int] ] = [len] |
| 219 |
|
| 220 |
first(annotated)() |
| 221 |
|
| 222 |
|
| 223 |
def values(mapping: Mapping[int, T]) -> List[T]: |
| 224 |
return list(mapping.values()) |
| 225 |
|
| 226 |
|
| 227 |
values(mapping_int_str)[0] |
| 228 |
|
| 229 |
x2 = values(mapping_int_str)[0] |
| 230 |
|
| 231 |
x2 |
| 232 |
|
| 233 |
for b in values(mapping_int_str): |
| 234 |
|
| 235 |
b |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
list_ints: List[int] = [42] |
| 242 |
|
| 243 |
class CustomGeneric(Generic[T_co]): |
| 244 |
def __init__(self, val: T_co) -> None: |
| 245 |
self.val = val |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
def custom(x: CustomGeneric[T]) -> T: |
| 250 |
return x.val |
| 251 |
|
| 252 |
custom_instance: CustomGeneric[int] = CustomGeneric(42) |
| 253 |
|
| 254 |
|
| 255 |
custom(custom_instance) |
| 256 |
|
| 257 |
x3 = custom(custom_instance) |
| 258 |
|
| 259 |
x3 |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
def wrap_custom(iterable: Iterable[T]) -> List[CustomGeneric[T]]: |
| 264 |
return [CustomGeneric(x) for x in iterable] |
| 265 |
|
| 266 |
|
| 267 |
wrap_custom(list_ints)[0].val |
| 268 |
|
| 269 |
x4 = wrap_custom(list_ints)[0] |
| 270 |
|
| 271 |
x4.val |
| 272 |
|
| 273 |
for x5 in wrap_custom(list_ints): |
| 274 |
|
| 275 |
x5.val |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
list_custom_instances: List[CustomGeneric[int]] = [CustomGeneric(42)] |
| 280 |
|
| 281 |
def unwrap_custom(iterable: Iterable[CustomGeneric[T]]) -> List[T]: |
| 282 |
return [x.val for x in iterable] |
| 283 |
|
| 284 |
|
| 285 |
unwrap_custom(list_custom_instances)[0] |
| 286 |
|
| 287 |
x6 = unwrap_custom(list_custom_instances)[0] |
| 288 |
|
| 289 |
x6 |
| 290 |
|
| 291 |
for x7 in unwrap_custom(list_custom_instances): |
| 292 |
|
| 293 |
x7 |
| 294 |
|
| 295 |
|
| 296 |
for xc in unwrap_custom([CustomGeneric(s) for s in 'abc']): |
| 297 |
|
| 298 |
xc |
| 299 |
|
| 300 |
|
| 301 |
for xg in unwrap_custom(CustomGeneric(s) for s in 'abc'): |
| 302 |
|
| 303 |
xg |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
custom_instance_list_int: CustomGeneric[List[int]] = CustomGeneric([42]) |
| 308 |
|
| 309 |
def unwrap_custom2(instance: CustomGeneric[Iterable[T]]) -> List[T]: |
| 310 |
return list(instance.val) |
| 311 |
|
| 312 |
|
| 313 |
unwrap_custom2(custom_instance_list_int)[0] |
| 314 |
|
| 315 |
x8 = unwrap_custom2(custom_instance_list_int)[0] |
| 316 |
|
| 317 |
x8 |
| 318 |
|
| 319 |
for x9 in unwrap_custom2(custom_instance_list_int): |
| 320 |
|
| 321 |
x9 |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
class Specialised(Mapping[int, str]): |
| 327 |
pass |
| 328 |
|
| 329 |
|
| 330 |
specialised_instance: Specialised = NotImplemented |
| 331 |
|
| 332 |
|
| 333 |
first(specialised_instance) |
| 334 |
|
| 335 |
|
| 336 |
values(specialised_instance)[0] |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
class ChildOfSpecialised(Specialised): |
| 342 |
pass |
| 343 |
|
| 344 |
|
| 345 |
child_of_specialised_instance: ChildOfSpecialised = NotImplemented |
| 346 |
|
| 347 |
|
| 348 |
first(child_of_specialised_instance) |
| 349 |
|
| 350 |
|
| 351 |
values(child_of_specialised_instance)[0] |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
class CustomPartialGeneric1(Mapping[str, T]): |
| 356 |
pass |
| 357 |
|
| 358 |
|
| 359 |
custom_partial1_instance: CustomPartialGeneric1[int] = NotImplemented |
| 360 |
|
| 361 |
|
| 362 |
first(custom_partial1_instance) |
| 363 |
|
| 364 |
|
| 365 |
custom_partial1_unbound_instance: CustomPartialGeneric1 = NotImplemented |
| 366 |
|
| 367 |
|
| 368 |
first(custom_partial1_unbound_instance) |
| 369 |
|
| 370 |
|
| 371 |
class CustomPartialGeneric2(Mapping[T, str]): |
| 372 |
pass |
| 373 |
|
| 374 |
|
| 375 |
custom_partial2_instance: CustomPartialGeneric2[int] = NotImplemented |
| 376 |
|
| 377 |
|
| 378 |
first(custom_partial2_instance) |
| 379 |
|
| 380 |
|
| 381 |
values(custom_partial2_instance)[0] |
| 382 |
|
| 383 |
|
| 384 |
custom_partial2_unbound_instance: CustomPartialGeneric2 = NotImplemented |
| 385 |
|
| 386 |
|
| 387 |
first(custom_partial2_unbound_instance) |
| 388 |
|
| 389 |
|
| 390 |
values(custom_partial2_unbound_instance)[0] |
| 391 |
|
| 392 |
def generic_func1(arg: T) -> int | str | T: pass |
| 393 |
def generic_func2(arg: T) -> Union[int, str, T]: pass |
| 394 |
|
| 395 |
|
| 396 |
generic_func1(b"hello") |
| 397 |
|
| 398 |
generic_func2(b"hello") |
| 399 |
|
| 400 |
class CustomGeneric2(Generic[T_co]): |
| 401 |
val: T_co |
| 402 |
def __init__(cls, val: T_co) -> Self: |
| 403 |
raise NotImplementedError |
| 404 |
|
| 405 |
|
| 406 |
CustomGeneric2(1).val |