enthought / enthought/traitsui

Delegated Enum with dynamic values fails to find values trait

Open
#189 1 comment 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
306
Forks
99
PR merge metrics
No merged PRs in 30d

Description

A `HasTraits` class with a dynamic `Enum` trait and an associated trait for the values confuses traitsui when it is viewed as part of a second `HasTraits` class that uses `DelegatesTo`.

``` python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import traits.api as ta
import traitsui.api as tu

class F(ta.HasTraits):
x = ta.Enum(values='_x_values')
_x_values = ta.Property
def _get__x_values(self):
return [1, 2, 3]

class G(ta.HasTraits):
x = ta.DelegatesTo('f')
f = ta.Instance(F, args=())
def default_traits_view(self):
return tu.View(tu.Item('x')) # crash!

g = G()
g.configure_traits()
```

Paraphrased analysis by @corranwebster follows:

This is an issue with the default editor on the Enum trait (and presumably all traits): https://github.com/enthought/traits/blob/5138c91af68eb05ed2916da17b615bc0e77b8b37/traits/trait_types.py#L1954

For better or worse, it doesn’t have the context of the View (or even the name of the current object in that context) so it has no way to build the correct name. And in the case of a DelegatesTo trait, that information may be even more obscure.

One potential to fix might be to track down the calls to traits `get_editor()` (eg. https://github.com/enthought/traitsui/blob/9282955313d303c4f1d23deb95c17aad06156b81/traitsui/qt4/ui_panel.py#L809) and see if there is a name that could be passed in as an additional argument.

This might make some other calls harder, however (eg. calls made by the `ListEdtior`).

There are a couple of work-arounds for this bug: either delegate `_x_values` as well, or create an `EnumEditor` which explicitly links to the right thing:

``` python
class G(ta.HasTraits):
x = ta.DelegatesTo('f')
f = ta.Instance(F, args=())
def default_traits_view(self):
return tu.View(tu.Item('x', editor=tu.EnumEditor(name='object.f._x_values')))
```

This feels pretty similar to #184.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.