| 1 |
from typing import ( |
| 2 |
Any, |
| 3 |
Callable, |
| 4 |
Iterable, |
| 5 |
List, |
| 6 |
Sequence, |
| 7 |
Tuple, |
| 8 |
Type, |
| 9 |
TypeVar, |
| 10 |
Union, |
| 11 |
Generic, |
| 12 |
) |
| 13 |
|
| 14 |
T = TypeVar('T') |
| 15 |
U = TypeVar('U') |
| 16 |
TList = TypeVar('TList', bound=List[Any]) |
| 17 |
TType = TypeVar('TType', bound=Type) |
| 18 |
TTypeAny = TypeVar('TTypeAny', bound=Type[Any]) |
| 19 |
TCallable = TypeVar('TCallable', bound=Callable[..., Any]) |
| 20 |
|
| 21 |
untyped_list_str = ['abc', 'def'] |
| 22 |
typed_list_str: List[str] = ['abc', 'def'] |
| 23 |
|
| 24 |
untyped_tuple_str = ('abc',) |
| 25 |
typed_tuple_str: Tuple[str] = ('abc',) |
| 26 |
|
| 27 |
untyped_tuple_str_int = ('abc', 4) |
| 28 |
typed_tuple_str_int: Tuple[str, int] = ('abc', 4) |
| 29 |
|
| 30 |
variadic_tuple_str: Tuple[str, ...] = ('abc',) |
| 31 |
variadic_tuple_str_int: Tuple[Union[str, int], ...] = ('abc', 4) |
| 32 |
|
| 33 |
|
| 34 |
def untyped_passthrough(x): |
| 35 |
return x |
| 36 |
|
| 37 |
def typed_list_generic_passthrough(x: List[T]) -> List[T]: |
| 38 |
return x |
| 39 |
|
| 40 |
def typed_tuple_generic_passthrough(x: Tuple[T]) -> Tuple[T]: |
| 41 |
return x |
| 42 |
|
| 43 |
def typed_multi_typed_tuple_generic_passthrough(x: Tuple[T, U]) -> Tuple[U, T]: |
| 44 |
return x[1], x[0] |
| 45 |
|
| 46 |
def typed_variadic_tuple_generic_passthrough(x: Tuple[T, ...]) -> Sequence[T]: |
| 47 |
return x |
| 48 |
|
| 49 |
def typed_iterable_generic_passthrough(x: Iterable[T]) -> Iterable[T]: |
| 50 |
return x |
| 51 |
|
| 52 |
def typed_fully_generic_passthrough(x: T) -> T: |
| 53 |
return x |
| 54 |
|
| 55 |
def typed_bound_generic_passthrough(x: TList) -> TList: |
| 56 |
|
| 57 |
x |
| 58 |
|
| 59 |
return x |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def typed_quoted_return_generic_passthrough(x: T) -> 'List[T]': |
| 65 |
return [x] |
| 66 |
|
| 67 |
def typed_quoted_input_generic_passthrough(x: 'Tuple[T]') -> T: |
| 68 |
x |
| 69 |
return x[0] |
| 70 |
|
| 71 |
|
| 72 |
for a in untyped_passthrough(untyped_list_str): |
| 73 |
|
| 74 |
a |
| 75 |
|
| 76 |
for b in untyped_passthrough(typed_list_str): |
| 77 |
|
| 78 |
b |
| 79 |
|
| 80 |
|
| 81 |
for c in typed_list_generic_passthrough(untyped_list_str): |
| 82 |
|
| 83 |
c |
| 84 |
|
| 85 |
for d in typed_list_generic_passthrough(typed_list_str): |
| 86 |
|
| 87 |
d |
| 88 |
|
| 89 |
|
| 90 |
for e in typed_iterable_generic_passthrough(untyped_list_str): |
| 91 |
|
| 92 |
e |
| 93 |
|
| 94 |
for f in typed_iterable_generic_passthrough(typed_list_str): |
| 95 |
|
| 96 |
f |
| 97 |
|
| 98 |
|
| 99 |
for g in typed_tuple_generic_passthrough(untyped_tuple_str): |
| 100 |
|
| 101 |
g |
| 102 |
|
| 103 |
for h in typed_tuple_generic_passthrough(typed_tuple_str): |
| 104 |
|
| 105 |
h |
| 106 |
|
| 107 |
|
| 108 |
out_untyped = typed_multi_typed_tuple_generic_passthrough(untyped_tuple_str_int) |
| 109 |
|
| 110 |
out_untyped[0] |
| 111 |
|
| 112 |
out_untyped[1] |
| 113 |
|
| 114 |
|
| 115 |
out_typed = typed_multi_typed_tuple_generic_passthrough(typed_tuple_str_int) |
| 116 |
|
| 117 |
out_typed[0] |
| 118 |
|
| 119 |
out_typed[1] |
| 120 |
|
| 121 |
|
| 122 |
for j in typed_variadic_tuple_generic_passthrough(untyped_tuple_str_int): |
| 123 |
|
| 124 |
j |
| 125 |
|
| 126 |
for k in typed_variadic_tuple_generic_passthrough(typed_tuple_str_int): |
| 127 |
|
| 128 |
k |
| 129 |
|
| 130 |
for l in typed_variadic_tuple_generic_passthrough(variadic_tuple_str): |
| 131 |
|
| 132 |
l |
| 133 |
|
| 134 |
for m in typed_variadic_tuple_generic_passthrough(variadic_tuple_str_int): |
| 135 |
|
| 136 |
m |
| 137 |
|
| 138 |
|
| 139 |
typed_fully_generic_passthrough(float) |
| 140 |
|
| 141 |
for n in typed_fully_generic_passthrough(untyped_list_str): |
| 142 |
|
| 143 |
n |
| 144 |
|
| 145 |
for o in typed_fully_generic_passthrough(typed_list_str): |
| 146 |
|
| 147 |
o |
| 148 |
|
| 149 |
|
| 150 |
for p in typed_bound_generic_passthrough(untyped_list_str): |
| 151 |
|
| 152 |
p |
| 153 |
|
| 154 |
for q in typed_bound_generic_passthrough(typed_list_str): |
| 155 |
|
| 156 |
q |
| 157 |
|
| 158 |
|
| 159 |
for r in typed_quoted_return_generic_passthrough("something"): |
| 160 |
|
| 161 |
r |
| 162 |
|
| 163 |
for s in typed_quoted_return_generic_passthrough(42): |
| 164 |
|
| 165 |
s |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
typed_quoted_input_generic_passthrough(("something",)) |
| 170 |
|
| 171 |
|
| 172 |
typed_quoted_input_generic_passthrough((42,)) |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
class CustomList(List): |
| 177 |
def get_first(self): |
| 178 |
return self[0] |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
CustomList[str]()[0] |
| 183 |
|
| 184 |
CustomList[str]().get_first() |
| 185 |
|
| 186 |
|
| 187 |
typed_fully_generic_passthrough(CustomList[str]())[0] |
| 188 |
|
| 189 |
typed_list_generic_passthrough(CustomList[str])[0] |
| 190 |
|
| 191 |
|
| 192 |
def typed_bound_type_implicit_any_generic_passthrough(x: TType) -> TType: |
| 193 |
|
| 194 |
x |
| 195 |
return x |
| 196 |
|
| 197 |
def typed_bound_type_any_generic_passthrough(x: TTypeAny) -> TTypeAny: |
| 198 |
|
| 199 |
|
| 200 |
x |
| 201 |
return x |
| 202 |
|
| 203 |
|
| 204 |
class MyClass: |
| 205 |
pass |
| 206 |
|
| 207 |
def my_func(a: str, b: int) -> float: |
| 208 |
pass |
| 209 |
|
| 210 |
|
| 211 |
typed_fully_generic_passthrough(MyClass) |
| 212 |
|
| 213 |
|
| 214 |
typed_fully_generic_passthrough(MyClass()) |
| 215 |
|
| 216 |
|
| 217 |
typed_fully_generic_passthrough(my_func) |
| 218 |
|
| 219 |
|
| 220 |
typed_bound_generic_passthrough(CustomList[str]()) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
typed_bound_generic_passthrough(42) |
| 225 |
|
| 226 |
|
| 227 |
typed_bound_type_implicit_any_generic_passthrough(MyClass) |
| 228 |
|
| 229 |
|
| 230 |
typed_bound_type_any_generic_passthrough(MyClass) |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
typed_bound_type_implicit_any_generic_passthrough(42) |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
typed_bound_type_any_generic_passthrough(42) |
| 239 |
|
| 240 |
|
| 241 |
def decorator(fn: TCallable) -> TCallable: |
| 242 |
pass |
| 243 |
|
| 244 |
|
| 245 |
def will_be_decorated(the_param: complex) -> float: |
| 246 |
pass |
| 247 |
|
| 248 |
|
| 249 |
is_decorated = decorator(will_be_decorated) |
| 250 |
|
| 251 |
|
| 252 |
is_decorated |
| 253 |
|
| 254 |
|
| 255 |
is_decorated(the_para |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
class class_decorator_factory_plain: |
| 260 |
def __call__(self, func: T) -> T: |
| 261 |
... |
| 262 |
|
| 263 |
|
| 264 |
class_decorator_factory_plain() |
| 265 |
|
| 266 |
|
| 267 |
class_decorator_factory_plain()() |
| 268 |
|
| 269 |
is_decorated_by_class_decorator_factory = class_decorator_factory_plain()(will_be_decorated) |
| 270 |
|
| 271 |
|
| 272 |
is_decorated_by_class_decorator_factory |
| 273 |
|
| 274 |
|
| 275 |
is_decorated_by_class_decorator_factory(the_par |
| 276 |
) |
| 277 |
|
| 278 |
|
| 279 |
def decorator_factory_plain() -> Callable[[T], T]: |
| 280 |
pass |
| 281 |
|
| 282 |
|
| 283 |
decorator_factory_plain() |
| 284 |
|
| 285 |
|
| 286 |
decorator_factory_plain()() |
| 287 |
|
| 288 |
|
| 289 |
decorator_factory_plain()(42) |
| 290 |
|
| 291 |
is_decorated_by_plain_factory = decorator_factory_plain()(will_be_decorated) |
| 292 |
|
| 293 |
|
| 294 |
is_decorated_by_plain_factory |
| 295 |
|
| 296 |
|
| 297 |
is_decorated_by_plain_factory(the_par |
| 298 |
) |
| 299 |
|
| 300 |
|
| 301 |
class class_decorator_factory_bound_callable: |
| 302 |
def __call__(self, func: TCallable) -> TCallable: |
| 303 |
... |
| 304 |
|
| 305 |
|
| 306 |
class_decorator_factory_bound_callable() |
| 307 |
|
| 308 |
|
| 309 |
class_decorator_factory_bound_callable()() |
| 310 |
|
| 311 |
is_decorated_by_class_bound_factory = class_decorator_factory_bound_callable()(will_be_decorated) |
| 312 |
|
| 313 |
|
| 314 |
is_decorated_by_class_bound_factory |
| 315 |
|
| 316 |
|
| 317 |
is_decorated_by_class_bound_factory(the_par |
| 318 |
) |
| 319 |
|
| 320 |
|
| 321 |
def decorator_factory_bound_callable() -> Callable[[TCallable], TCallable]: |
| 322 |
pass |
| 323 |
|
| 324 |
|
| 325 |
decorator_factory_bound_callable() |
| 326 |
|
| 327 |
|
| 328 |
decorator_factory_bound_callable()() |
| 329 |
|
| 330 |
is_decorated_by_bound_factory = decorator_factory_bound_callable()(will_be_decorated) |
| 331 |
|
| 332 |
|
| 333 |
is_decorated_by_bound_factory |
| 334 |
|
| 335 |
|
| 336 |
is_decorated_by_bound_factory(the_par |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
class That(Generic[T]): |
| 341 |
def __init__(self, items: List[Tuple[str, T]]) -> None: |
| 342 |
pass |
| 343 |
|
| 344 |
def get(self) -> T: |
| 345 |
pass |
| 346 |
|
| 347 |
inst = That([("abc", 2)]) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
inst.get() |