BoolItem: numpy.bool_ Compatibility Fix
Issue Title
Fix BoolItem to convert numpy.bool_ to Python bool for Qt compatibility
Labels
- bug
- enhancement
- compatibility
Description
Problem
BoolItem values could be stored as numpy.bool_ type instead of Python bool, causing compatibility issues with Qt APIs that strictly require Python bool type.
Error encountered:
TypeError: setChecked(self, a0: bool): argument 1 has unexpected type 'numpy.bool'
This occurred in applications like DataLab when:
- Image parameters with boolean values are saved to HDF5
- Values are deserialized and later used with Qt widgets
- Qt's
QAction.setChecked() (and similar methods) reject numpy.bool_ values
This bug was reported in downstream project DataLab Issue #265.
Root Cause
When boolean values are read from HDF5 files via h5py, they can be stored as numpy.bool_ scalars. While numpy.bool_ is technically a subclass of Python bool, Qt's type system performs strict type checking and requires the native Python bool type.
Solution
Added a __set__ override to the BoolItem class that automatically converts all assigned values to Python bool:
def __set__(self, instance: DataSet, value: bool | None) -> None:
"""Set data item's value, ensuring it's a Python bool
This override ensures that numpy.bool_ values are converted to Python bool,
which is necessary for compatibility with Qt APIs that strictly require
Python bool type (e.g., QAction.setChecked()).
"""
if value is not None:
value = bool(value)
super().__set__(instance, value)
Benefits
- Generic fix: Handles the issue at the data storage layer, protecting all BoolItem users
- Prevents propagation: Stops
numpy.bool_ from spreading throughout the application
- Backward compatible:
bool(bool) is a no-op, so existing code is unaffected
- Works for all scenarios: Direct assignment, HDF5 deserialization, etc.
Testing
- Added comprehensive unit tests in
guidata/tests/unit/test_boolitem_numpy.py
- Tests cover numpy.bool_ assignment, Python bool assignment, HDF5 serialization, and mixed scenarios
- All existing guidata tests pass (58 tests)
Files Changed
guidata/dataset/dataitems.py: Added __set__ method to BoolItem class
guidata/tests/unit/test_boolitem_numpy.py: New test file (5 tests)
Impact
This fix resolves Qt compatibility issues in downstream projects (PlotPy, DataLab, Sigima) without requiring changes to those codebases.
BoolItem:numpy.bool_Compatibility FixIssue Title
Fix
BoolItemto convertnumpy.bool_to Python bool for Qt compatibilityLabels
Description
Problem
BoolItemvalues could be stored asnumpy.bool_type instead of Pythonbool, causing compatibility issues with Qt APIs that strictly require Pythonbooltype.Error encountered:
This occurred in applications like DataLab when:
QAction.setChecked()(and similar methods) rejectnumpy.bool_valuesThis bug was reported in downstream project DataLab Issue #265.
Root Cause
When boolean values are read from HDF5 files via h5py, they can be stored as
numpy.bool_scalars. Whilenumpy.bool_is technically a subclass of Pythonbool, Qt's type system performs strict type checking and requires the native Pythonbooltype.Solution
Added a
__set__override to theBoolItemclass that automatically converts all assigned values to Pythonbool:Benefits
numpy.bool_from spreading throughout the applicationbool(bool)is a no-op, so existing code is unaffectedTesting
guidata/tests/unit/test_boolitem_numpy.pyFiles Changed
guidata/dataset/dataitems.py: Added__set__method toBoolItemclassguidata/tests/unit/test_boolitem_numpy.py: New test file (5 tests)Impact
This fix resolves Qt compatibility issues in downstream projects (PlotPy, DataLab, Sigima) without requiring changes to those codebases.