1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
|
# Misc test cases (compile and run)
[case testAsync]
import asyncio
async def h() -> int:
return 1
async def g() -> int:
await asyncio.sleep(0.01)
return await h()
async def f() -> int:
return await g()
loop = asyncio.get_event_loop()
result = loop.run_until_complete(f())
assert result == 1
[typing fixtures/typing-full.pyi]
[file driver.py]
from native import f
import asyncio
loop = asyncio.get_event_loop()
result = loop.run_until_complete(f())
assert result == 1
[case testMaybeUninitVar]
class C:
def __init__(self, x: int) -> None:
self.x = x
def f(b: bool) -> None:
u = C(1)
while b:
v = C(2)
if v is not u:
break
print(v.x)
[file driver.py]
from native import f
f(True)
[out]
2
[case testUninitBoom]
def f(a: bool, b: bool) -> None:
if a:
x = 'lol'
if b:
print(x)
def g() -> None:
try:
[0][1]
y = 1
except Exception:
pass
print(y)
[file driver.py]
from native import f, g
from testutil import assertRaises
f(True, True)
f(False, False)
with assertRaises(NameError):
f(False, True)
with assertRaises(NameError):
g()
[out]
lol
[case testBuiltins]
y = 10
def f(x: int) -> None:
print(5)
d = globals()
assert d['y'] == 10
d['y'] = 20
assert y == 20
[file driver.py]
from native import f
f(5)
[out]
5
[case testOptional]
from typing import Optional
class A: pass
def f(x: Optional[A]) -> Optional[A]:
return x
def g(x: Optional[A]) -> int:
if x is None:
return 1
if x is not None:
return 2
return 3
def h(x: Optional[int], y: Optional[bool]) -> None:
pass
[file driver.py]
from native import f, g, A
a = A()
assert f(None) is None
assert f(a) is a
assert g(None) == 1
assert g(a) == 2
[case testWith]
from typing import Any
class Thing:
def __init__(self, x: str) -> None:
self.x = x
def __enter__(self) -> str:
print('enter!', self.x)
if self.x == 'crash':
raise Exception('ohno')
return self.x
def __exit__(self, x: Any, y: Any, z: Any) -> None:
print('exit!', self.x, y)
def foo(i: int) -> int:
with Thing('a') as x:
print("yooo?", x)
if i == 0:
return 10
elif i == 1:
raise Exception('exception!')
return -1
def bar() -> None:
with Thing('a') as x, Thing('b') as y:
print("yooo?", x, y)
def baz() -> None:
with Thing('a') as x, Thing('crash') as y:
print("yooo?", x, y)
[file driver.py]
from native import foo, bar, baz
assert foo(0) == 10
print('== foo ==')
try:
foo(1)
except Exception:
print('caught')
assert foo(2) == -1
print('== bar ==')
bar()
print('== baz ==')
try:
baz()
except Exception:
print('caught')
[out]
enter! a
yooo? a
exit! a None
== foo ==
enter! a
yooo? a
exit! a exception!
caught
enter! a
yooo? a
exit! a None
== bar ==
enter! a
enter! b
yooo? a b
exit! b None
exit! a None
== baz ==
enter! a
enter! crash
exit! a ohno
caught
[case testDisplays]
from typing import List, Set, Tuple, Sequence, Dict, Any
def listDisplay(x: List[int], y: List[int]) -> List[int]:
return [1, 2, *x, *y, 3]
def setDisplay(x: Set[int], y: Set[int]) -> Set[int]:
return {1, 2, *x, *y, 3}
def tupleDisplay(x: Sequence[str], y: Sequence[str]) -> Tuple[str, ...]:
return ('1', '2', *x, *y, '3')
def dictDisplay(x: str, y1: Dict[str, int], y2: Dict[str, int]) -> Dict[str, int]:
return {x: 2, **y1, 'z': 3, **y2}
[file driver.py]
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay
assert listDisplay([4], [5, 6]) == [1, 2, 4, 5, 6, 3]
assert setDisplay({4}, {5}) == {1, 2, 3, 4, 5}
assert tupleDisplay(['4', '5'], ['6']) == ('1', '2', '4', '5', '6', '3')
assert dictDisplay('x', {'y1': 1}, {'y2': 2, 'z': 5}) == {'x': 2, 'y1': 1, 'y2': 2, 'z': 5}
[case testArbitraryLvalues]
from typing import List, Dict, Any
class O(object):
def __init__(self) -> None:
self.x = 1
def increment_attr(a: Any) -> Any:
a.x += 1
return a
def increment_attr_o(o: O) -> O:
o.x += 1
return o
def increment_all_indices(l: List[int]) -> List[int]:
for i in range(len(l)):
l[i] += 1
return l
def increment_all_keys(d: Dict[str, int]) -> Dict[str, int]:
for k in d:
d[k] += 1
return d
[file driver.py]
from native import O, increment_attr, increment_attr_o, increment_all_indices, increment_all_keys
class P(object):
def __init__(self) -> None:
self.x = 0
assert increment_attr(P()).x == 1
assert increment_attr_o(O()).x == 2
assert increment_all_indices([1, 2, 3]) == [2, 3, 4]
assert increment_all_keys({'a':1, 'b':2, 'c':3}) == {'a':2, 'b':3, 'c':4}
[case testControlFlowExprs]
from typing import Tuple
def foo() -> object:
print('foo')
return 'foo'
def bar() -> object:
print('bar')
return 'bar'
def t(x: int) -> int:
print(x)
return x
def f(b: bool) -> Tuple[object, object, object]:
x = foo() if b else bar()
y = b or foo()
z = b and foo()
return (x, y, z)
def g() -> Tuple[object, object]:
return (foo() or bar(), foo() and bar())
def nand(p: bool, q: bool) -> bool:
if not (p and q):
return True
return False
def chained(x: int, y: int, z: int) -> bool:
return t(x) < t(y) > t(z)
def chained2(x: int, y: int, z: int, w: int) -> bool:
return t(x) < t(y) < t(z) < t(w)
[file driver.py]
from native import f, g, nand, chained, chained2
assert f(True) == ('foo', True, 'foo')
print()
assert f(False) == ('bar', 'foo', False)
print()
assert g() == ('foo', 'bar')
assert nand(True, True) == False
assert nand(True, False) == True
assert nand(False, True) == True
assert nand(False, False) == True
print()
assert chained(10, 20, 15) == True
print()
assert chained(10, 20, 30) == False
print()
assert chained(21, 20, 30) == False
print()
assert chained2(1, 2, 3, 4) == True
print()
assert chained2(1, 0, 3, 4) == False
print()
assert chained2(1, 2, 0, 4) == False
[out]
foo
foo
bar
foo
foo
foo
bar
10
20
15
10
20
30
21
20
1
2
3
4
1
0
1
2
0
[case testMultipleAssignment]
from typing import Tuple, List, Any
def from_tuple(t: Tuple[int, str]) -> List[Any]:
x, y = t
return [y, x]
def from_tuple_sequence(t: Tuple[int, ...]) -> List[int]:
x, y, z = t
return [z, y, x]
def from_list(l: List[int]) -> List[int]:
x, y = l
return [y, x]
def from_list_complex(l: List[int]) -> List[int]:
ll = l[:]
ll[1], ll[0] = l
return ll
def from_any(o: Any) -> List[Any]:
x, y = o
return [y, x]
def multiple_assignments(t: Tuple[int, str]) -> List[Any]:
a, b = c, d = t
e, f = g, h = 1, 2
return [a, b, c, d, e, f, g, h]
[file driver.py]
from native import (
from_tuple, from_tuple_sequence, from_list, from_list_complex, from_any, multiple_assignments
)
assert from_tuple((1, 'x')) == ['x', 1]
assert from_tuple_sequence((1, 5, 4)) == [4, 5, 1]
try:
from_tuple_sequence((1, 5))
except ValueError as e:
assert 'not enough values to unpack (expected 3, got 2)' in str(e)
else:
assert False
assert from_list([3, 4]) == [4, 3]
try:
from_list([5, 4, 3])
except ValueError as e:
assert 'too many values to unpack (expected 2)' in str(e)
else:
assert False
assert from_list_complex([7, 6]) == [6, 7]
try:
from_list_complex([5, 4, 3])
except ValueError as e:
assert 'too many values to unpack (expected 2)' in str(e)
else:
assert False
assert from_any('xy') == ['y', 'x']
assert multiple_assignments((4, 'x')) == [4, 'x', 4, 'x', 1, 2, 1, 2]
[case testUnpack]
from typing import List
a, *b = [1, 2, 3, 4, 5]
*c, d = [1, 2, 3, 4, 5]
e, *f = [1,2]
j, *k, l = [1, 2, 3]
m, *n, o = [1, 2, 3, 4, 5, 6]
p, q, r, *s, t = [1,2,3,4,5,6,7,8,9,10]
tup = (1,2,3)
y, *z = tup
def unpack1(l : List[int]) -> None:
*v1, v2, v3 = l
def unpack2(l : List[int]) -> None:
v1, *v2, v3 = l
def unpack3(l : List[int]) -> None:
v1, v2, *v3 = l
[file driver.py]
from native import a, b, c, d, e, f, j, k, l, m, n, o, p, q, r, s, t, y, z
from native import unpack1, unpack2, unpack3
from testutil import assertRaises
assert a == 1
assert b == [2,3,4,5]
assert c == [1,2,3,4]
assert d == 5
assert e == 1
assert f == [2]
assert j == 1
assert k == [2]
assert l == 3
assert m == 1
assert n == [2,3,4,5]
assert o == 6
assert p == 1
assert q == 2
assert r == 3
assert s == [4,5,6,7,8,9]
assert t == 10
assert y == 1
assert z == [2,3]
with assertRaises(ValueError, "not enough values to unpack"):
unpack1([1])
with assertRaises(ValueError, "not enough values to unpack"):
unpack2([1])
with assertRaises(ValueError, "not enough values to unpack"):
unpack3([1])
[out]
[case testModuleTopLevel]
x = 1
print(x)
def f() -> None:
print(x + 1)
def g() -> None:
global x
x = 77
[file driver.py]
import native
native.f()
native.x = 5
native.f()
native.g()
print(native.x)
[out]
1
2
6
77
[case testComprehensions]
# A list comprehension
l = [str(x) + " " + str(y) + " " + str(x*y) for x in range(10)
if x != 6 if x != 5 for y in range(x) if y*x != 8]
# Test short-circuiting as well
def pred(x: int) -> bool:
if x > 6:
raise Exception()
return x > 3
# If we fail to short-circuit, pred(x) will be called with x=7
# eventually and will raise an exception.
l2 = [x for x in range(10) if x <= 6 if pred(x)]
# A dictionary comprehension
d = {k: k*k for k in range(10) if k != 5 if k != 6}
# A set comprehension
s = {str(x) + " " + str(y) + " " + str(x*y) for x in range(10)
if x != 6 if x != 5 for y in range(x) if y*x != 8}
[file driver.py]
from native import l, l2, d, s
for a in l:
print(a)
print(tuple(l2))
for k in sorted(d):
print(k, d[k])
for a in sorted(s):
print(a)
[out]
1 0 0
2 0 0
2 1 2
3 0 0
3 1 3
3 2 6
4 0 0
4 1 4
4 3 12
7 0 0
7 1 7
7 2 14
7 3 21
7 4 28
7 5 35
7 6 42
8 0 0
8 2 16
8 3 24
8 4 32
8 5 40
8 6 48
8 7 56
9 0 0
9 1 9
9 2 18
9 3 27
9 4 36
9 5 45
9 6 54
9 7 63
9 8 72
(4, 5, 6)
0 0
1 1
2 4
3 9
4 16
7 49
8 64
9 81
1 0 0
2 0 0
2 1 2
3 0 0
3 1 3
3 2 6
4 0 0
4 1 4
4 3 12
7 0 0
7 1 7
7 2 14
7 3 21
7 4 28
7 5 35
7 6 42
8 0 0
8 2 16
8 3 24
8 4 32
8 5 40
8 6 48
8 7 56
9 0 0
9 1 9
9 2 18
9 3 27
9 4 36
9 5 45
9 6 54
9 7 63
9 8 72
[case testDunders]
from typing import Any
class Item:
def __init__(self, value: str) -> None:
self.value = value
def __hash__(self) -> int:
return hash(self.value)
def __eq__(self, rhs: object) -> bool:
return isinstance(rhs, Item) and self.value == rhs.value
def __lt__(self, x: 'Item') -> bool:
return self.value < x.value
class Subclass1(Item):
def __bool__(self) -> bool:
return bool(self.value)
class NonBoxedThing:
def __getitem__(self, index: Item) -> Item:
return Item("2 * " + index.value + " + 1")
class BoxedThing:
def __getitem__(self, index: int) -> int:
return 2 * index + 1
class Subclass2(BoxedThing):
pass
class UsesNotImplemented:
def __eq__(self, b: object) -> bool:
return NotImplemented
def index_into(x : Any, y : Any) -> Any:
return x[y]
def internal_index_into() -> None:
x = BoxedThing()
print (x[3])
y = NonBoxedThing()
z = Item("3")
print(y[z].value)
def is_truthy(x: Item) -> bool:
return True if x else False
[file driver.py]
from native import *
x = BoxedThing()
y = 3
print(x[y], index_into(x, y))
x = Subclass2()
y = 3
print(x[y], index_into(x, y))
z = NonBoxedThing()
w = Item("3")
print(z[w].value, index_into(z, w).value)
i1 = Item('lolol')
i2 = Item('lol' + 'ol')
i3 = Item('xyzzy')
assert hash(i1) == hash(i2)
assert i1 == i2
assert not i1 != i2
assert not i1 == i3
assert i1 != i3
assert i2 < i3
assert not i1 < i2
assert i1 == Subclass1('lolol')
assert is_truthy(Item(''))
assert is_truthy(Item('a'))
assert not is_truthy(Subclass1(''))
assert is_truthy(Subclass1('a'))
assert UsesNotImplemented() != object()
internal_index_into()
[out]
7 7
7 7
2 * 3 + 1 2 * 3 + 1
7
2 * 3 + 1
[case testDummyTypes]
from typing import Tuple, List, Dict, NamedTuple
from typing_extensions import Literal, TypedDict, NewType
class A:
pass
T = List[A]
U = List[Tuple[int, str]]
Z = List[List[int]]
D = Dict[int, List[int]]
N = NewType('N', int)
G = Tuple[int, str]
def foo(x: N) -> int:
return x
foo(N(10))
z = N(10)
Lol = NamedTuple('Lol', (('a', int), ('b', T)))
x = Lol(1, [])
def take_lol(x: Lol) -> int:
return x.a
TD = TypedDict('TD', {'a': int})
def take_typed_dict(x: TD) -> int:
return x['a']
def take_literal(x: Literal[1, 2, 3]) -> None:
print(x)
[file driver.py]
import sys
from native import *
if sys.version_info[:3] > (3, 5, 2):
assert "%s %s %s %s" % (T, U, Z, D) == "typing.List[native.A] typing.List[typing.Tuple[int, str]] typing.List[typing.List[int]] typing.Dict[int, typing.List[int]]"
print(x)
print(z)
print(take_lol(x))
print(take_typed_dict({'a': 20}))
try:
take_typed_dict(None)
except Exception as e:
print(type(e).__name__)
take_literal(1)
# We check that the type is the real underlying type
try:
take_literal(None)
except Exception as e:
print(type(e).__name__)
# ... but not that it is a valid literal value
take_literal(10)
[out]
Lol(a=1, b=[])
10
1
20
TypeError
1
TypeError
10
[case testUnion]
from typing import Union
class A:
def __init__(self, x: int) -> None:
self.x = x
def f(self, y: int) -> int:
return y + self.x
class B:
def __init__(self, x: object) -> None:
self.x = x
def f(self, y: object) -> object:
return y
def f(x: Union[A, str]) -> object:
if isinstance(x, A):
return x.x
else:
return x + 'x'
def g(x: int) -> Union[A, int]:
if x == 0:
return A(1)
else:
return x + 1
def get(x: Union[A, B]) -> object:
return x.x
def call(x: Union[A, B]) -> object:
return x.f(5)
[file driver.py]
from native import A, B, f, g, get, call
assert f('a') == 'ax'
assert f(A(4)) == 4
assert isinstance(g(0), A)
assert g(2) == 3
assert get(A(5)) == 5
assert get(B('x')) == 'x'
assert call(A(4)) == 9
assert call(B('x')) == 5
try:
f(1)
except TypeError:
pass
else:
assert False
[case testAnyAll]
from typing import Iterable
def call_any_nested(l: Iterable[Iterable[int]], val: int = 0) -> int:
res = any(i == val for l2 in l for i in l2)
return 0 if res else 1
def call_any(l: Iterable[int], val: int = 0) -> int:
res = any(i == val for i in l)
return 0 if res else 1
def call_all(l: Iterable[int], val: int = 0) -> int:
res = all(i == val for i in l)
return 0 if res else 1
[file driver.py]
from native import call_any, call_all, call_any_nested
zeros = [0, 0, 0]
ones = [1, 1, 1]
mixed_001 = [0, 0, 1]
mixed_010 = [0, 1, 0]
mixed_100 = [1, 0, 0]
mixed_011 = [0, 1, 1]
mixed_101 = [1, 0, 1]
mixed_110 = [1, 1, 0]
assert call_any([]) == 1
assert call_any(zeros) == 0
assert call_any(ones) == 1
assert call_any(mixed_001) == 0
assert call_any(mixed_010) == 0
assert call_any(mixed_100) == 0
assert call_any(mixed_011) == 0
assert call_any(mixed_101) == 0
assert call_any(mixed_110) == 0
assert call_all([]) == 0
assert call_all(zeros) == 0
assert call_all(ones) == 1
assert call_all(mixed_001) == 1
assert call_all(mixed_010) == 1
assert call_all(mixed_100) == 1
assert call_all(mixed_011) == 1
assert call_all(mixed_101) == 1
assert call_all(mixed_110) == 1
assert call_any_nested([[1, 1, 1], [1, 1], []]) == 1
assert call_any_nested([[1, 1, 1], [0, 1], []]) == 0
[case testNoneStuff]
from typing import Optional
class A:
x: int
def lol(x: A) -> None:
setattr(x, 'x', 5)
def none() -> None:
return
def arg(x: Optional[A]) -> bool:
return x is None
[file driver.py]
import native
native.lol(native.A())
# Catch refcounting failures
for i in range(10000):
native.none()
native.arg(None)
[case testBorrowRefs]
def make_garbage(arg: object) -> None:
b = True
while b:
arg = None
b = False
[file driver.py]
from native import make_garbage
import sys
def test():
x = object()
r0 = sys.getrefcount(x)
make_garbage(x)
r1 = sys.getrefcount(x)
assert r0 == r1
test()
[case testFinalStaticRunFail]
if False:
from typing import Final
if bool():
x: 'Final' = [1]
def f() -> int:
return x[0]
[file driver.py]
from native import f
try:
print(f())
except NameError as e:
print(e.args[0])
[out]
value for final name "x" was not set
[case testFinalStaticRunListTupleInt]
if False:
from typing import Final
x: 'Final' = [1]
y: 'Final' = (1, 2)
z: 'Final' = 1 + 1
def f() -> int:
return x[0]
def g() -> int:
return y[0]
def h() -> int:
return z - 1
[file driver.py]
from native import f, g, h, x, y, z
print(f())
print(x[0])
print(g())
print(y)
print(h())
print(z)
[out]
1
1
1
(1, 2)
1
2
[case testCheckVersion]
import sys
# We lie about the version we are running in tests if it is 3.5, so
# that hits a crash case.
if sys.version_info[:2] == (3, 9):
def version() -> int:
return 9
elif sys.version_info[:2] == (3, 8):
def version() -> int:
return 8
elif sys.version_info[:2] == (3, 7):
def version() -> int:
return 7
elif sys.version_info[:2] == (3, 6):
def version() -> int:
return 6
else:
raise Exception("we don't support this version yet!")
[file driver.py]
import sys
version = sys.version_info[:2]
try:
import native
assert version != (3, 5), "3.5 should fail!"
assert native.version() == sys.version_info[1]
except RuntimeError:
assert version == (3, 5), "only 3.5 should fail!"
[case testTypeErrorMessages]
from typing import Tuple
class A:
pass
class B:
pass
def f(x: B) -> None:
pass
def g(x: Tuple[int, A]) -> None:
pass
[file driver.py]
from testutil import assertRaises
from native import A, f, g
class Busted:
pass
Busted.__module__ = None
with assertRaises(TypeError, "int"):
f(0)
with assertRaises(TypeError, "native.A"):
f(A())
with assertRaises(TypeError, "tuple[None, native.A]"):
f((None, A()))
with assertRaises(TypeError, "tuple[tuple[int, str], native.A]"):
f(((1, "ha"), A()))
with assertRaises(TypeError, "tuple[<50 items>]"):
f(tuple(range(50)))
with assertRaises(TypeError, "errored formatting real type!"):
f(Busted())
with assertRaises(TypeError, "tuple[int, native.A] object expected; got tuple[int, int]"):
g((20, 30))
[case testComprehensionShadowBinder]
def foo(x: object) -> object:
if isinstance(x, list):
return tuple(x for x in x), x
return None
[file driver.py]
from native import foo
assert foo(None) == None
assert foo([1, 2, 3]) == ((1, 2, 3), [1, 2, 3])
|