Skip to content

DictFeatWidget fails to initialize when keys are not explicitly set #64

Open

Description

@thibaudruelle

Context: I ran start_test_app on an instance of a custom driver which contained DictFeats initialized without specifying a keys=() argument. The following error shows up:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\lantz\ui\widgets.py", line 528, in __init__
    feat_widget = LabeledFeatWidget(self, target, feat)
  File "C:\Anaconda3\lib\site-packages\lantz\ui\widgets.py", line 415, in __init__
    self._widget = DictFeatWidget(parent, target, feat)
  File "C:\Anaconda3\lib\site-packages\lantz\ui\widgets.py", line 340, in __init__
    wid.feat_key = self._keys[0]
AttributeError: 'DictFeatWidget' object has no attribute '_keys'

Probable origin: In that case, keys default to None so when the DictFeatWidget is created by start_test_app the _keys attribute is not set within the if feat.keys statement before wid.feat_key = self._keys[0] is run, leading to the error.

Probable fix: wid.feat_key = self._keys[0] should only be run when keys exist.

Proposed fix: rewrite DictFeatWidget.__init__ as

def __init__(self, parent, target, feat):
        super().__init__(parent)
        self._feat = feat

        layout = QtGui.QHBoxLayout(self)

        valwid = WidgetMixin.from_feat(feat)
        valwid.bind_feat(feat)
        valwid.lantz_target = target

        if feat.keys:
            keywid = QtGui.QComboBox()
            if isinstance(feat.keys, dict):
                self._keys = list(feat.keys.keys())
            else:
                self._keys = list(feat.keys)

            keywid .addItems([str(key) for key in self._keys])
            keywid .currentIndexChanged.connect(self._combobox_changed)

            valwid.feat_key = self._keys[0]
        else:
            keywid = QtGui.QLineEdit()
            keywid .textChanged.connect(self._lineedit_changed)

        layout.addWidget(keywid )
        self._key_widget = keywid 

        layout.addWidget(valwid )
        self._value_widget = valwid

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions