Skip to content

PreferencesHelper sets *_items preference for List preferences #129

@notmatthancock

Description

@notmatthancock

Version info (although the same problem essentially exists with traits v6.0.0):

$ edm list | grep -P "apptools|traits "
apptools             4.5.0-5         enthought/free
traits               6.1.1-1         enthought/free

Here's a small example:

Initially, the preferences file contains:

# File: myprefs.ini
[my_section]
things = "['one', 'two']"

Then, we run a script that updates the things preference list:

from apptools.preferences.api import Preferences, PreferencesHelper
from traits.api import Int, List, Str

class MyHelper(PreferencesHelper):
    preferences_path = 'my_section'
    things = List(Str)

prefs = Preferences(filename='myprefs.ini')
helper = MyHelper(preferences=prefs)
helper.things.append('another thing!')
prefs.save()

After the script is run, the preferences file looks like:

# File: myprefs.ini
[my_section]
things = "['one', 'two']"
things_items = "TraitListEvent(index=2, removed=[], added=['another thing!'])"

As far as I can tell, the culprit is this block in the PreferencesHelper class:

def _anytrait_changed(self, trait_name, old, new):
""" Static trait change handler. """
# If we were the one that set the trait (because the underlying
# preferences node changed) then do nothing.
if self.preferences and self._is_preference_trait(trait_name):
self.preferences.set('%s.%s' % (self._get_path(), trait_name), new)
return

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions