| 1 |
[case testMaybeUninitVar] |
| 2 |
class C: |
| 3 |
def __init__(self, x: int) -> None: |
| 4 |
self.x = x |
| 5 |
|
| 6 |
def f(b: bool) -> None: |
| 7 |
u = C(1) |
| 8 |
while b: |
| 9 |
v = C(2) |
| 10 |
if v is not u: |
| 11 |
break |
| 12 |
print(v.x) |
| 13 |
[file driver.py] |
| 14 |
from native import f |
| 15 |
f(True) |
| 16 |
[out] |
| 17 |
2 |
| 18 |
|
| 19 |
[case testUninitBoom] |
| 20 |
def f(a: bool, b: bool) -> None: |
| 21 |
if a: |
| 22 |
x = 'lol' |
| 23 |
if b: |
| 24 |
print(x) |
| 25 |
|
| 26 |
def g() -> None: |
| 27 |
try: |
| 28 |
[0][1] |
| 29 |
y = 1 |
| 30 |
except Exception: |
| 31 |
pass |
| 32 |
print(y) |
| 33 |
|
| 34 |
[file driver.py] |
| 35 |
from native import f, g |
| 36 |
from testutil import assertRaises |
| 37 |
|
| 38 |
f(True, True) |
| 39 |
f(False, False) |
| 40 |
with assertRaises(UnboundLocalError): |
| 41 |
f(False, True) |
| 42 |
with assertRaises(UnboundLocalError): |
| 43 |
g() |
| 44 |
[out] |
| 45 |
lol |
| 46 |
|
| 47 |
[case testBuiltins] |
| 48 |
y = 10 |
| 49 |
def f(x: int) -> None: |
| 50 |
print(5) |
| 51 |
d = globals() |
| 52 |
assert d['y'] == 10 |
| 53 |
d['y'] = 20 |
| 54 |
assert y == 20 |
| 55 |
[file driver.py] |
| 56 |
from native import f |
| 57 |
f(5) |
| 58 |
[out] |
| 59 |
5 |
| 60 |
|
| 61 |
[case testOptional] |
| 62 |
from typing import Optional |
| 63 |
|
| 64 |
class A: pass |
| 65 |
|
| 66 |
def f(x: Optional[A]) -> Optional[A]: |
| 67 |
return x |
| 68 |
|
| 69 |
def g(x: Optional[A]) -> int: |
| 70 |
if x is None: |
| 71 |
return 1 |
| 72 |
if x is not None: |
| 73 |
return 2 |
| 74 |
return 3 |
| 75 |
|
| 76 |
def h(x: Optional[int], y: Optional[bool]) -> None: |
| 77 |
pass |
| 78 |
|
| 79 |
[file driver.py] |
| 80 |
from native import f, g, A |
| 81 |
a = A() |
| 82 |
assert f(None) is None |
| 83 |
assert f(a) is a |
| 84 |
assert g(None) == 1 |
| 85 |
assert g(a) == 2 |
| 86 |
|
| 87 |
[case testInferredOptionalAssignment] |
| 88 |
from typing import Any, Generator |
| 89 |
|
| 90 |
def f(b: bool) -> Any: |
| 91 |
if b: |
| 92 |
x = None |
| 93 |
else: |
| 94 |
x = 1 |
| 95 |
|
| 96 |
if b: |
| 97 |
y = 1 |
| 98 |
else: |
| 99 |
y = None |
| 100 |
|
| 101 |
m = 1 if b else None |
| 102 |
n = None if b else 1 |
| 103 |
return ((x, y), (m, n)) |
| 104 |
|
| 105 |
def gen(b: bool) -> Generator[Any, None, None]: |
| 106 |
if b: |
| 107 |
y = 1 |
| 108 |
else: |
| 109 |
y = None |
| 110 |
yield y |
| 111 |
|
| 112 |
def test_inferred() -> None: |
| 113 |
assert f(False) == ((1, None), (None, 1)) |
| 114 |
assert f(True) == ((None, 1), (1, None)) |
| 115 |
assert next(gen(False)) is None |
| 116 |
assert next(gen(True)) == 1 |
| 117 |
|
| 118 |
[case testWith] |
| 119 |
from typing import Any |
| 120 |
class Thing: |
| 121 |
def __init__(self, x: str) -> None: |
| 122 |
self.x = x |
| 123 |
def __enter__(self) -> str: |
| 124 |
print('enter!', self.x) |
| 125 |
if self.x == 'crash': |
| 126 |
raise Exception('ohno') |
| 127 |
return self.x |
| 128 |
def __exit__(self, x: Any, y: Any, z: Any) -> None: |
| 129 |
print('exit!', self.x, y) |
| 130 |
|
| 131 |
def foo(i: int) -> int: |
| 132 |
with Thing('a') as x: |
| 133 |
print("yooo?", x) |
| 134 |
if i == 0: |
| 135 |
return 10 |
| 136 |
elif i == 1: |
| 137 |
raise Exception('exception!') |
| 138 |
return -1 |
| 139 |
|
| 140 |
def bar() -> None: |
| 141 |
with Thing('a') as x, Thing('b') as y: |
| 142 |
print("yooo?", x, y) |
| 143 |
|
| 144 |
def baz() -> None: |
| 145 |
with Thing('a') as x, Thing('crash') as y: |
| 146 |
print("yooo?", x, y) |
| 147 |
|
| 148 |
[file driver.py] |
| 149 |
from native import foo, bar, baz |
| 150 |
assert foo(0) == 10 |
| 151 |
print('== foo ==') |
| 152 |
try: |
| 153 |
foo(1) |
| 154 |
except Exception: |
| 155 |
print('caught') |
| 156 |
assert foo(2) == -1 |
| 157 |
|
| 158 |
print('== bar ==') |
| 159 |
bar() |
| 160 |
|
| 161 |
print('== baz ==') |
| 162 |
try: |
| 163 |
baz() |
| 164 |
except Exception: |
| 165 |
print('caught') |
| 166 |
|
| 167 |
[out] |
| 168 |
enter! a |
| 169 |
yooo? a |
| 170 |
exit! a None |
| 171 |
== foo == |
| 172 |
enter! a |
| 173 |
yooo? a |
| 174 |
exit! a exception! |
| 175 |
caught |
| 176 |
enter! a |
| 177 |
yooo? a |
| 178 |
exit! a None |
| 179 |
== bar == |
| 180 |
enter! a |
| 181 |
enter! b |
| 182 |
yooo? a b |
| 183 |
exit! b None |
| 184 |
exit! a None |
| 185 |
== baz == |
| 186 |
enter! a |
| 187 |
enter! crash |
| 188 |
exit! a ohno |
| 189 |
caught |
| 190 |
|
| 191 |
[case testDisplays] |
| 192 |
from typing import List, Set, Tuple, Sequence, Dict, Any, Mapping |
| 193 |
|
| 194 |
def listDisplay(x: List[int], y: List[int]) -> List[int]: |
| 195 |
return [1, 2, *x, *y, 3] |
| 196 |
|
| 197 |
def setDisplay(x: Set[int], y: Set[int]) -> Set[int]: |
| 198 |
return {1, 2, *x, *y, 3} |
| 199 |
|
| 200 |
def tupleDisplay(x: Sequence[str], y: Sequence[str]) -> Tuple[str, ...]: |
| 201 |
return ('1', '2', *x, *y, '3') |
| 202 |
|
| 203 |
def dictDisplay(x: str, y1: Dict[str, int], y2: Dict[str, int]) -> Dict[str, int]: |
| 204 |
return {x: 2, **y1, 'z': 3, **y2} |
| 205 |
|
| 206 |
def dictDisplayUnpackMapping(obj: Mapping[str, str]) -> Dict[str, str]: |
| 207 |
return {**obj, "env": "value"} |
| 208 |
|
| 209 |
[file driver.py] |
| 210 |
import os |
| 211 |
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay, dictDisplayUnpackMapping |
| 212 |
assert listDisplay([4], [5, 6]) == [1, 2, 4, 5, 6, 3] |
| 213 |
assert setDisplay({4}, {5}) == {1, 2, 3, 4, 5} |
| 214 |
assert tupleDisplay(['4', '5'], ['6']) == ('1', '2', '4', '5', '6', '3') |
| 215 |
assert dictDisplay('x', {'y1': 1}, {'y2': 2, 'z': 5}) == {'x': 2, 'y1': 1, 'y2': 2, 'z': 5} |
| 216 |
assert dictDisplayUnpackMapping(os.environ) == {**os.environ, "env": "value"} |
| 217 |
|
| 218 |
[case testArbitraryLvalues] |
| 219 |
from typing import List, Dict, Any |
| 220 |
|
| 221 |
class O(object): |
| 222 |
def __init__(self) -> None: |
| 223 |
self.x = 1 |
| 224 |
|
| 225 |
def increment_attr(a: Any) -> Any: |
| 226 |
a.x += 1 |
| 227 |
return a |
| 228 |
|
| 229 |
def increment_attr_o(o: O) -> O: |
| 230 |
o.x += 1 |
| 231 |
return o |
| 232 |
|
| 233 |
def increment_all_indices(l: List[int]) -> List[int]: |
| 234 |
for i in range(len(l)): |
| 235 |
l[i] += 1 |
| 236 |
return l |
| 237 |
|
| 238 |
def increment_all_keys(d: Dict[str, int]) -> Dict[str, int]: |
| 239 |
for k in d: |
| 240 |
d[k] += 1 |
| 241 |
return d |
| 242 |
|
| 243 |
[file driver.py] |
| 244 |
from native import O, increment_attr, increment_attr_o, increment_all_indices, increment_all_keys |
| 245 |
|
| 246 |
class P(object): |
| 247 |
def __init__(self) -> None: |
| 248 |
self.x = 0 |
| 249 |
|
| 250 |
assert increment_attr(P()).x == 1 |
| 251 |
assert increment_attr_o(O()).x == 2 |
| 252 |
assert increment_all_indices([1, 2, 3]) == [2, 3, 4] |
| 253 |
assert increment_all_keys({'a':1, 'b':2, 'c':3}) == {'a':2, 'b':3, 'c':4} |
| 254 |
|
| 255 |
[case testControlFlowExprs] |
| 256 |
from typing import Tuple |
| 257 |
def foo() -> object: |
| 258 |
print('foo') |
| 259 |
return 'foo' |
| 260 |
def bar() -> object: |
| 261 |
print('bar') |
| 262 |
return 'bar' |
| 263 |
def t(x: int) -> int: |
| 264 |
print(x) |
| 265 |
return x |
| 266 |
|
| 267 |
def f(b: bool) -> Tuple[object, object, object]: |
| 268 |
x = foo() if b else bar() |
| 269 |
y = b or foo() |
| 270 |
z = b and foo() |
| 271 |
return (x, y, z) |
| 272 |
def g() -> Tuple[object, object]: |
| 273 |
return (foo() or bar(), foo() and bar()) |
| 274 |
|
| 275 |
def nand(p: bool, q: bool) -> bool: |
| 276 |
if not (p and q): |
| 277 |
return True |
| 278 |
return False |
| 279 |
|
| 280 |
def chained(x: int, y: int, z: int) -> bool: |
| 281 |
return t(x) < t(y) > t(z) |
| 282 |
|
| 283 |
def chained2(x: int, y: int, z: int, w: int) -> bool: |
| 284 |
return t(x) < t(y) < t(z) < t(w) |
| 285 |
[file driver.py] |
| 286 |
from native import f, g, nand, chained, chained2 |
| 287 |
assert f(True) == ('foo', True, 'foo') |
| 288 |
print() |
| 289 |
assert f(False) == ('bar', 'foo', False) |
| 290 |
print() |
| 291 |
assert g() == ('foo', 'bar') |
| 292 |
|
| 293 |
assert nand(True, True) == False |
| 294 |
assert nand(True, False) == True |
| 295 |
assert nand(False, True) == True |
| 296 |
assert nand(False, False) == True |
| 297 |
|
| 298 |
print() |
| 299 |
assert chained(10, 20, 15) == True |
| 300 |
print() |
| 301 |
assert chained(10, 20, 30) == False |
| 302 |
print() |
| 303 |
assert chained(21, 20, 30) == False |
| 304 |
print() |
| 305 |
assert chained2(1, 2, 3, 4) == True |
| 306 |
print() |
| 307 |
assert chained2(1, 0, 3, 4) == False |
| 308 |
print() |
| 309 |
assert chained2(1, 2, 0, 4) == False |
| 310 |
[out] |
| 311 |
foo |
| 312 |
foo |
| 313 |
|
| 314 |
bar |
| 315 |
foo |
| 316 |
|
| 317 |
foo |
| 318 |
foo |
| 319 |
bar |
| 320 |
|
| 321 |
10 |
| 322 |
20 |
| 323 |
15 |
| 324 |
|
| 325 |
10 |
| 326 |
20 |
| 327 |
30 |
| 328 |
|
| 329 |
21 |
| 330 |
20 |
| 331 |
|
| 332 |
1 |
| 333 |
2 |
| 334 |
3 |
| 335 |
4 |
| 336 |
|
| 337 |
1 |
| 338 |
0 |
| 339 |
|
| 340 |
1 |
| 341 |
2 |
| 342 |
0 |
| 343 |
|
| 344 |
[case testMultipleAssignment] |
| 345 |
from typing import Tuple, List, Any |
| 346 |
|
| 347 |
def from_tuple(t: Tuple[int, str]) -> List[Any]: |
| 348 |
x, y = t |
| 349 |
return [y, x] |
| 350 |
|
| 351 |
def from_tuple_sequence(t: Tuple[int, ...]) -> List[int]: |
| 352 |
x, y, z = t |
| 353 |
return [z, y, x] |
| 354 |
|
| 355 |
def from_list(l: List[int]) -> List[int]: |
| 356 |
x, y = l |
| 357 |
return [y, x] |
| 358 |
|
| 359 |
def from_list_complex(l: List[int]) -> List[int]: |
| 360 |
ll = l[:] |
| 361 |
ll[1], ll[0] = l |
| 362 |
return ll |
| 363 |
|
| 364 |
def from_any(o: Any) -> List[Any]: |
| 365 |
x, y = o |
| 366 |
return [y, x] |
| 367 |
|
| 368 |
def multiple_assignments(t: Tuple[int, str]) -> List[Any]: |
| 369 |
a, b = c, d = t |
| 370 |
e, f = g, h = 1, 2 |
| 371 |
return [a, b, c, d, e, f, g, h] |
| 372 |
[file driver.py] |
| 373 |
from native import ( |
| 374 |
from_tuple, from_tuple_sequence, from_list, from_list_complex, from_any, multiple_assignments |
| 375 |
) |
| 376 |
|
| 377 |
assert from_tuple((1, 'x')) == ['x', 1] |
| 378 |
|
| 379 |
assert from_tuple_sequence((1, 5, 4)) == [4, 5, 1] |
| 380 |
try: |
| 381 |
from_tuple_sequence((1, 5)) |
| 382 |
except ValueError as e: |
| 383 |
assert 'not enough values to unpack (expected 3, got 2)' in str(e) |
| 384 |
else: |
| 385 |
assert False |
| 386 |
|
| 387 |
assert from_list([3, 4]) == [4, 3] |
| 388 |
try: |
| 389 |
from_list([5, 4, 3]) |
| 390 |
except ValueError as e: |
| 391 |
assert 'too many values to unpack (expected 2)' in str(e) |
| 392 |
else: |
| 393 |
assert False |
| 394 |
|
| 395 |
assert from_list_complex([7, 6]) == [6, 7] |
| 396 |
try: |
| 397 |
from_list_complex([5, 4, 3]) |
| 398 |
except ValueError as e: |
| 399 |
assert 'too many values to unpack (expected 2)' in str(e) |
| 400 |
else: |
| 401 |
assert False |
| 402 |
|
| 403 |
assert from_any('xy') == ['y', 'x'] |
| 404 |
|
| 405 |
assert multiple_assignments((4, 'x')) == [4, 'x', 4, 'x', 1, 2, 1, 2] |
| 406 |
|
| 407 |
[case testUnpack] |
| 408 |
from typing import List |
| 409 |
|
| 410 |
a, *b = [1, 2, 3, 4, 5] |
| 411 |
|
| 412 |
*c, d = [1, 2, 3, 4, 5] |
| 413 |
|
| 414 |
e, *f = [1,2] |
| 415 |
|
| 416 |
j, *k, l = [1, 2, 3] |
| 417 |
|
| 418 |
m, *n, o = [1, 2, 3, 4, 5, 6] |
| 419 |
|
| 420 |
p, q, r, *s, t = [1,2,3,4,5,6,7,8,9,10] |
| 421 |
|
| 422 |
tup = (1,2,3) |
| 423 |
y, *z = tup |
| 424 |
|
| 425 |
def unpack1(l : List[int]) -> None: |
| 426 |
*v1, v2, v3 = l |
| 427 |
|
| 428 |
def unpack2(l : List[int]) -> None: |
| 429 |
v1, *v2, v3 = l |
| 430 |
|
| 431 |
def unpack3(l : List[int]) -> None: |
| 432 |
v1, v2, *v3 = l |
| 433 |
|
| 434 |
[file driver.py] |
| 435 |
from native import a, b, c, d, e, f, j, k, l, m, n, o, p, q, r, s, t, y, z |
| 436 |
from native import unpack1, unpack2, unpack3 |
| 437 |
from testutil import assertRaises |
| 438 |
|
| 439 |
assert a == 1 |
| 440 |
assert b == [2,3,4,5] |
| 441 |
assert c == [1,2,3,4] |
| 442 |
assert d == 5 |
| 443 |
assert e == 1 |
| 444 |
assert f == [2] |
| 445 |
assert j == 1 |
| 446 |
assert k == [2] |
| 447 |
assert l == 3 |
| 448 |
assert m == 1 |
| 449 |
assert n == [2,3,4,5] |
| 450 |
assert o == 6 |
| 451 |
assert p == 1 |
| 452 |
assert q == 2 |
| 453 |
assert r == 3 |
| 454 |
assert s == [4,5,6,7,8,9] |
| 455 |
assert t == 10 |
| 456 |
assert y == 1 |
| 457 |
assert z == [2,3] |
| 458 |
|
| 459 |
with assertRaises(ValueError, "not enough values to unpack"): |
| 460 |
unpack1([1]) |
| 461 |
|
| 462 |
with assertRaises(ValueError, "not enough values to unpack"): |
| 463 |
unpack2([1]) |
| 464 |
|
| 465 |
with assertRaises(ValueError, "not enough values to unpack"): |
| 466 |
unpack3([1]) |
| 467 |
|
| 468 |
[out] |
| 469 |
|
| 470 |
[case testModuleTopLevel] |
| 471 |
x = 1 |
| 472 |
print(x) |
| 473 |
|
| 474 |
def f() -> None: |
| 475 |
print(x + 1) |
| 476 |
|
| 477 |
def g() -> None: |
| 478 |
global x |
| 479 |
x = 77 |
| 480 |
|
| 481 |
[file driver.py] |
| 482 |
import native |
| 483 |
native.f() |
| 484 |
native.x = 5 |
| 485 |
native.f() |
| 486 |
native.g() |
| 487 |
print(native.x) |
| 488 |
|
| 489 |
[out] |
| 490 |
1 |
| 491 |
2 |
| 492 |
6 |
| 493 |
77 |
| 494 |
|
| 495 |
[case testComprehensions] |
| 496 |
from typing import List |
| 497 |
|
| 498 |
# A list comprehension |
| 499 |
l = [str(x) + " " + str(y) + " " + str(x*y) for x in range(10) |
| 500 |
if x != 6 if x != 5 for y in range(x) if y*x != 8] |
| 501 |
|
| 502 |
# Test short-circuiting as well |
| 503 |
def pred(x: int) -> bool: |
| 504 |
if x > 6: |
| 505 |
raise Exception() |
| 506 |
return x > 3 |
| 507 |
# If we fail to short-circuit, pred(x) will be called with x=7 |
| 508 |
# eventually and will raise an exception. |
| 509 |
l2 = [x for x in range(10) if x <= 6 if pred(x)] |
| 510 |
|
| 511 |
src = ['x'] |
| 512 |
|
| 513 |
def f() -> List[str]: |
| 514 |
global src |
| 515 |
res = src |
| 516 |
src = [] |
| 517 |
return res |
| 518 |
|
| 519 |
l3 = [s for s in f()] |
| 520 |
l4 = [s for s in f()] |
| 521 |
|
| 522 |
# A dictionary comprehension |
| 523 |
d = {k: k*k for k in range(10) if k != 5 if k != 6} |
| 524 |
|
| 525 |
# A set comprehension |
| 526 |
s = {str(x) + " " + str(y) + " " + str(x*y) for x in range(10) |
| 527 |
if x != 6 if x != 5 for y in range(x) if y*x != 8} |
| 528 |
|
| 529 |
[file driver.py] |
| 530 |
from native import l, l2, l3, l4, d, s |
| 531 |
for a in l: |
| 532 |
print(a) |
| 533 |
print(tuple(l2)) |
| 534 |
assert l3 == ['x'] |
| 535 |
assert l4 == [] |
| 536 |
for k in sorted(d): |
| 537 |
print(k, d[k]) |
| 538 |
for a in sorted(s): |
| 539 |
print(a) |
| 540 |
[out] |
| 541 |
1 0 0 |
| 542 |
2 0 0 |
| 543 |
2 1 2 |
| 544 |
3 0 0 |
| 545 |
3 1 3 |
| 546 |
3 2 6 |
| 547 |
4 0 0 |
| 548 |
4 1 4 |
| 549 |
4 3 12 |
| 550 |
7 0 0 |
| 551 |
7 1 7 |
| 552 |
7 2 14 |
| 553 |
7 3 21 |
| 554 |
7 4 28 |
| 555 |
7 5 35 |
| 556 |
7 6 42 |
| 557 |
8 0 0 |
| 558 |
8 2 16 |
| 559 |
8 3 24 |
| 560 |
8 4 32 |
| 561 |
8 5 40 |
| 562 |
8 6 48 |
| 563 |
8 7 56 |
| 564 |
9 0 0 |
| 565 |
9 1 9 |
| 566 |
9 2 18 |
| 567 |
9 3 27 |
| 568 |
9 4 36 |
| 569 |
9 5 45 |
| 570 |
9 6 54 |
| 571 |
9 7 63 |
| 572 |
9 8 72 |
| 573 |
(4, 5, 6) |
| 574 |
0 0 |
| 575 |
1 1 |
| 576 |
2 4 |
| 577 |
3 9 |
| 578 |
4 16 |
| 579 |
7 49 |
| 580 |
8 64 |
| 581 |
9 81 |
| 582 |
1 0 0 |
| 583 |
2 0 0 |
| 584 |
2 1 2 |
| 585 |
3 0 0 |
| 586 |
3 1 3 |
| 587 |
3 2 6 |
| 588 |
4 0 0 |
| 589 |
4 1 4 |
| 590 |
4 3 12 |
| 591 |
7 0 0 |
| 592 |
7 1 7 |
| 593 |
7 2 14 |
| 594 |
7 3 21 |
| 595 |
7 4 28 |
| 596 |
7 5 35 |
| 597 |
7 6 42 |
| 598 |
8 0 0 |
| 599 |
8 2 16 |
| 600 |
8 3 24 |
| 601 |
8 4 32 |
| 602 |
8 5 40 |
| 603 |
8 6 48 |
| 604 |
8 7 56 |
| 605 |
9 0 0 |
| 606 |
9 1 9 |
| 607 |
9 2 18 |
| 608 |
9 3 27 |
| 609 |
9 4 36 |
| 610 |
9 5 45 |
| 611 |
9 6 54 |
| 612 |
9 7 63 |
| 613 |
9 8 72 |
| 614 |
|
| 615 |
[case testDummyTypes] |
| 616 |
from typing import Tuple, List, Dict, Literal, NamedTuple, NewType, TypedDict |
| 617 |
|
| 618 |
class A: |
| 619 |
pass |
| 620 |
|
| 621 |
T = List[A] |
| 622 |
U = List[Tuple[int, str]] |
| 623 |
Z = List[List[int]] |
| 624 |
D = Dict[int, List[int]] |
| 625 |
N = NewType('N', int) |
| 626 |
G = Tuple[int, str] |
| 627 |
def foo(x: N) -> int: |
| 628 |
return x |
| 629 |
foo(N(10)) |
| 630 |
z = N(10) |
| 631 |
Lol = NamedTuple('Lol', (('a', int), ('b', T))) |
| 632 |
x = Lol(1, []) |
| 633 |
def take_lol(x: Lol) -> int: |
| 634 |
return x.a |
| 635 |
|
| 636 |
TD = TypedDict('TD', {'a': int}) |
| 637 |
def take_typed_dict(x: TD) -> int: |
| 638 |
return x['a'] |
| 639 |
|
| 640 |
def take_literal(x: Literal[1, 2, 3]) -> None: |
| 641 |
print(x) |
| 642 |
|
| 643 |
[file driver.py] |
| 644 |
import sys |
| 645 |
from native import * |
| 646 |
|
| 647 |
if sys.version_info[:3] > (3, 5, 2): |
| 648 |
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]]" |
| 649 |
print(x) |
| 650 |
print(z) |
| 651 |
print(take_lol(x)) |
| 652 |
print(take_typed_dict({'a': 20})) |
| 653 |
try: |
| 654 |
take_typed_dict(None) |
| 655 |
except Exception as e: |
| 656 |
print(type(e).__name__) |
| 657 |
|
| 658 |
|
| 659 |
take_literal(1) |
| 660 |
# We check that the type is the real underlying type |
| 661 |
try: |
| 662 |
take_literal(None) |
| 663 |
except Exception as e: |
| 664 |
print(type(e).__name__) |
| 665 |
# ... but not that it is a valid literal value |
| 666 |
take_literal(10) |
| 667 |
[typing fixtures/typing-full.pyi] |
| 668 |
[out] |
| 669 |
Lol(a=1, b=[]) |
| 670 |
10 |
| 671 |
1 |
| 672 |
20 |
| 673 |
TypeError |
| 674 |
1 |
| 675 |
TypeError |
| 676 |
10 |
| 677 |
|
| 678 |
[case testClassBasedTypedDict] |
| 679 |
from typing import TypedDict |
| 680 |
|
| 681 |
class TD(TypedDict): |
| 682 |
a: int |
| 683 |
|
| 684 |
class TD2(TD): |
| 685 |
b: int |
| 686 |
|
| 687 |
class TD3(TypedDict, total=False): |
| 688 |
c: int |
| 689 |
|
| 690 |
class TD4(TD3, TD2, total=False): |
| 691 |
d: int |
| 692 |
|
| 693 |
def test_typed_dict() -> None: |
| 694 |
d = TD(a=5) |
| 695 |
assert d['a'] == 5 |
| 696 |
assert type(d) == dict |
| 697 |
# TODO: This doesn't work yet |
| 698 |
# assert TD.__annotations__ == {'a': int} |
| 699 |
|
| 700 |
def test_inherited_typed_dict() -> None: |
| 701 |
d = TD2(a=5, b=3) |
| 702 |
assert d['a'] == 5 |
| 703 |
assert d['b'] == 3 |
| 704 |
assert type(d) == dict |
| 705 |
|
| 706 |
def test_non_total_typed_dict() -> None: |
| 707 |
d3 = TD3(c=3) |
| 708 |
d4 = TD4(a=1, b=2, c=3, d=4) |
| 709 |
assert d3['c'] == 3 |
| 710 |
assert d4['d'] == 4 |
| 711 |
[typing fixtures/typing-full.pyi] |
| 712 |
|
| 713 |
[case testClassBasedNamedTuple] |
| 714 |
from typing import NamedTuple |
| 715 |
import sys |
| 716 |
|
| 717 |
# Class-based NamedTuple requires Python 3.6+ |
| 718 |
version = sys.version_info[:2] |
| 719 |
if version[0] == 3 and version[1] < 6: |
| 720 |
exit() |
| 721 |
|
| 722 |
class NT(NamedTuple): |
| 723 |
a: int |
| 724 |
|
| 725 |
def test_named_tuple() -> None: |
| 726 |
t = NT(a=1) |
| 727 |
assert t.a == 1 |
| 728 |
assert type(t) is NT |
| 729 |
assert isinstance(t, tuple) |
| 730 |
assert not isinstance(tuple([1]), NT) |
| 731 |
|
| 732 |
[case testUnion] |
| 733 |
from typing import Union |
| 734 |
|
| 735 |
class A: |
| 736 |
def __init__(self, x: int) -> None: |
| 737 |
self.x = x |
| 738 |
def f(self, y: int) -> int: |
| 739 |
return y + self.x |
| 740 |
|
| 741 |
class B: |
| 742 |
def __init__(self, x: object) -> None: |
| 743 |
self.x = x |
| 744 |
def f(self, y: object) -> object: |
| 745 |
return y |
| 746 |
|
| 747 |
def f(x: Union[A, str]) -> object: |
| 748 |
if isinstance(x, A): |
| 749 |
return x.x |
| 750 |
else: |
| 751 |
return x + 'x' |
| 752 |
|
| 753 |
def g(x: int) -> Union[A, int]: |
| 754 |
if x == 0: |
| 755 |
return A(1) |
| 756 |
else: |
| 757 |
return x + 1 |
| 758 |
|
| 759 |
def get(x: Union[A, B]) -> object: |
| 760 |
return x.x |
| 761 |
|
| 762 |
def call(x: Union[A, B]) -> object: |
| 763 |
return x.f(5) |
| 764 |
|
| 765 |
[file driver.py] |
| 766 |
from native import A, B, f, g, get, call |
| 767 |
assert f('a') == 'ax' |
| 768 |
assert f(A(4)) == 4 |
| 769 |
assert isinstance(g(0), A) |
| 770 |
assert g(2) == 3 |
| 771 |
assert get(A(5)) == 5 |
| 772 |
assert get(B('x')) == 'x' |
| 773 |
assert call(A(4)) == 9 |
| 774 |
assert call(B('x')) == 5 |
| 775 |
try: |
| 776 |
f(1) |
| 777 |
except TypeError: |
| 778 |
pass |
| 779 |
else: |
| 780 |
assert False |
| 781 |
|
| 782 |
[case testAnyAll] |
| 783 |
from typing import Iterable |
| 784 |
|
| 785 |
def call_any_nested(l: Iterable[Iterable[int]], val: int = 0) -> int: |
| 786 |
res = any(i == val for l2 in l for i in l2) |
| 787 |
return 0 if res else 1 |
| 788 |
|
| 789 |
def call_any(l: Iterable[int], val: int = 0) -> int: |
| 790 |
res = any(i == val for i in l) |
| 791 |
return 0 if res else 1 |
| 792 |
|
| 793 |
def call_all(l: Iterable[int], val: int = 0) -> int: |
| 794 |
res = all(i == val for i in l) |
| 795 |
return 0 if res else 1 |
| 796 |
|
| 797 |
[file driver.py] |
| 798 |
from native import call_any, call_all, call_any_nested |
| 799 |
|
| 800 |
zeros = [0, 0, 0] |
| 801 |
ones = [1, 1, 1] |
| 802 |
mixed_001 = [0, 0, 1] |
| 803 |
mixed_010 = [0, 1, 0] |
| 804 |
mixed_100 = [1, 0, 0] |
| 805 |
mixed_011 = [0, 1, 1] |
| 806 |
mixed_101 = [1, 0, 1] |
| 807 |
mixed_110 = [1, 1, 0] |
| 808 |
|
| 809 |
assert call_any([]) == 1 |
| 810 |
assert call_any(zeros) == 0 |
| 811 |
assert call_any(ones) == 1 |
| 812 |
assert call_any(mixed_001) == 0 |
| 813 |
assert call_any(mixed_010) == 0 |
| 814 |
assert call_any(mixed_100) == 0 |
| 815 |
assert call_any(mixed_011) == 0 |
| 816 |
assert call_any(mixed_101) == 0 |
| 817 |
assert call_any(mixed_110) == 0 |
| 818 |
|
| 819 |
assert call_all([]) == 0 |
| 820 |
assert call_all(zeros) == 0 |
| 821 |
assert call_all(ones) == 1 |
| 822 |
assert call_all(mixed_001) == 1 |
| 823 |
assert call_all(mixed_010) == 1 |
| 824 |
assert call_all(mixed_100) == 1 |
| 825 |
assert call_all(mixed_011) == 1 |
| 826 |
assert call_all(mixed_101) == 1 |
| 827 |
assert call_all(mixed_110) == 1 |
| 828 |
|
| 829 |
assert call_any_nested([[1, 1, 1], [1, 1], []]) == 1 |
| 830 |
assert call_any_nested([[1, 1, 1], [0, 1], []]) == 0 |
| 831 |
|
| 832 |
[case testSum] |
| 833 |
from typing import List |
| 834 |
|
| 835 |
empty: List[int] = [] |
| 836 |
def test_sum_of_numbers() -> None: |
| 837 |
assert sum(x for x in [1, 2, 3]) == 6 |
| 838 |
assert sum(x for x in [0.0, 1.2, 2]) == 3.2 |
| 839 |
assert sum(x for x in [1, 1j]) == 1 + 1j |
| 840 |
|
| 841 |
def test_sum_callables() -> None: |
| 842 |
assert sum((lambda x: x == 0)(x) for x in empty) == 0 |
| 843 |
assert sum((lambda x: x == 0)(x) for x in [0]) == 1 |
| 844 |
assert sum((lambda x: x == 0)(x) for x in [0, 0, 0]) == 3 |
| 845 |
assert sum((lambda x: x == 0)(x) for x in [0, 1, 0]) == 2 |
| 846 |
assert sum((lambda x: x % 2 == 0)(x) for x in range(2**10)) == 2**9 |
| 847 |
|
| 848 |
def test_sum_comparisons() -> None: |
| 849 |
assert sum(x == 0 for x in empty) == 0 |
| 850 |
assert sum(x == 0 for x in [0]) == 1 |
| 851 |
assert sum(x == 0 for x in [0, 0, 0]) == 3 |
| 852 |
assert sum(x == 0 for x in [0, 1, 0]) == 2 |
| 853 |
assert sum(x % 2 == 0 for x in range(2**10)) == 2**9 |
| 854 |
|
| 855 |
def test_sum_multi() -> None: |
| 856 |
assert sum(i + j == 0 for i, j in zip([0, 0, 0], [0, 1, 0])) == 2 |
| 857 |
|
| 858 |
def test_sum_misc() -> None: |
| 859 |
# misc cases we do optimize (note, according to sum's helptext, we don't need to support |
| 860 |
# non-numeric cases, but CPython and mypyc both do anyway) |
| 861 |
assert sum(c == 'd' for c in 'abcdd') == 2 |
| 862 |
# misc cases we do not optimize |
| 863 |
assert sum([0, 1]) == 1 |
| 864 |
assert sum([0, 1], 1) == 2 |
| 865 |
|
| 866 |
def test_sum_start_given() -> None: |
| 867 |
a = 1 |
| 868 |
assert sum((x == 0 for x in [0, 1]), a) == 2 |
| 869 |
assert sum(((lambda x: x == 0)(x) for x in empty), 1) == 1 |
| 870 |
assert sum(((lambda x: x == 0)(x) for x in [0]), 1) == 2 |
| 871 |
assert sum(((lambda x: x == 0)(x) for x in [0, 0, 0]), 1) == 4 |
| 872 |
assert sum(((lambda x: x == 0)(x) for x in [0, 1, 0]), 1) == 3 |
| 873 |
assert sum(((lambda x: x % 2 == 0)(x) for x in range(2**10)), 1) == 2**9 + 1 |
| 874 |
assert sum((x for x in [1, 1j]), 2j) == 1 + 3j |
| 875 |
assert sum((c == 'd' for c in 'abcdd'), 1) == 3 |
| 876 |
[typing fixtures/typing-full.pyi] |
| 877 |
|
| 878 |
[case testNoneStuff] |
| 879 |
from typing import Optional |
| 880 |
class A: |
| 881 |
x: int |
| 882 |
|
| 883 |
def lol(x: A) -> None: |
| 884 |
setattr(x, 'x', 5) |
| 885 |
|
| 886 |
def none() -> None: |
| 887 |
return |
| 888 |
|
| 889 |
def arg(x: Optional[A]) -> bool: |
| 890 |
return x is None |
| 891 |
|
| 892 |
[file driver.py] |
| 893 |
import native |
| 894 |
native.lol(native.A()) |
| 895 |
|
| 896 |
# Catch refcounting failures |
| 897 |
for i in range(10000): |
| 898 |
native.none() |
| 899 |
native.arg(None) |
| 900 |
|
| 901 |
[case testBorrowRefs] |
| 902 |
def make_garbage(arg: object) -> None: |
| 903 |
b = True |
| 904 |
while b: |
| 905 |
arg = None |
| 906 |
b = False |
| 907 |
|
| 908 |
[file driver.py] |
| 909 |
from native import make_garbage |
| 910 |
import sys |
| 911 |
|
| 912 |
def test(): |
| 913 |
x = object() |
| 914 |
r0 = sys.getrefcount(x) |
| 915 |
make_garbage(x) |
| 916 |
r1 = sys.getrefcount(x) |
| 917 |
assert r0 == r1 |
| 918 |
|
| 919 |
test() |
| 920 |
|
| 921 |
[case testFinalStaticRunFail] |
| 922 |
if False: |
| 923 |
from typing import Final |
| 924 |
|
| 925 |
if bool(): |
| 926 |
x: 'Final' = [1] |
| 927 |
|
| 928 |
def f() -> int: |
| 929 |
return x[0] |
| 930 |
|
| 931 |
[file driver.py] |
| 932 |
from native import f |
| 933 |
try: |
| 934 |
print(f()) |
| 935 |
except NameError as e: |
| 936 |
print(e.args[0]) |
| 937 |
[out] |
| 938 |
value for final name "x" was not set |
| 939 |
|
| 940 |
[case testFinalStaticRunListTupleInt] |
| 941 |
if False: |
| 942 |
from typing import Final |
| 943 |
|
| 944 |
x: 'Final' = [1] |
| 945 |
y: 'Final' = (1, 2) |
| 946 |
z: 'Final' = 1 + 1 |
| 947 |
|
| 948 |
def f() -> int: |
| 949 |
return x[0] |
| 950 |
def g() -> int: |
| 951 |
return y[0] |
| 952 |
def h() -> int: |
| 953 |
return z - 1 |
| 954 |
|
| 955 |
[file driver.py] |
| 956 |
from native import f, g, h, x, y, z |
| 957 |
print(f()) |
| 958 |
print(x[0]) |
| 959 |
print(g()) |
| 960 |
print(y) |
| 961 |
print(h()) |
| 962 |
print(z) |
| 963 |
[out] |
| 964 |
1 |
| 965 |
1 |
| 966 |
1 |
| 967 |
(1, 2) |
| 968 |
1 |
| 969 |
2 |
| 970 |
|
| 971 |
[case testCheckVersion] |
| 972 |
import sys |
| 973 |
|
| 974 |
if sys.version_info[:2] == (3, 16): |
| 975 |
def version() -> int: |
| 976 |
return 16 |
| 977 |
elif sys.version_info[:2] == (3, 15): |
| 978 |
def version() -> int: |
| 979 |
return 15 |
| 980 |
elif sys.version_info[:2] == (3, 14): |
| 981 |
def version() -> int: |
| 982 |
return 14 |
| 983 |
elif sys.version_info[:2] == (3, 13): |
| 984 |
def version() -> int: |
| 985 |
return 13 |
| 986 |
elif sys.version_info[:2] == (3, 12): |
| 987 |
def version() -> int: |
| 988 |
return 12 |
| 989 |
elif sys.version_info[:2] == (3, 11): |
| 990 |
def version() -> int: |
| 991 |
return 11 |
| 992 |
elif sys.version_info[:2] == (3, 10): |
| 993 |
def version() -> int: |
| 994 |
return 10 |
| 995 |
else: |
| 996 |
raise Exception("we don't support this version yet!") |
| 997 |
|
| 998 |
|
| 999 |
[file driver.py] |
| 1000 |
import sys |
| 1001 |
version = sys.version_info[:2] |
| 1002 |
|
| 1003 |
import native |
| 1004 |
assert native.version() == sys.version_info[1] |
| 1005 |
|
| 1006 |
[case testTypeErrorMessages] |
| 1007 |
from typing import Tuple |
| 1008 |
class A: |
| 1009 |
pass |
| 1010 |
class B: |
| 1011 |
pass |
| 1012 |
|
| 1013 |
def f(x: B) -> None: |
| 1014 |
pass |
| 1015 |
def g(x: Tuple[int, A]) -> None: |
| 1016 |
pass |
| 1017 |
[file driver.py] |
| 1018 |
from testutil import assertRaises |
| 1019 |
from native import A, f, g |
| 1020 |
|
| 1021 |
class Busted: |
| 1022 |
pass |
| 1023 |
Busted.__module__ = None |
| 1024 |
|
| 1025 |
with assertRaises(TypeError, "int"): |
| 1026 |
f(0) |
| 1027 |
with assertRaises(TypeError, "native.A"): |
| 1028 |
f(A()) |
| 1029 |
with assertRaises(TypeError, "tuple[None, native.A]"): |
| 1030 |
f((None, A())) |
| 1031 |
with assertRaises(TypeError, "tuple[tuple[int, str], native.A]"): |
| 1032 |
f(((1, "ha"), A())) |
| 1033 |
with assertRaises(TypeError, "tuple[<50 items>]"): |
| 1034 |
f(tuple(range(50))) |
| 1035 |
|
| 1036 |
with assertRaises(TypeError, "errored formatting real type!"): |
| 1037 |
f(Busted()) |
| 1038 |
|
| 1039 |
with assertRaises(TypeError, "tuple[int, native.A] object expected; got tuple[int, int]"): |
| 1040 |
g((20, 30)) |
| 1041 |
|
| 1042 |
[case testComprehensionShadowBinder] |
| 1043 |
def foo(x: object) -> object: |
| 1044 |
if isinstance(x, list): |
| 1045 |
return tuple(x for x in x), x |
| 1046 |
return None |
| 1047 |
|
| 1048 |
[file driver.py] |
| 1049 |
from native import foo |
| 1050 |
|
| 1051 |
assert foo(None) == None |
| 1052 |
assert foo([1, 2, 3]) == ((1, 2, 3), [1, 2, 3]) |
| 1053 |
|
| 1054 |
[case testAllLiterals] |
| 1055 |
# Test having all sorts of literals in a single file |
| 1056 |
|
| 1057 |
def test_str() -> None: |
| 1058 |
assert '' == eval("''") |
| 1059 |
assert len('foo bar' + str()) == 7 |
| 1060 |
assert 'foo bar' == eval("'foo bar'") |
| 1061 |
assert 'foo\u1245\0bar' == eval("'foo' + chr(0x1245) + chr(0) + 'bar'") |
| 1062 |
assert 'foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345' == eval("'foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345'") |
| 1063 |
assert 'Zoobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar123' == eval("'Zoobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar123'") |
| 1064 |
|
| 1065 |
def test_bytes() -> None: |
| 1066 |
assert b'' == eval("b''") |
| 1067 |
assert b'foo bar' == eval("b'foo bar'") |
| 1068 |
assert b'\xafde' == eval(r"b'\xafde'") |
| 1069 |
assert b'foo\xde\0bar' == eval("b'foo' + bytes([0xde, 0]) + b'bar'") |
| 1070 |
assert b'foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345' == eval("b'foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345foobar12345'") |
| 1071 |
|
| 1072 |
def test_int() -> None: |
| 1073 |
assert 2875872359823758923758923759 == eval('2875872359823758923758923759') |
| 1074 |
assert -552875872359823758923758923759 == eval('-552875872359823758923758923759') |
| 1075 |
|
| 1076 |
def test_float() -> None: |
| 1077 |
assert 1.5 == eval('1.5') |
| 1078 |
assert -3.75 == eval('-3.75') |
| 1079 |
assert 2.5e10 == eval('2.5e10') |
| 1080 |
assert 2.5e50 == eval('2.5e50') |
| 1081 |
assert 2.5e1000 == eval('2.5e1000') |
| 1082 |
assert -2.5e1000 == eval('-2.5e1000') |
| 1083 |
|
| 1084 |
def test_complex() -> None: |
| 1085 |
assert 1.5j == eval('1.5j') |
| 1086 |
assert 1.5j + 2.5 == eval('2.5 + 1.5j') |
| 1087 |
assert -3.75j == eval('-3.75j') |
| 1088 |
assert 2.5e10j == eval('2.5e10j') |
| 1089 |
assert 2.5e50j == eval('2.5e50j') |
| 1090 |
assert 2.5e1000j == eval('2.5e1000j') |
| 1091 |
assert 2.5e1000j + 3.5e2000 == eval('3.5e2000 + 2.5e1000j') |
| 1092 |
assert -2.5e1000j == eval('-2.5e1000j') |
| 1093 |
|
| 1094 |
[case testUnreachableExpressions] |
| 1095 |
from typing import cast |
| 1096 |
import sys |
| 1097 |
|
| 1098 |
def test_unreachable() -> None: |
| 1099 |
A = sys.platform == 'x' and foobar |
| 1100 |
B = sys.platform == 'x' and sys.foobar |
| 1101 |
C = sys.platform == 'x' and f(a, -b, 'y') > [c + e, g(y=2)] |
| 1102 |
C = sys.platform == 'x' and cast(a, b[c]) |
| 1103 |
C = sys.platform == 'x' and (lambda x: y + x) |
| 1104 |
C = sys.platform == 'x' and (x for y in z) |
| 1105 |
C = sys.platform == 'x' and [x for y in z] |
| 1106 |
C = sys.platform == 'x' and {x: x for y in z} |
| 1107 |
C = sys.platform == 'x' and {x for y in z} |
| 1108 |
|
| 1109 |
assert not A |
| 1110 |
assert not B |
| 1111 |
assert not C |
| 1112 |
|
| 1113 |
[case testDoesntSegfaultWhenTopLevelFails] |
| 1114 |
# make the initial import fail |
| 1115 |
assert False |
| 1116 |
|
| 1117 |
[file driver.py] |
| 1118 |
# load native, cause PyInit to be run, create the module but don't finish initializing the globals |
| 1119 |
for _ in range(2): |
| 1120 |
try: |
| 1121 |
import native |
| 1122 |
raise RuntimeError('exception expected') |
| 1123 |
except AssertionError: |
| 1124 |
pass |
| 1125 |
|
| 1126 |
[case testUnderscoreFunctionsInMethods] |
| 1127 |
|
| 1128 |
class A: |
| 1129 |
def _(arg): pass |
| 1130 |
def _(arg): pass |
| 1131 |
class B(A): |
| 1132 |
def _(arg): pass |
| 1133 |
def _(arg): pass |
| 1134 |
|
| 1135 |
def test_underscore() -> None: |
| 1136 |
A() |
| 1137 |
B() |
| 1138 |
|
| 1139 |
[case testGlobalRedefinition_toplevel] |
| 1140 |
# mypy: allow-redefinition-old |
| 1141 |
i = 0 |
| 1142 |
i += 1 |
| 1143 |
i = "foo" |
| 1144 |
i += i |
| 1145 |
i = b"foo" |
| 1146 |
|
| 1147 |
def test_redefinition() -> None: |
| 1148 |
assert i == b"foo" |
| 1149 |
|
| 1150 |
[case testWithNative] |
| 1151 |
class DummyContext: |
| 1152 |
def __init__(self): |
| 1153 |
self.c = 0 |
| 1154 |
def __enter__(self) -> None: |
| 1155 |
self.c += 1 |
| 1156 |
def __exit__(self, exc_type, exc_val, exc_tb) -> None: |
| 1157 |
self.c -= 1 |
| 1158 |
|
| 1159 |
def test_dummy_context() -> None: |
| 1160 |
c = DummyContext() |
| 1161 |
with c: |
| 1162 |
assert c.c == 1 |
| 1163 |
assert c.c == 0 |
| 1164 |
|
| 1165 |
[case testWithNativeVarArgs] |
| 1166 |
class DummyContext: |
| 1167 |
def __init__(self): |
| 1168 |
self.c = 0 |
| 1169 |
def __enter__(self) -> None: |
| 1170 |
self.c += 1 |
| 1171 |
def __exit__(self, *args: object) -> None: |
| 1172 |
self.c -= 1 |
| 1173 |
|
| 1174 |
def test_dummy_context() -> None: |
| 1175 |
c = DummyContext() |
| 1176 |
with c: |
| 1177 |
assert c.c == 1 |
| 1178 |
assert c.c == 0 |
| 1179 |
|
| 1180 |
[case testIsInstanceTuple] |
| 1181 |
from typing import Any |
| 1182 |
|
| 1183 |
def isinstance_empty(x: Any) -> bool: |
| 1184 |
return isinstance(x, ()) |
| 1185 |
def isinstance_single(x: Any) -> bool: |
| 1186 |
return isinstance(x, (str,)) |
| 1187 |
def isinstance_multi(x: Any) -> bool: |
| 1188 |
return isinstance(x, (str, int)) |
| 1189 |
|
| 1190 |
def test_isinstance_empty() -> None: |
| 1191 |
assert isinstance_empty("a") is False |
| 1192 |
assert isinstance_empty(1) is False |
| 1193 |
assert isinstance_empty(None) is False |
| 1194 |
def test_isinstance_single() -> None: |
| 1195 |
assert isinstance_single("a") is True |
| 1196 |
assert isinstance_single(1) is False |
| 1197 |
assert isinstance_single(None) is False |
| 1198 |
def test_isinstance_multi() -> None: |
| 1199 |
assert isinstance_multi("a") is True |
| 1200 |
assert isinstance_multi(1) is True |
| 1201 |
assert isinstance_multi(None) is False |