|
| 1 | +"""Test BoolItem with numpy bool types |
| 2 | +
|
| 3 | +This test ensures that BoolItem properly converts numpy.bool_ values to Python bool, |
| 4 | +which is necessary for compatibility with Qt APIs and other code that strictly requires |
| 5 | +Python bool type. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import tempfile |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import pytest |
| 13 | + |
| 14 | +import guidata.dataset as gds |
| 15 | +from guidata.io import HDF5Reader, HDF5Writer |
| 16 | + |
| 17 | + |
| 18 | +class BoolDataSet(gds.DataSet): |
| 19 | + """Test dataset with boolean items""" |
| 20 | + |
| 21 | + bool_true = gds.BoolItem("Boolean True", default=True) |
| 22 | + bool_false = gds.BoolItem("Boolean False", default=False) |
| 23 | + bool_none = gds.BoolItem("Boolean None", default=None, allow_none=True) |
| 24 | + |
| 25 | + |
| 26 | +class TestBoolItemNumpy: |
| 27 | + """Test BoolItem with numpy bool types""" |
| 28 | + |
| 29 | + def test_numpy_bool_assignment(self): |
| 30 | + """Test that assigning numpy.bool_ is converted to Python bool""" |
| 31 | + ds = BoolDataSet() |
| 32 | + |
| 33 | + # Test True |
| 34 | + ds.bool_true = np.bool_(True) |
| 35 | + assert ds.bool_true is True |
| 36 | + assert type(ds.bool_true) is bool |
| 37 | + |
| 38 | + # Test False |
| 39 | + ds.bool_false = np.bool_(False) |
| 40 | + assert ds.bool_false is False |
| 41 | + assert type(ds.bool_false) is bool |
| 42 | + |
| 43 | + def test_python_bool_assignment(self): |
| 44 | + """Test that assigning Python bool still works""" |
| 45 | + ds = BoolDataSet() |
| 46 | + |
| 47 | + # Test True |
| 48 | + ds.bool_true = True |
| 49 | + assert ds.bool_true is True |
| 50 | + assert type(ds.bool_true) is bool |
| 51 | + |
| 52 | + # Test False |
| 53 | + ds.bool_false = False |
| 54 | + assert ds.bool_false is False |
| 55 | + assert type(ds.bool_false) is bool |
| 56 | + |
| 57 | + def test_none_assignment(self): |
| 58 | + """Test that None assignment works when allow_none=True""" |
| 59 | + ds = BoolDataSet() |
| 60 | + |
| 61 | + ds.bool_none = None |
| 62 | + assert ds.bool_none is None |
| 63 | + |
| 64 | + def test_hdf5_serialization(self): |
| 65 | + """Test that HDF5 serialization/deserialization maintains Python bool type""" |
| 66 | + ds = BoolDataSet() |
| 67 | + ds.bool_true = True |
| 68 | + ds.bool_false = False |
| 69 | + |
| 70 | + with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as tmp: |
| 71 | + tmp_path = tmp.name |
| 72 | + |
| 73 | + try: |
| 74 | + # Write |
| 75 | + with HDF5Writer(tmp_path) as writer: |
| 76 | + writer.write(ds, group_name="test") |
| 77 | + |
| 78 | + # Read back |
| 79 | + ds2 = BoolDataSet() |
| 80 | + with HDF5Reader(tmp_path) as reader: |
| 81 | + reader.read("test", instance=ds2) |
| 82 | + |
| 83 | + # Verify types |
| 84 | + assert type(ds2.bool_true) is bool |
| 85 | + assert ds2.bool_true is True |
| 86 | + |
| 87 | + assert type(ds2.bool_false) is bool |
| 88 | + assert ds2.bool_false is False |
| 89 | + |
| 90 | + finally: |
| 91 | + os.unlink(tmp_path) |
| 92 | + |
| 93 | + def test_numpy_bool_after_deserialization(self): |
| 94 | + """Test that numpy.bool_ assignment works after HDF5 deserialization""" |
| 95 | + ds = BoolDataSet() |
| 96 | + ds.bool_true = True |
| 97 | + |
| 98 | + with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as tmp: |
| 99 | + tmp_path = tmp.name |
| 100 | + |
| 101 | + try: |
| 102 | + # Write and read |
| 103 | + with HDF5Writer(tmp_path) as writer: |
| 104 | + writer.write(ds, group_name="test") |
| 105 | + |
| 106 | + ds2 = BoolDataSet() |
| 107 | + with HDF5Reader(tmp_path) as reader: |
| 108 | + reader.read("test", instance=ds2) |
| 109 | + |
| 110 | + # Now assign numpy.bool_ and verify it's converted |
| 111 | + ds2.bool_true = np.bool_(False) |
| 112 | + assert type(ds2.bool_true) is bool |
| 113 | + assert ds2.bool_true is False |
| 114 | + |
| 115 | + finally: |
| 116 | + os.unlink(tmp_path) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + pytest.main([__file__, "-v"]) |
0 commit comments