| 1 |
""" |
| 2 |
Test the typing library, with docstrings and annotations |
| 3 |
""" |
| 4 |
import typing |
| 5 |
from typing import Sequence, MutableSequence, List, Iterable, Iterator, \ |
| 6 |
AbstractSet, Tuple, Mapping, Dict, Union, Optional, Final, Self |
| 7 |
|
| 8 |
class B: |
| 9 |
pass |
| 10 |
|
| 11 |
def we_can_has_sequence(p: Sequence[int], q: Sequence[B], r: Sequence[int], |
| 12 |
s: Sequence["int"], t: MutableSequence[dict], u: List[float]): |
| 13 |
|
| 14 |
p.c |
| 15 |
|
| 16 |
p[1] |
| 17 |
|
| 18 |
q.c |
| 19 |
|
| 20 |
q[1] |
| 21 |
|
| 22 |
r.c |
| 23 |
|
| 24 |
r[1] |
| 25 |
|
| 26 |
s.c |
| 27 |
|
| 28 |
s[1] |
| 29 |
|
| 30 |
s.a |
| 31 |
|
| 32 |
t.a |
| 33 |
|
| 34 |
t[1] |
| 35 |
|
| 36 |
u.a |
| 37 |
|
| 38 |
u[1.0] |
| 39 |
|
| 40 |
u[1] |
| 41 |
|
| 42 |
def iterators(ps: Iterable[int], qs: Iterator[str], rs: |
| 43 |
Sequence["ForwardReference"], ts: AbstractSet["float"]): |
| 44 |
for p in ps: |
| 45 |
|
| 46 |
p |
| 47 |
|
| 48 |
next(ps) |
| 49 |
a, b = ps |
| 50 |
|
| 51 |
a |
| 52 |
|
| 53 |
b |
| 54 |
|
| 55 |
for q in qs: |
| 56 |
|
| 57 |
q |
| 58 |
|
| 59 |
next(qs) |
| 60 |
for r in rs: |
| 61 |
|
| 62 |
r |
| 63 |
|
| 64 |
next(rs) |
| 65 |
for t in ts: |
| 66 |
|
| 67 |
t |
| 68 |
|
| 69 |
def sets(p: AbstractSet[int], q: typing.MutableSet[float]): |
| 70 |
|
| 71 |
p.a |
| 72 |
|
| 73 |
q.a |
| 74 |
|
| 75 |
def tupletest(p: Tuple[int], q: Tuple[int, str, float], r: Tuple[B, ...]): |
| 76 |
|
| 77 |
p[0] |
| 78 |
|
| 79 |
p.index |
| 80 |
|
| 81 |
q[0] |
| 82 |
|
| 83 |
q[1] |
| 84 |
|
| 85 |
q[2] |
| 86 |
|
| 87 |
r[0] |
| 88 |
|
| 89 |
r[1] |
| 90 |
|
| 91 |
r[2] |
| 92 |
|
| 93 |
r[10000] |
| 94 |
i, s, f = q |
| 95 |
|
| 96 |
i |
| 97 |
|
| 98 |
s |
| 99 |
|
| 100 |
f |
| 101 |
|
| 102 |
class Key: |
| 103 |
pass |
| 104 |
|
| 105 |
class Value: |
| 106 |
pass |
| 107 |
|
| 108 |
def mapping( |
| 109 |
p: Mapping[Key, Value], |
| 110 |
q: typing.MutableMapping[Key, Value], |
| 111 |
d: Dict[Key, Value], |
| 112 |
dd: typing.DefaultDict[Key, Value], |
| 113 |
r: typing.KeysView[Key], |
| 114 |
s: typing.ValuesView[Value], |
| 115 |
t: typing.ItemsView[Key, Value]): |
| 116 |
|
| 117 |
p.setd |
| 118 |
|
| 119 |
q.setd |
| 120 |
|
| 121 |
d.setd |
| 122 |
|
| 123 |
dd.setd |
| 124 |
|
| 125 |
p[1] |
| 126 |
for key in p: |
| 127 |
|
| 128 |
key |
| 129 |
for key in p.keys(): |
| 130 |
|
| 131 |
key |
| 132 |
for value in p.values(): |
| 133 |
|
| 134 |
value |
| 135 |
for item in p.items(): |
| 136 |
|
| 137 |
item[0] |
| 138 |
|
| 139 |
item[1] |
| 140 |
(key, value) = item |
| 141 |
|
| 142 |
key |
| 143 |
|
| 144 |
value |
| 145 |
for key, value in p.items(): |
| 146 |
|
| 147 |
key |
| 148 |
|
| 149 |
value |
| 150 |
for key, value in q.items(): |
| 151 |
|
| 152 |
key |
| 153 |
|
| 154 |
value |
| 155 |
for key, value in d.items(): |
| 156 |
|
| 157 |
key |
| 158 |
|
| 159 |
value |
| 160 |
for key, value in dd.items(): |
| 161 |
|
| 162 |
key |
| 163 |
|
| 164 |
value |
| 165 |
for key in r: |
| 166 |
|
| 167 |
key |
| 168 |
for value in s: |
| 169 |
|
| 170 |
value |
| 171 |
for key, value in t: |
| 172 |
|
| 173 |
key |
| 174 |
|
| 175 |
value |
| 176 |
|
| 177 |
def union( |
| 178 |
p: Union[int], |
| 179 |
q: Union[int, int], |
| 180 |
r: Union[int, str, "int"], |
| 181 |
s: Union[int, typing.Union[str, "typing.Union['float', 'dict']"]], |
| 182 |
t: Union[int, None]): |
| 183 |
|
| 184 |
p |
| 185 |
|
| 186 |
q |
| 187 |
|
| 188 |
r |
| 189 |
|
| 190 |
s |
| 191 |
|
| 192 |
t |
| 193 |
|
| 194 |
def optional(p: Optional[int]): |
| 195 |
""" |
| 196 |
Optional does not do anything special. However it should be recognised |
| 197 |
as being of that type. Jedi doesn't do anything with the extra into that |
| 198 |
it can be None as well |
| 199 |
""" |
| 200 |
|
| 201 |
p |
| 202 |
|
| 203 |
class ForwardReference: |
| 204 |
pass |
| 205 |
|
| 206 |
class TestDict(typing.Dict[str, int]): |
| 207 |
def setdud(self): |
| 208 |
pass |
| 209 |
|
| 210 |
def testdict(x: TestDict): |
| 211 |
|
| 212 |
x.setd |
| 213 |
for key in x.keys(): |
| 214 |
|
| 215 |
key |
| 216 |
for value in x.values(): |
| 217 |
|
| 218 |
value |
| 219 |
|
| 220 |
x = TestDict() |
| 221 |
|
| 222 |
x.setd |
| 223 |
for key in x.keys(): |
| 224 |
|
| 225 |
key |
| 226 |
for value in x.values(): |
| 227 |
|
| 228 |
value |
| 229 |
|
| 230 |
WrappingType = typing.NewType('WrappingType', str) |
| 231 |
y = WrappingType(0) |
| 232 |
|
| 233 |
y |
| 234 |
|
| 235 |
def testnewtype(y: WrappingType): |
| 236 |
|
| 237 |
y |
| 238 |
|
| 239 |
y.u |
| 240 |
|
| 241 |
WrappingType2 = typing.NewType() |
| 242 |
|
| 243 |
def testnewtype2(y: WrappingType2): |
| 244 |
|
| 245 |
y |
| 246 |
|
| 247 |
y. |
| 248 |
|
| 249 |
|
| 250 |
MyInt = typing.NewType('MyInt', int) |
| 251 |
x = type(MyInt) |
| 252 |
|
| 253 |
x.mro |
| 254 |
|
| 255 |
PlainInt = int |
| 256 |
y = type(PlainInt) |
| 257 |
|
| 258 |
y.mro |
| 259 |
|
| 260 |
class TestDefaultDict(typing.DefaultDict[str, int]): |
| 261 |
def setdud(self): |
| 262 |
pass |
| 263 |
|
| 264 |
def testdict(x: TestDefaultDict): |
| 265 |
|
| 266 |
x.setd |
| 267 |
for key in x.keys(): |
| 268 |
|
| 269 |
key |
| 270 |
for value in x.values(): |
| 271 |
|
| 272 |
value |
| 273 |
|
| 274 |
x = TestDefaultDict() |
| 275 |
|
| 276 |
x.setd |
| 277 |
for key in x.keys(): |
| 278 |
|
| 279 |
key |
| 280 |
for value in x.values(): |
| 281 |
|
| 282 |
value |
| 283 |
|
| 284 |
|
| 285 |
""" |
| 286 |
docstrings have some auto-import, annotations can use all of Python's |
| 287 |
import logic |
| 288 |
""" |
| 289 |
import typing as t |
| 290 |
def union2(x: t.Union[int, str]): |
| 291 |
|
| 292 |
x |
| 293 |
from typing import Union |
| 294 |
def union3(x: Union[int, str]): |
| 295 |
|
| 296 |
x |
| 297 |
|
| 298 |
from typing import Union as U |
| 299 |
def union4(x: U[int, str]): |
| 300 |
|
| 301 |
x |
| 302 |
|
| 303 |
|
| 304 |
typing.Optional[0] |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
TYPE_VARX = typing.TypeVar('TYPE_VARX') |
| 311 |
TYPE_VAR_CONSTRAINTSX = typing.TypeVar('TYPE_VAR_CONSTRAINTSX', str, int) |
| 312 |
|
| 313 |
TYPE_VARX.__clas |
| 314 |
|
| 315 |
TYPE_VARX |
| 316 |
|
| 317 |
|
| 318 |
class WithTypeVar(typing.Generic[TYPE_VARX]): |
| 319 |
def lala(self) -> TYPE_VARX: |
| 320 |
... |
| 321 |
|
| 322 |
|
| 323 |
def maaan(p: WithTypeVar[int]): |
| 324 |
|
| 325 |
p.lala() |
| 326 |
|
| 327 |
def in_out1(x: TYPE_VARX) -> TYPE_VARX: ... |
| 328 |
|
| 329 |
|
| 330 |
in_out1(1) |
| 331 |
|
| 332 |
in_out1("") |
| 333 |
|
| 334 |
in_out1(str()) |
| 335 |
|
| 336 |
in_out1() |
| 337 |
|
| 338 |
def type_in_out1(x: typing.Type[TYPE_VARX]) -> TYPE_VARX: ... |
| 339 |
|
| 340 |
|
| 341 |
type_in_out1(int) |
| 342 |
|
| 343 |
type_in_out1(str) |
| 344 |
|
| 345 |
type_in_out1(float) |
| 346 |
|
| 347 |
type_in_out1() |
| 348 |
|
| 349 |
def in_out2(x: TYPE_VAR_CONSTRAINTSX) -> TYPE_VAR_CONSTRAINTSX: ... |
| 350 |
|
| 351 |
|
| 352 |
in_out2(1) |
| 353 |
|
| 354 |
in_out2("") |
| 355 |
|
| 356 |
in_out2(str()) |
| 357 |
|
| 358 |
in_out2() |
| 359 |
|
| 360 |
|
| 361 |
in_out2(1.0) |
| 362 |
|
| 363 |
def type_in_out2(x: typing.Type[TYPE_VAR_CONSTRAINTSX]) -> TYPE_VAR_CONSTRAINTSX: ... |
| 364 |
|
| 365 |
|
| 366 |
type_in_out2(int) |
| 367 |
|
| 368 |
type_in_out2(str) |
| 369 |
|
| 370 |
type_in_out2() |
| 371 |
|
| 372 |
|
| 373 |
type_in_out2(float) |
| 374 |
|
| 375 |
def ma(a: typing.Callable[[str], TYPE_VARX]) -> typing.Callable[[str], TYPE_VARX]: |
| 376 |
|
| 377 |
return a |
| 378 |
|
| 379 |
def mf(s: str) -> int: |
| 380 |
return int(s) |
| 381 |
|
| 382 |
|
| 383 |
ma(mf)('2') |
| 384 |
|
| 385 |
def xxx(x: typing.Iterable[TYPE_VARX]) -> typing.Tuple[str, TYPE_VARX]: ... |
| 386 |
|
| 387 |
|
| 388 |
xxx([0])[0] |
| 389 |
|
| 390 |
xxx([0])[1] |
| 391 |
|
| 392 |
xxx([0])[2] |
| 393 |
|
| 394 |
def call_pls() -> typing.Callable[[TYPE_VARX], TYPE_VARX]: ... |
| 395 |
|
| 396 |
call_pls()(1) |
| 397 |
|
| 398 |
def call2_pls() -> typing.Callable[[str, typing.Callable[[int], TYPE_VARX]], TYPE_VARX]: ... |
| 399 |
|
| 400 |
call2_pls('')(1, lambda x: 3.0) |
| 401 |
|
| 402 |
def call3_pls() -> typing.Callable[[typing.Callable[[int], TYPE_VARX]], typing.List[TYPE_VARX]]: ... |
| 403 |
def the_callable() -> float: ... |
| 404 |
|
| 405 |
call3_pls()(the_callable)[0] |
| 406 |
|
| 407 |
def call4_pls(fn: typing.Callable[..., TYPE_VARX]) -> typing.Callable[..., TYPE_VARX]: |
| 408 |
return "" |
| 409 |
|
| 410 |
|
| 411 |
call4_pls(lambda x: 1)() |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
if typing.TYPE_CHECKING: |
| 418 |
with_type_checking = 1 |
| 419 |
else: |
| 420 |
without_type_checking = 1.0 |
| 421 |
|
| 422 |
with_type_checking |
| 423 |
|
| 424 |
without_type_checking |
| 425 |
|
| 426 |
def foo(a: typing.List, b: typing.Dict, c: typing.MutableMapping) -> typing.Type[int]: |
| 427 |
|
| 428 |
a.appen |
| 429 |
|
| 430 |
a |
| 431 |
|
| 432 |
a[0] |
| 433 |
|
| 434 |
b.setd |
| 435 |
|
| 436 |
c.setd |
| 437 |
|
| 438 |
c |
| 439 |
|
| 440 |
c['asdf'] |
| 441 |
|
| 442 |
foo() |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
def cast_tests(): |
| 449 |
x = 3.0 |
| 450 |
y = typing.cast(int, x) |
| 451 |
|
| 452 |
y |
| 453 |
return typing.cast(str, x) |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
cast_tests() |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
def dynamic_annotation(x: int): |
| 465 |
|
| 466 |
return x |
| 467 |
|
| 468 |
|
| 469 |
dynamic_annotation('') |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
class Foo(typing.TypedDict): |
| 476 |
foo: str |
| 477 |
bar: typing.List[float] |
| 478 |
an_int: int |
| 479 |
|
| 480 |
foo |
| 481 |
|
| 482 |
foo |
| 483 |
|
| 484 |
an_int |
| 485 |
|
| 486 |
def typed_dict_test_foo(arg: Foo): |
| 487 |
a_string = arg['foo'] |
| 488 |
a_list_of_floats = arg['bar'] |
| 489 |
an_int = arg['an_int'] |
| 490 |
|
| 491 |
|
| 492 |
a_string |
| 493 |
|
| 494 |
a_list_of_floats |
| 495 |
|
| 496 |
a_list_of_floats[0] |
| 497 |
|
| 498 |
an_int |
| 499 |
|
| 500 |
|
| 501 |
a_string.isuppe |
| 502 |
|
| 503 |
a_list_of_floats.po |
| 504 |
|
| 505 |
an_int.as_integer_rati |
| 506 |
|
| 507 |
|
| 508 |
d: Foo |
| 509 |
|
| 510 |
d['foo'] |
| 511 |
|
| 512 |
d['bar'][0] |
| 513 |
|
| 514 |
d['baz'] |
| 515 |
|
| 516 |
|
| 517 |
d.foo |
| 518 |
|
| 519 |
d.bar |
| 520 |
|
| 521 |
d.foo |
| 522 |
|
| 523 |
|
| 524 |
Foo.set |
| 525 |
|
| 526 |
d.setdefaul |
| 527 |
|
| 528 |
Foo.setdefaul |
| 529 |
|
| 530 |
|
| 531 |
d['fo'] |
| 532 |
|
| 533 |
d["bar"] |
| 534 |
|
| 535 |
class Bar(Foo): |
| 536 |
another_variable: int |
| 537 |
|
| 538 |
|
| 539 |
another_variable |
| 540 |
|
| 541 |
an_int |
| 542 |
|
| 543 |
def typed_dict_test_foo(arg: Bar): |
| 544 |
|
| 545 |
arg['foo'] |
| 546 |
|
| 547 |
arg['bar'] |
| 548 |
|
| 549 |
arg['bar'][0] |
| 550 |
|
| 551 |
arg['an_int'] |
| 552 |
|
| 553 |
arg['another_variable'] |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
import typing_extensions |
| 560 |
|
| 561 |
|
| 562 |
class Builder: |
| 563 |
def __init__(self): |
| 564 |
self.x = 0 |
| 565 |
self.y = 0 |
| 566 |
|
| 567 |
def add_x(self: Self, x: int) -> Self: |
| 568 |
self.x = x |
| 569 |
return self |
| 570 |
|
| 571 |
def add_y(self: Self, y: int) -> Self: |
| 572 |
self.y = y |
| 573 |
return self |
| 574 |
|
| 575 |
def add_not_implemented(self: Self, y: int) -> Self: |
| 576 |
raise NotImplementedError |
| 577 |
|
| 578 |
def add_not_implemented_typing_extensions(self: Self, y: int) -> typing_extensions.Self: |
| 579 |
raise NotImplementedError |
| 580 |
|
| 581 |
b = Builder() |
| 582 |
|
| 583 |
b.add_x(2) |
| 584 |
|
| 585 |
b.add_x(2).add_y(5) |
| 586 |
|
| 587 |
|
| 588 |
b.add_x(2).add_not_implemented(5) |
| 589 |
|
| 590 |
b.add_x(2).add_not_implemented_typing_extensions(5) |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
from typing import TypeAlias |
| 597 |
|
| 598 |
IntX: typing.TypeAlias = int |
| 599 |
IntY: TypeAlias = int |
| 600 |
|
| 601 |
|
| 602 |
IntX |
| 603 |
def f(x: IntX, y: IntY): |
| 604 |
|
| 605 |
x |
| 606 |
|
| 607 |
y |