Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnumEditor does not pickup values from Enum trait as suggested in documentation #2037

Open
mgrady3 opened this issue Oct 24, 2023 · 1 comment

Comments

@mgrady3
Copy link
Contributor

mgrady3 commented Oct 24, 2023

The documentation for the EnumEditor states that the values/name kwargs are only required if the trait being edited is not "an enumeration". https://docs.enthought.com/traitsui/traitsui_user_manual/factories_basic.html#enumeditor

The way things are worded make it seem like you should not need to pass in values if the trait being edited is an enumeration, however that does not appear to work as intended. See example below:

from traitsui.api import EnumEditor, UItem, VGroup, View

VALUES = ("ABC", "123", "!@#")


class TestSelect(HasStrictTraits):
    selection = Enum(VALUES)

    def default_traits_view(self):
        return View(
            VGroup(
                UItem(
                    "selection",
                    style="custom",
                    editor=EnumEditor(
                        # Commenting out values= ... generates an error, Invalid value for values specified
                        values={val: f"{i}:{val}" for i, val in enumerate(VALUES)},
                        cols=1,
                    ),
                )
            ),
            buttons=["OK"],
            title="Make Selection",
        )


if __name__ == "__main__":
    s = TestSelect()
    s.configure_traits()
    print(f"Selected: {s.selection}")

In the above example the code works as intended when the value kwarg for EnumEditor is provided. However if you comment out or remove that line, then the code fails with the following message: traits.trait_errors.TraitError: Invalid value for 'values' specified.

Possibly related issue (though the error message is different): #726

While passing through values to the editor is not difficult, the way the docs are worded it sounds like you should not need to. Additionally, for simple cases such as above where the intent is simply to display a static list of pre-defined values, it results in unnecessary code duplication. The underlying Enum trait already knows about the set of values.

@mgrady3
Copy link
Contributor Author

mgrady3 commented Oct 24, 2023

Actually, on further inspection I think this is just a duplicate of #726.

The error message on that issue matches that shown on this issue if you re-run the code with current traits/traitsui versions. It must have been a different message in prior versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant