Skip to content

format_func not used when EnumEditor is initialized with static values #782

@kitchoi

Description

@kitchoi

To reproduce:


class Foo(HasTraits):

    true_or_false = Int()

    def default_traits_view(self):
        return View(
            Item(
                "true_or_false",
                editor=EnumEditor(
                    values=[0, 1],
                    format_func=lambda v: str(bool(v)).upper(),
                )
            )
        )

foo = Foo()
foo.configure_traits()

One expects to see the "TRUE" and "FALSE" in the dropdown. However one gets "0" and "1" in the view, which is unexpected.
Screenshot 2020-04-14 at 17 10 41

Printing the mapping:


ui = foo.edit_traits()
editor, = ui.get_editors("true_or_false")
print(editor.mapping)
ui.dispose()

We get:

{'0': 0, '1': 1}

If I create the editor first and then mutates the values, then I get the expected behaviour:

    def default_traits_view(self):
        editor = EnumEditor(
            format_func=lambda v: str(bool(v)).upper(),
        )
        # trigger _values_changed to fire
        editor.values = [0, 1] 
        return View(
            Item(
                "true_or_false",
                editor=editor,
            )
        )

Screenshot 2020-04-14 at 17 14 13

Metadata

Metadata

Assignees

Labels

ETS BacklogGood issue for ETS team members to look at

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions