| 1 |
# Test cases for (some) dunder methods (compile and run) |
| 2 |
|
| 3 |
[case testDundersMisc] |
| 4 |
# Legacy test case for dunders (don't add more here) |
| 5 |
|
| 6 |
from typing import Any |
| 7 |
class Item: |
| 8 |
def __init__(self, value: str) -> None: |
| 9 |
self.value = value |
| 10 |
|
| 11 |
def __hash__(self) -> int: |
| 12 |
return hash(self.value) |
| 13 |
|
| 14 |
def __eq__(self, rhs: object) -> bool: |
| 15 |
return isinstance(rhs, Item) and self.value == rhs.value |
| 16 |
|
| 17 |
def __lt__(self, x: 'Item') -> bool: |
| 18 |
return self.value < x.value |
| 19 |
|
| 20 |
class Subclass1(Item): |
| 21 |
def __bool__(self) -> bool: |
| 22 |
return bool(self.value) |
| 23 |
|
| 24 |
class NonBoxedThing: |
| 25 |
def __getitem__(self, index: Item) -> Item: |
| 26 |
return Item("2 * " + index.value + " + 1") |
| 27 |
|
| 28 |
class BoxedThing: |
| 29 |
def __getitem__(self, index: int) -> int: |
| 30 |
return 2 * index + 1 |
| 31 |
|
| 32 |
class Subclass2(BoxedThing): |
| 33 |
pass |
| 34 |
|
| 35 |
def index_into(x : Any, y : Any) -> Any: |
| 36 |
return x[y] |
| 37 |
|
| 38 |
def internal_index_into() -> None: |
| 39 |
x = BoxedThing() |
| 40 |
print (x[3]) |
| 41 |
y = NonBoxedThing() |
| 42 |
z = Item("3") |
| 43 |
print(y[z].value) |
| 44 |
|
| 45 |
def is_truthy(x: Item) -> bool: |
| 46 |
return True if x else False |
| 47 |
|
| 48 |
[file driver.py] |
| 49 |
from native import * |
| 50 |
x = BoxedThing() |
| 51 |
y = 3 |
| 52 |
print(x[y], index_into(x, y)) |
| 53 |
|
| 54 |
x = Subclass2() |
| 55 |
y = 3 |
| 56 |
print(x[y], index_into(x, y)) |
| 57 |
|
| 58 |
z = NonBoxedThing() |
| 59 |
w = Item("3") |
| 60 |
print(z[w].value, index_into(z, w).value) |
| 61 |
|
| 62 |
i1 = Item('lolol') |
| 63 |
i2 = Item('lol' + 'ol') |
| 64 |
i3 = Item('xyzzy') |
| 65 |
assert hash(i1) == hash(i2) |
| 66 |
|
| 67 |
assert i1 == i2 |
| 68 |
assert not i1 != i2 |
| 69 |
assert not i1 == i3 |
| 70 |
assert i1 != i3 |
| 71 |
assert i2 < i3 |
| 72 |
assert not i1 < i2 |
| 73 |
assert i1 == Subclass1('lolol') |
| 74 |
|
| 75 |
assert is_truthy(Item('')) |
| 76 |
assert is_truthy(Item('a')) |
| 77 |
assert not is_truthy(Subclass1('')) |
| 78 |
assert is_truthy(Subclass1('a')) |
| 79 |
|
| 80 |
internal_index_into() |
| 81 |
[out] |
| 82 |
7 7 |
| 83 |
7 7 |
| 84 |
2 * 3 + 1 2 * 3 + 1 |
| 85 |
7 |
| 86 |
2 * 3 + 1 |
| 87 |
|
| 88 |
[case testDundersContainer] |
| 89 |
# Sequence/mapping dunder methods |
| 90 |
|
| 91 |
from typing import Any |
| 92 |
|
| 93 |
class Seq: |
| 94 |
def __init__(self) -> None: |
| 95 |
self.key = 0 |
| 96 |
self.value = 0 |
| 97 |
|
| 98 |
def __len__(self) -> int: |
| 99 |
return 5 |
| 100 |
|
| 101 |
def __setitem__(self, key: int, value: int) -> None: |
| 102 |
self.key = key |
| 103 |
self.value = value |
| 104 |
|
| 105 |
def __contains__(self, x: int) -> bool: |
| 106 |
return x == 3 |
| 107 |
|
| 108 |
def __delitem__(self, key: int) -> None: |
| 109 |
self.key = key |
| 110 |
|
| 111 |
class Plain: pass |
| 112 |
|
| 113 |
def any_seq() -> Any: |
| 114 |
"""Return Any-typed Seq.""" |
| 115 |
return Seq() |
| 116 |
|
| 117 |
def any_plain() -> Any: |
| 118 |
"""Return Any-typed Seq.""" |
| 119 |
return Plain() |
| 120 |
|
| 121 |
def test_len() -> None: |
| 122 |
assert len(any_seq()) == 5 |
| 123 |
assert len(Seq()) == 5 |
| 124 |
|
| 125 |
def test_len_error() -> None: |
| 126 |
try: |
| 127 |
len(any_plain()) |
| 128 |
except TypeError: |
| 129 |
pass |
| 130 |
else: |
| 131 |
assert False |
| 132 |
|
| 133 |
def test_set_item() -> None: |
| 134 |
s = any_seq() |
| 135 |
s[44] = 66 |
| 136 |
assert s.key == 44 and s.value == 66 |
| 137 |
ss = Seq() |
| 138 |
ss[33] = 55 |
| 139 |
assert ss.key == 33 and ss.value == 55 |
| 140 |
|
| 141 |
def test_contains() -> None: |
| 142 |
assert 3 in any_seq() |
| 143 |
assert 4 not in any_seq() |
| 144 |
assert 2 not in any_seq() |
| 145 |
assert 3 in Seq() |
| 146 |
assert 4 not in Seq() |
| 147 |
assert 2 not in Seq() |
| 148 |
|
| 149 |
def test_delitem() -> None: |
| 150 |
s = any_seq() |
| 151 |
del s[55] |
| 152 |
assert s.key == 55 |
| 153 |
|
| 154 |
class SeqAny: |
| 155 |
def __contains__(self, x: Any) -> Any: |
| 156 |
return x == 3 |
| 157 |
|
| 158 |
def __setitem__(self, x: Any, y: Any) -> Any: |
| 159 |
self.x = x |
| 160 |
return 'x' |
| 161 |
|
| 162 |
def test_contains_any() -> None: |
| 163 |
assert (3 in SeqAny()) is True |
| 164 |
assert (2 in SeqAny()) is False |
| 165 |
assert (3 not in SeqAny()) is False |
| 166 |
assert (2 not in SeqAny()) is True |
| 167 |
s = SeqAny() # type: Any |
| 168 |
assert (3 in s) is True |
| 169 |
assert (2 in s) is False |
| 170 |
assert (3 not in s) is False |
| 171 |
assert (2 not in s) is True |
| 172 |
|
| 173 |
def test_set_item_any() -> None: |
| 174 |
s = SeqAny() |
| 175 |
s[4] = 6 |
| 176 |
assert s.x == 4 |
| 177 |
ss = SeqAny() # type: Any |
| 178 |
ss[5] = 7 |
| 179 |
assert ss.x == 5 |
| 180 |
|
| 181 |
class SeqError: |
| 182 |
def __setitem__(self, key: int, value: int) -> None: |
| 183 |
raise RuntimeError() |
| 184 |
|
| 185 |
def __contains__(self, x: int) -> bool: |
| 186 |
raise RuntimeError() |
| 187 |
|
| 188 |
def __len__(self) -> int: |
| 189 |
return -5 |
| 190 |
|
| 191 |
def any_seq_error() -> Any: |
| 192 |
return SeqError() |
| 193 |
|
| 194 |
def test_set_item_error_propagate() -> None: |
| 195 |
s = any_seq_error() |
| 196 |
try: |
| 197 |
s[44] = 66 |
| 198 |
except RuntimeError: |
| 199 |
pass |
| 200 |
else: |
| 201 |
assert False |
| 202 |
|
| 203 |
def test_contains_error_propagate() -> None: |
| 204 |
s = any_seq_error() |
| 205 |
try: |
| 206 |
3 in s |
| 207 |
except RuntimeError: |
| 208 |
pass |
| 209 |
else: |
| 210 |
assert False |
| 211 |
|
| 212 |
def test_negative_len() -> None: |
| 213 |
try: |
| 214 |
len(SeqError()) |
| 215 |
except ValueError: |
| 216 |
pass |
| 217 |
else: |
| 218 |
assert False |
| 219 |
|
| 220 |
class DelItemNoSetItem: |
| 221 |
def __delitem__(self, x: int) -> None: |
| 222 |
self.key = x |
| 223 |
|
| 224 |
def test_del_item_with_no_set_item() -> None: |
| 225 |
o = DelItemNoSetItem() |
| 226 |
del o[22] |
| 227 |
assert o.key == 22 |
| 228 |
a = o # type: Any |
| 229 |
del a[12] |
| 230 |
assert a.key == 12 |
| 231 |
try: |
| 232 |
a[1] = 2 |
| 233 |
except TypeError as e: |
| 234 |
assert str(e) == "'DelItemNoSetItem' object does not support item assignment" |
| 235 |
else: |
| 236 |
assert False |
| 237 |
|
| 238 |
class SetItemOverride(dict): |
| 239 |
# Only override __setitem__, __delitem__ comes from dict |
| 240 |
|
| 241 |
def __setitem__(self, x: int, y: int) -> None: |
| 242 |
self.key = x |
| 243 |
self.value = y |
| 244 |
|
| 245 |
def test_set_item_override() -> None: |
| 246 |
o = SetItemOverride({'x': 12, 'y': 13}) |
| 247 |
o[2] = 3 |
| 248 |
assert o.key == 2 and o.value == 3 |
| 249 |
a = o # type: Any |
| 250 |
o[4] = 5 |
| 251 |
assert o.key == 4 and o.value == 5 |
| 252 |
assert o['x'] == 12 |
| 253 |
assert o['y'] == 13 |
| 254 |
del o['x'] |
| 255 |
assert 'x' not in o and 'y' in o |
| 256 |
del a['y'] |
| 257 |
assert 'y' not in a and 'x' not in a |
| 258 |
|
| 259 |
class DelItemOverride(dict): |
| 260 |
# Only override __delitem__, __setitem__ comes from dict |
| 261 |
|
| 262 |
def __delitem__(self, x: int) -> None: |
| 263 |
self.key = x |
| 264 |
|
| 265 |
def test_del_item_override() -> None: |
| 266 |
o = DelItemOverride() |
| 267 |
del o[2] |
| 268 |
assert o.key == 2 |
| 269 |
a = o # type: Any |
| 270 |
del o[5] |
| 271 |
assert o.key == 5 |
| 272 |
o['x'] = 12 |
| 273 |
assert o['x'] == 12 |
| 274 |
a['y'] = 13 |
| 275 |
assert a['y'] == 13 |
| 276 |
|
| 277 |
class SetItemOverrideNative(Seq): |
| 278 |
def __setitem__(self, key: int, value: int) -> None: |
| 279 |
self.key = key + 1 |
| 280 |
self.value = value + 1 |
| 281 |
|
| 282 |
def test_native_set_item_override() -> None: |
| 283 |
o = SetItemOverrideNative() |
| 284 |
o[1] = 4 |
| 285 |
assert o.key == 2 and o.value == 5 |
| 286 |
del o[6] |
| 287 |
assert o.key == 6 |
| 288 |
a = o # type: Any |
| 289 |
a[10] = 12 |
| 290 |
assert a.key == 11 and a.value == 13 |
| 291 |
del a[16] |
| 292 |
assert a.key == 16 |
| 293 |
|
| 294 |
class DelItemOverrideNative(Seq): |
| 295 |
def __delitem__(self, key: int) -> None: |
| 296 |
self.key = key + 2 |
| 297 |
|
| 298 |
def test_native_del_item_override() -> None: |
| 299 |
o = DelItemOverrideNative() |
| 300 |
o[1] = 4 |
| 301 |
assert o.key == 1 and o.value == 4 |
| 302 |
del o[6] |
| 303 |
assert o.key == 8 |
| 304 |
a = o # type: Any |
| 305 |
a[10] = 12 |
| 306 |
assert a.key == 10 and a.value == 12 |
| 307 |
del a[16] |
| 308 |
assert a.key == 18 |
| 309 |
|
| 310 |
[case testDundersNumber] |
| 311 |
from typing import Any |
| 312 |
|
| 313 |
class C: |
| 314 |
def __init__(self, x: int) -> None: |
| 315 |
self.x = x |
| 316 |
|
| 317 |
def __neg__(self) -> int: |
| 318 |
return self.x + 1 |
| 319 |
|
| 320 |
def __invert__(self) -> int: |
| 321 |
return self.x + 2 |
| 322 |
|
| 323 |
def __int__(self) -> int: |
| 324 |
return self.x + 3 |
| 325 |
|
| 326 |
def __float__(self) -> float: |
| 327 |
return float(self.x + 4) |
| 328 |
|
| 329 |
def __pos__(self) -> int: |
| 330 |
return self.x + 5 |
| 331 |
|
| 332 |
def __abs__(self) -> int: |
| 333 |
return abs(self.x) + 6 |
| 334 |
|
| 335 |
|
| 336 |
def test_unary_dunders_generic() -> None: |
| 337 |
a: Any = C(10) |
| 338 |
|
| 339 |
assert -a == 11 |
| 340 |
assert ~a == 12 |
| 341 |
assert int(a) == 13 |
| 342 |
assert float(a) == 14.0 |
| 343 |
assert +a == 15 |
| 344 |
assert abs(a) == 16 |
| 345 |
|
| 346 |
def test_unary_dunders_native() -> None: |
| 347 |
c = C(10) |
| 348 |
|
| 349 |
assert -c == 11 |
| 350 |
assert ~c == 12 |
| 351 |
assert int(c) == 13 |
| 352 |
assert float(c) == 14.0 |
| 353 |
assert +c == 15 |
| 354 |
assert abs(c) == 16 |
| 355 |
|
| 356 |
[case testDundersBinarySimple] |
| 357 |
from typing import Any |
| 358 |
|
| 359 |
class C: |
| 360 |
def __init__(self) -> None: |
| 361 |
self.x = 5 |
| 362 |
|
| 363 |
def __add__(self, y: int) -> int: |
| 364 |
return self.x + y |
| 365 |
|
| 366 |
def __sub__(self, y: int) -> int: |
| 367 |
return self.x - y |
| 368 |
|
| 369 |
def __mul__(self, y: int) -> int: |
| 370 |
return self.x * y |
| 371 |
|
| 372 |
def __mod__(self, y: int) -> int: |
| 373 |
return self.x % y |
| 374 |
|
| 375 |
def __lshift__(self, y: int) -> int: |
| 376 |
return self.x << y |
| 377 |
|
| 378 |
def __rshift__(self, y: int) -> int: |
| 379 |
return self.x >> y |
| 380 |
|
| 381 |
def __and__(self, y: int) -> int: |
| 382 |
return self.x & y |
| 383 |
|
| 384 |
def __or__(self, y: int) -> int: |
| 385 |
return self.x | y |
| 386 |
|
| 387 |
def __xor__(self, y: int) -> int: |
| 388 |
return self.x ^ y |
| 389 |
|
| 390 |
def __matmul__(self, y: int) -> int: |
| 391 |
return self.x + y + 10 |
| 392 |
|
| 393 |
def __truediv__(self, y: int) -> int: |
| 394 |
return self.x + y + 20 |
| 395 |
|
| 396 |
def __floordiv__(self, y: int) -> int: |
| 397 |
return self.x + y + 30 |
| 398 |
|
| 399 |
def __divmod__(self, y: int) -> int: |
| 400 |
return self.x + y + 40 |
| 401 |
|
| 402 |
def __pow__(self, y: int) -> int: |
| 403 |
return self.x + y + 50 |
| 404 |
|
| 405 |
def test_generic() -> None: |
| 406 |
a: Any = C() |
| 407 |
assert a + 3 == 8 |
| 408 |
assert a - 3 == 2 |
| 409 |
assert a * 5 == 25 |
| 410 |
assert a % 2 == 1 |
| 411 |
assert a << 4 == 80 |
| 412 |
assert a >> 0 == 5 |
| 413 |
assert a >> 1 == 2 |
| 414 |
assert a & 1 == 1 |
| 415 |
assert a | 3 == 7 |
| 416 |
assert a ^ 3 == 6 |
| 417 |
assert a @ 3 == 18 |
| 418 |
assert a / 2 == 27 |
| 419 |
assert a // 2 == 37 |
| 420 |
assert divmod(a, 2) == 47 |
| 421 |
assert a ** 2 == 57 |
| 422 |
|
| 423 |
def test_native() -> None: |
| 424 |
c = C() |
| 425 |
assert c + 3 == 8 |
| 426 |
assert c - 3 == 2 |
| 427 |
assert divmod(c, 3) == 48 |
| 428 |
assert c ** 3 == 58 |
| 429 |
|
| 430 |
def test_error() -> None: |
| 431 |
a: Any = C() |
| 432 |
try: |
| 433 |
a + 'x' |
| 434 |
except TypeError as e: |
| 435 |
assert str(e) == "unsupported operand type(s) for +: 'C' and 'str'" |
| 436 |
else: |
| 437 |
assert False |
| 438 |
try: |
| 439 |
a - 'x' |
| 440 |
except TypeError as e: |
| 441 |
assert str(e) == "unsupported operand type(s) for -: 'C' and 'str'" |
| 442 |
else: |
| 443 |
assert False |
| 444 |
try: |
| 445 |
a ** 'x' |
| 446 |
except TypeError as e: |
| 447 |
assert str(e) == "unsupported operand type(s) for **: 'C' and 'str'" |
| 448 |
else: |
| 449 |
assert False |
| 450 |
|
| 451 |
[case testDundersBinaryReverse] |
| 452 |
from typing import Any |
| 453 |
|
| 454 |
class C: |
| 455 |
def __init__(self) -> None: |
| 456 |
self.x = 5 |
| 457 |
|
| 458 |
def __add__(self, y: int) -> int: |
| 459 |
return self.x + y |
| 460 |
|
| 461 |
def __radd__(self, y: int) -> int: |
| 462 |
return self.x + y + 1 |
| 463 |
|
| 464 |
def __sub__(self, y: int) -> int: |
| 465 |
return self.x - y |
| 466 |
|
| 467 |
def __rsub__(self, y: int) -> int: |
| 468 |
return self.x - y - 1 |
| 469 |
|
| 470 |
def __pow__(self, y: int) -> int: |
| 471 |
return self.x**y |
| 472 |
|
| 473 |
def __rpow__(self, y: int) -> int: |
| 474 |
return self.x**y + 1 |
| 475 |
|
| 476 |
def test_generic() -> None: |
| 477 |
a: Any = C() |
| 478 |
assert a + 3 == 8 |
| 479 |
assert 4 + a == 10 |
| 480 |
assert a - 3 == 2 |
| 481 |
assert 4 - a == 0 |
| 482 |
assert a**3 == 125 |
| 483 |
assert 4**a == 626 |
| 484 |
|
| 485 |
def test_native() -> None: |
| 486 |
c = C() |
| 487 |
assert c + 3 == 8 |
| 488 |
assert 4 + c == 10 |
| 489 |
assert c - 3 == 2 |
| 490 |
assert 4 - c == 0 |
| 491 |
assert c**3 == 125 |
| 492 |
assert 4**c == 626 |
| 493 |
|
| 494 |
def test_errors() -> None: |
| 495 |
a: Any = C() |
| 496 |
try: |
| 497 |
a + 'x' |
| 498 |
except TypeError as e: |
| 499 |
assert str(e) == "unsupported operand type(s) for +: 'C' and 'str'" |
| 500 |
else: |
| 501 |
assert False |
| 502 |
try: |
| 503 |
a - 'x' |
| 504 |
except TypeError as e: |
| 505 |
assert str(e) == "unsupported operand type(s) for -: 'C' and 'str'" |
| 506 |
else: |
| 507 |
assert False |
| 508 |
try: |
| 509 |
'x' + a |
| 510 |
except TypeError as e: |
| 511 |
assert str(e) in ('can only concatenate str (not "C") to str', |
| 512 |
'must be str, not C') |
| 513 |
else: |
| 514 |
assert False |
| 515 |
try: |
| 516 |
'x' ** a |
| 517 |
except TypeError as e: |
| 518 |
assert str(e) == "unsupported operand type(s) for ** or pow(): 'str' and 'C'" |
| 519 |
else: |
| 520 |
assert False |
| 521 |
|
| 522 |
|
| 523 |
class F: |
| 524 |
def __add__(self, x: int) -> int: |
| 525 |
return 5 |
| 526 |
|
| 527 |
def __pow__(self, x: int) -> int: |
| 528 |
return -5 |
| 529 |
|
| 530 |
class G: |
| 531 |
def __add__(self, x: int) -> int: |
| 532 |
return 33 |
| 533 |
|
| 534 |
def __pow__(self, x: int) -> int: |
| 535 |
return -33 |
| 536 |
|
| 537 |
def __radd__(self, x: F) -> int: |
| 538 |
return 6 |
| 539 |
|
| 540 |
def __rpow__(self, x: F) -> int: |
| 541 |
return -6 |
| 542 |
|
| 543 |
def test_type_mismatch_fall_back_to_reverse() -> None: |
| 544 |
assert F() + G() == 6 |
| 545 |
assert F()**G() == -6 |
| 546 |
|
| 547 |
[case testDundersBinaryNotImplemented] |
| 548 |
# mypy: allow-untyped-defs |
| 549 |
from typing import Any, Union |
| 550 |
from testutil import assertRaises |
| 551 |
|
| 552 |
class C: |
| 553 |
def __init__(self, v: int) -> None: |
| 554 |
self.v = v |
| 555 |
|
| 556 |
def __add__(self, y: int) -> Union[int, Any]: |
| 557 |
if y == 1: |
| 558 |
return self.v |
| 559 |
return NotImplemented |
| 560 |
|
| 561 |
def test_any_add() -> None: |
| 562 |
a: Any = C(4) |
| 563 |
assert a + 1 == 4 |
| 564 |
try: |
| 565 |
a + 2 |
| 566 |
except TypeError: |
| 567 |
pass |
| 568 |
else: |
| 569 |
assert False |
| 570 |
|
| 571 |
class D: |
| 572 |
def __init__(self, x: int) -> None: |
| 573 |
self.x = x |
| 574 |
|
| 575 |
def __add__(self, e: E) -> Union[int, Any]: |
| 576 |
if e.x == 1: |
| 577 |
return 2 |
| 578 |
return NotImplemented |
| 579 |
|
| 580 |
class E: |
| 581 |
def __init__(self, x: int) -> None: |
| 582 |
self.x = x |
| 583 |
|
| 584 |
def __radd__(self, d: D) -> Union[int, Any]: |
| 585 |
if d.x == 3: |
| 586 |
return 4 |
| 587 |
return NotImplemented |
| 588 |
|
| 589 |
def test_any_radd() -> None: |
| 590 |
d1: Any = D(1) |
| 591 |
d3: Any = D(3) |
| 592 |
e1: Any = E(1) |
| 593 |
e3: Any = E(3) |
| 594 |
assert d1 + e1 == 2 |
| 595 |
assert d3 + e1 == 2 |
| 596 |
assert d3 + e3 == 4 |
| 597 |
|
| 598 |
class F: |
| 599 |
def __init__(self, v): |
| 600 |
self.v = v |
| 601 |
|
| 602 |
def __add__(self, x): |
| 603 |
if isinstance(x, int): |
| 604 |
return self.v + x |
| 605 |
return NotImplemented |
| 606 |
|
| 607 |
class G: |
| 608 |
def __radd__(self, x): |
| 609 |
if isinstance(x, F): |
| 610 |
return x.v + 1 |
| 611 |
if isinstance(x, str): |
| 612 |
return 'a' |
| 613 |
return NotImplemented |
| 614 |
|
| 615 |
def test_unannotated_add() -> None: |
| 616 |
o = F(4) |
| 617 |
assert o + 5 == 9 |
| 618 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'F' and 'str'"): |
| 619 |
o + 'x' |
| 620 |
|
| 621 |
o2: Any = F(4) |
| 622 |
assert o2 + 5 == 9 |
| 623 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'F' and 'str'"): |
| 624 |
o2 + 'x' |
| 625 |
|
| 626 |
def test_unannotated_add_and_radd_1() -> None: |
| 627 |
o = F(4) |
| 628 |
assert o + G() == 5 |
| 629 |
|
| 630 |
o2: Any = F(4) |
| 631 |
assert o2 + G() == 5 |
| 632 |
|
| 633 |
def test_unannotated_radd() -> None: |
| 634 |
assert 'x' + G() == 'a' |
| 635 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'G'"): |
| 636 |
1 + G() |
| 637 |
|
| 638 |
o: Any = G() |
| 639 |
assert 'x' + o == 'a' |
| 640 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'G'"): |
| 641 |
1 + o |
| 642 |
|
| 643 |
class H: |
| 644 |
def __add__(self, x): |
| 645 |
if isinstance(x, int): |
| 646 |
return x + 1 |
| 647 |
return NotImplemented |
| 648 |
|
| 649 |
def __radd__(self, x): |
| 650 |
if isinstance(x, str): |
| 651 |
return 22 |
| 652 |
return NotImplemented |
| 653 |
|
| 654 |
def test_unannotated_add_and_radd_2() -> None: |
| 655 |
h = H() |
| 656 |
assert h + 5 == 6 |
| 657 |
assert 'x' + h == 22 |
| 658 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'H'"): |
| 659 |
1 + h |
| 660 |
|
| 661 |
h2: Any = H() |
| 662 |
assert h + 5 == 6 |
| 663 |
assert 'x' + h == 22 |
| 664 |
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'H'"): |
| 665 |
1 + h |
| 666 |
|
| 667 |
# TODO: Inheritance |
| 668 |
|
| 669 |
[case testDifferentReverseDunders] |
| 670 |
from typing import Any |
| 671 |
|
| 672 |
class C: |
| 673 |
# __radd__ and __rsub__ are tested elsewhere |
| 674 |
|
| 675 |
def __rmul__(self, x: Any) -> int: |
| 676 |
return 1 |
| 677 |
|
| 678 |
def __rtruediv__(self, x: Any) -> int: |
| 679 |
return 2 |
| 680 |
|
| 681 |
def __rmod__(self, x: Any) -> int: |
| 682 |
return 3 |
| 683 |
|
| 684 |
def __rfloordiv__(self, x: Any) -> int: |
| 685 |
return 4 |
| 686 |
|
| 687 |
def __rlshift__(self, x: Any) -> int: |
| 688 |
return 5 |
| 689 |
|
| 690 |
def __rrshift__(self, x: Any) -> int: |
| 691 |
return 6 |
| 692 |
|
| 693 |
def __rand__(self, x: Any) -> int: |
| 694 |
return 7 |
| 695 |
|
| 696 |
def __ror__(self, x: Any) -> int: |
| 697 |
return 8 |
| 698 |
|
| 699 |
def __rxor__(self, x: Any) -> int: |
| 700 |
return 9 |
| 701 |
|
| 702 |
def __rmatmul__(self, x: Any) -> int: |
| 703 |
return 10 |
| 704 |
|
| 705 |
def test_reverse_dunders() -> None: |
| 706 |
x = 0 |
| 707 |
c = C() |
| 708 |
assert x * c == 1 |
| 709 |
assert x / c == 2 |
| 710 |
assert x % c == 3 |
| 711 |
assert x // c == 4 |
| 712 |
assert x << c == 5 |
| 713 |
assert x >> c == 6 |
| 714 |
assert x & c == 7 |
| 715 |
assert x | c == 8 |
| 716 |
assert x ^ c == 9 |
| 717 |
assert x @ c == 10 |
| 718 |
|
| 719 |
[case testDundersInplace] |
| 720 |
from typing import Any |
| 721 |
from testutil import assertRaises |
| 722 |
|
| 723 |
class C: |
| 724 |
def __init__(self) -> None: |
| 725 |
self.x = 5 |
| 726 |
|
| 727 |
def __iadd__(self, y: int) -> C: |
| 728 |
self.x += y |
| 729 |
return self |
| 730 |
|
| 731 |
def __isub__(self, y: int) -> C: |
| 732 |
self.x -= y |
| 733 |
return self |
| 734 |
|
| 735 |
def __imul__(self, y: int) -> C: |
| 736 |
self.x *= y |
| 737 |
return self |
| 738 |
|
| 739 |
def __imod__(self, y: int) -> C: |
| 740 |
self.x %= y |
| 741 |
return self |
| 742 |
|
| 743 |
def __itruediv__(self, y: int) -> C: |
| 744 |
self.x += y + 10 |
| 745 |
return self |
| 746 |
|
| 747 |
def __ifloordiv__(self, y: int) -> C: |
| 748 |
self.x += y + 20 |
| 749 |
return self |
| 750 |
|
| 751 |
def __ilshift__(self, y: int) -> C: |
| 752 |
self.x <<= y |
| 753 |
return self |
| 754 |
|
| 755 |
def __irshift__(self, y: int) -> C: |
| 756 |
self.x >>= y |
| 757 |
return self |
| 758 |
|
| 759 |
def __iand__(self, y: int) -> C: |
| 760 |
self.x &= y |
| 761 |
return self |
| 762 |
|
| 763 |
def __ior__(self, y: int) -> C: |
| 764 |
self.x |= y |
| 765 |
return self |
| 766 |
|
| 767 |
def __ixor__(self, y: int) -> C: |
| 768 |
self.x ^= y |
| 769 |
return self |
| 770 |
|
| 771 |
def __imatmul__(self, y: int) -> C: |
| 772 |
self.x += y + 5 |
| 773 |
return self |
| 774 |
|
| 775 |
def __ipow__(self, y: int, __mod_throwaway: None = None) -> C: |
| 776 |
self.x **= y |
| 777 |
return self |
| 778 |
|
| 779 |
def test_generic_1() -> None: |
| 780 |
c: Any = C() |
| 781 |
c += 3 |
| 782 |
assert c.x == 8 |
| 783 |
c -= 5 |
| 784 |
assert c.x == 3 |
| 785 |
c *= 3 |
| 786 |
assert c.x == 9 |
| 787 |
c %= 4 |
| 788 |
assert c.x == 1 |
| 789 |
c /= 5 |
| 790 |
assert c.x == 16 |
| 791 |
c //= 4 |
| 792 |
assert c.x == 40 |
| 793 |
c **= 2 |
| 794 |
assert c.x == 1600 |
| 795 |
|
| 796 |
def test_generic_2() -> None: |
| 797 |
c: Any = C() |
| 798 |
c <<= 4 |
| 799 |
assert c.x == 80 |
| 800 |
c >>= 3 |
| 801 |
assert c.x == 10 |
| 802 |
c &= 3 |
| 803 |
assert c.x == 2 |
| 804 |
c |= 6 |
| 805 |
assert c.x == 6 |
| 806 |
c ^= 12 |
| 807 |
assert c.x == 10 |
| 808 |
c @= 3 |
| 809 |
assert c.x == 18 |
| 810 |
|
| 811 |
def test_native() -> None: |
| 812 |
c = C() |
| 813 |
c += 3 |
| 814 |
assert c.x == 8 |
| 815 |
c -= 5 |
| 816 |
assert c.x == 3 |
| 817 |
c *= 3 |
| 818 |
assert c.x == 9 |
| 819 |
c **= 2 |
| 820 |
assert c.x == 81 |
| 821 |
|
| 822 |
def test_error() -> None: |
| 823 |
c: Any = C() |
| 824 |
with assertRaises(TypeError, "int object expected; got str"): |
| 825 |
c += 'x' |
| 826 |
|
| 827 |
class BadInplaceAdd: |
| 828 |
def __init__(self) -> None: |
| 829 |
self.x = 0 |
| 830 |
|
| 831 |
def __iadd__(self, x: int) -> Any: |
| 832 |
self.x += x |
| 833 |
|
| 834 |
def test_in_place_operator_returns_none() -> None: |
| 835 |
o = BadInplaceAdd() |
| 836 |
with assertRaises(TypeError, "native.BadInplaceAdd object expected; got None"): |
| 837 |
o += 5 |
| 838 |
|
| 839 |
[case testDunderMinMax] |
| 840 |
class SomeItem: |
| 841 |
def __init__(self, val: int) -> None: |
| 842 |
self.val = val |
| 843 |
|
| 844 |
def __lt__(self, x: 'SomeItem') -> bool: |
| 845 |
return self.val < x.val |
| 846 |
|
| 847 |
def __gt__(self, x: 'SomeItem') -> bool: |
| 848 |
return self.val > x.val |
| 849 |
|
| 850 |
class AnotherItem: |
| 851 |
def __init__(self, val: str) -> None: |
| 852 |
self.val = val |
| 853 |
|
| 854 |
def __lt__(self, x: 'AnotherItem') -> bool: |
| 855 |
return True |
| 856 |
|
| 857 |
def __gt__(self, x: 'AnotherItem') -> bool: |
| 858 |
return True |
| 859 |
|
| 860 |
def test_dunder_min() -> None: |
| 861 |
x = SomeItem(5) |
| 862 |
y = SomeItem(10) |
| 863 |
z = SomeItem(15) |
| 864 |
assert min(x, y).val == 5 |
| 865 |
assert min(y, z).val == 10 |
| 866 |
assert max(x, y).val == 10 |
| 867 |
assert max(y, z).val == 15 |
| 868 |
x2 = AnotherItem('xxx') |
| 869 |
y2 = AnotherItem('yyy') |
| 870 |
z2 = AnotherItem('zzz') |
| 871 |
assert min(x2, y2).val == 'yyy' |
| 872 |
assert min(y2, x2).val == 'xxx' |
| 873 |
assert max(x2, y2).val == 'yyy' |
| 874 |
assert max(y2, x2).val == 'xxx' |
| 875 |
assert min(y2, z2).val == 'zzz' |
| 876 |
assert max(x2, z2).val == 'zzz' |
| 877 |
|
| 878 |
|
| 879 |
[case testDundersPowerSpecial] |
| 880 |
import sys |
| 881 |
from typing import Any, Optional |
| 882 |
from testutil import assertRaises |
| 883 |
|
| 884 |
class Forward: |
| 885 |
def __pow__(self, exp: int, mod: Optional[int] = None) -> int: |
| 886 |
if mod is None: |
| 887 |
return 2**exp |
| 888 |
else: |
| 889 |
return 2**exp % mod |
| 890 |
|
| 891 |
class ForwardModRequired: |
| 892 |
def __pow__(self, exp: int, mod: int) -> int: |
| 893 |
return 2**exp % mod |
| 894 |
|
| 895 |
class ForwardNotImplemented: |
| 896 |
def __pow__(self, exp: int, mod: Optional[object] = None) -> Any: |
| 897 |
return NotImplemented |
| 898 |
|
| 899 |
class Reverse: |
| 900 |
def __rpow__(self, exp: int) -> int: |
| 901 |
return 2**exp + 1 |
| 902 |
|
| 903 |
class Both: |
| 904 |
def __pow__(self, exp: int, mod: Optional[int] = None) -> int: |
| 905 |
if mod is None: |
| 906 |
return 2**exp |
| 907 |
else: |
| 908 |
return 2**exp % mod |
| 909 |
|
| 910 |
def __rpow__(self, exp: int) -> int: |
| 911 |
return 2**exp + 1 |
| 912 |
|
| 913 |
class Child(ForwardNotImplemented): |
| 914 |
def __rpow__(self, exp: object) -> int: |
| 915 |
return 50 |
| 916 |
|
| 917 |
class Inplace: |
| 918 |
value = 2 |
| 919 |
|
| 920 |
def __ipow__(self, exp: int, mod: Optional[int] = None) -> "Inplace": |
| 921 |
self.value **= exp - (mod or 0) |
| 922 |
return self |
| 923 |
|
| 924 |
def test_native() -> None: |
| 925 |
f = Forward() |
| 926 |
assert f**3 == 8 |
| 927 |
assert pow(f, 3) == 8 |
| 928 |
assert pow(f, 3, 3) == 2 |
| 929 |
assert pow(ForwardModRequired(), 3, 3) == 2 |
| 930 |
b = Both() |
| 931 |
assert b**3 == 8 |
| 932 |
assert 3**b == 9 |
| 933 |
assert pow(b, 3) == 8 |
| 934 |
assert pow(b, 3, 3) == 2 |
| 935 |
i = Inplace() |
| 936 |
i **= 2 |
| 937 |
assert i.value == 4 |
| 938 |
|
| 939 |
def test_errors() -> None: |
| 940 |
if sys.version_info[0] >= 3 and sys.version_info[1] >= 10: |
| 941 |
op = "** or pow()" |
| 942 |
else: |
| 943 |
op = "pow()" |
| 944 |
|
| 945 |
f = Forward() |
| 946 |
with assertRaises(TypeError, f"unsupported operand type(s) for {op}: 'Forward', 'int', 'str'"): |
| 947 |
pow(f, 3, "x") # type: ignore |
| 948 |
with assertRaises(TypeError, "unsupported operand type(s) for **: 'Forward' and 'str'"): |
| 949 |
f**"x" # type: ignore |
| 950 |
r = Reverse() |
| 951 |
with assertRaises(TypeError, "unsupported operand type(s) for ** or pow(): 'str' and 'Reverse'"): |
| 952 |
"x"**r # type: ignore |
| 953 |
with assertRaises(TypeError, f"unsupported operand type(s) for {op}: 'int', 'Reverse', 'int'"): |
| 954 |
# Ternary pow() does not fallback to __rpow__ if LHS's __pow__ returns NotImplemented. |
| 955 |
pow(3, r, 3) # type: ignore |
| 956 |
with assertRaises(TypeError, f"unsupported operand type(s) for {op}: 'ForwardNotImplemented', 'Child', 'int'"): |
| 957 |
# Ternary pow() does not try RHS's __rpow__ first when it's a subclass and redefines |
| 958 |
# __rpow__ unlike other ops. |
| 959 |
pow(ForwardNotImplemented(), Child(), 3) # type: ignore |
| 960 |
with assertRaises(TypeError, "unsupported operand type(s) for ** or pow(): 'ForwardModRequired' and 'int'"): |
| 961 |
ForwardModRequired()**3 # type: ignore |
| 962 |
|
| 963 |
[case testDundersWithFinal] |
| 964 |
from typing import final |
| 965 |
class A: |
| 966 |
def __init__(self, x: int) -> None: |
| 967 |
self.x = x |
| 968 |
|
| 969 |
def __add__(self, y: int) -> int: |
| 970 |
return self.x + y |
| 971 |
|
| 972 |
def __lt__(self, x: 'A') -> bool: |
| 973 |
return self.x < x.x |
| 974 |
|
| 975 |
@final |
| 976 |
class B(A): |
| 977 |
def __add__(self, y: int) -> int: |
| 978 |
return self.x + y + 1 |
| 979 |
|
| 980 |
def __lt__(self, x: 'A') -> bool: |
| 981 |
return self.x < x.x + 1 |
| 982 |
|
| 983 |
def test_final() -> None: |
| 984 |
a = A(5) |
| 985 |
b = B(5) |
| 986 |
assert a + 3 == 8 |
| 987 |
assert b + 3 == 9 |
| 988 |
assert (a < A(5)) is False |
| 989 |
assert (b < A(5)) is True |
| 990 |
|
| 991 |
[case testDundersEq] |
| 992 |
class Eq: |
| 993 |
def __init__(self, x: int) -> None: |
| 994 |
self.x = x |
| 995 |
|
| 996 |
def __eq__(self, other: object) -> bool: |
| 997 |
if not isinstance(other, Eq): |
| 998 |
return NotImplemented |
| 999 |
return self.x == other.x |
| 1000 |
|
| 1001 |
def eq(x: Eq, y: Eq) -> bool: |
| 1002 |
return x == y |
| 1003 |
|
| 1004 |
def ne(x: Eq, y: Eq) -> bool: |
| 1005 |
return x != y |
| 1006 |
|
| 1007 |
def test_equality_with_implicit_ne() -> None: |
| 1008 |
assert eq(Eq(1), Eq(1)) |
| 1009 |
assert not eq(Eq(1), Eq(2)) |
| 1010 |
assert ne(Eq(1), Eq(2)) |
| 1011 |
assert not ne(Eq(1), Eq(1)) |
| 1012 |
|
| 1013 |
[case testDundersImplicitNeWithNarrowRhs] |
| 1014 |
class Accepted: |
| 1015 |
pass |
| 1016 |
|
| 1017 |
class Eq: |
| 1018 |
def __eq__(self, other: Accepted) -> bool: # type: ignore[override] |
| 1019 |
return True |
| 1020 |
|
| 1021 |
def test_implicit_ne_with_narrow_rhs() -> None: |
| 1022 |
assert Eq() == Accepted() |
| 1023 |
assert not (Eq() != Accepted()) |