Commit 346fa3bc authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

[widgets][plotterparameterseditor] Gracefully handle default values

Sometimes the default value of a parameter does not have the required
type of that paramter and this can lead to failures further down the
code. Here, we're converting the default value to the required type so
that errors do not occur.
Fixes #278
parent ab6a89f3
Pipeline #41550 passed with stage
in 26 minutes and 22 seconds
......@@ -23,6 +23,10 @@
# #
###############################################################################
import logging
import numpy as np
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import pyqtSignal
......@@ -50,6 +54,8 @@ from .parameterwidget import InputType
from .scrollwidget import ScrollWidget
from .spinboxes import NumpySpinBox
logger = logging.getLogger(__name__)
class ParameterChoiceDialog(QDialog):
"""Dialog to retrieve a value to to add to the editable parameters"""
......@@ -120,7 +126,18 @@ class RestrictedParameterWidget(QWidget):
super().__init__(parent)
self._type = data.get("type", None)
self.default = data.get("default", None)
default = data.get("default", "" if self._type == "string" else 0)
# cast the default value to the required type
cast_fn = np.cast[self._type] if self._type != "string" else str
try:
default = cast_fn(default)
except Exception:
logger.error(
f"Failed to convert the default value: {default} to type {self._type}",
exc_info=True,
)
default = cast_fn("" if self._type == "string" else 0)
self.default = default
self.current_type = InputType[self._type.upper()]
self.modality = "single"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment