Test bool caster implicit conversions.
()
| 538 | |
| 539 | |
| 540 | def test_bool_caster(): |
| 541 | """Test bool caster implicit conversions.""" |
| 542 | convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert |
| 543 | |
| 544 | def require_implicit(v): |
| 545 | pytest.raises(TypeError, noconvert, v) |
| 546 | |
| 547 | def cant_convert(v): |
| 548 | pytest.raises(TypeError, convert, v) |
| 549 | |
| 550 | # straight up bool |
| 551 | assert convert(True) is True |
| 552 | assert convert(False) is False |
| 553 | assert noconvert(True) is True |
| 554 | assert noconvert(False) is False |
| 555 | |
| 556 | # None requires implicit conversion |
| 557 | require_implicit(None) |
| 558 | assert convert(None) is False |
| 559 | |
| 560 | class A: |
| 561 | def __init__(self, x): |
| 562 | self.x = x |
| 563 | |
| 564 | def __nonzero__(self): |
| 565 | return self.x |
| 566 | |
| 567 | def __bool__(self): |
| 568 | return self.x |
| 569 | |
| 570 | class B: |
| 571 | pass |
| 572 | |
| 573 | # Arbitrary objects are not accepted |
| 574 | cant_convert(object()) |
| 575 | cant_convert(B()) |
| 576 | |
| 577 | # Objects with __nonzero__ / __bool__ defined can be converted |
| 578 | require_implicit(A(True)) |
| 579 | assert convert(A(True)) is True |
| 580 | assert convert(A(False)) is False |
| 581 | |
| 582 | |
| 583 | def test_numpy_bool(): |
nothing calls this directly
no test coverage detected