MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_bool_caster

Function test_bool_caster

tests/test_builtin_casters.py:540–580  ·  view source on GitHub ↗

Test bool caster implicit conversions.

()

Source from the content-addressed store, hash-verified

538
539
540def 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
583def test_numpy_bool():

Callers

nothing calls this directly

Calls 5

require_implicitFunction · 0.85
cant_convertFunction · 0.85
BClass · 0.70
AClass · 0.70
objectFunction · 0.50

Tested by

no test coverage detected