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

GroupEditor does not call super() #1673

Open
corranwebster opened this issue May 18, 2021 · 2 comments
Open

GroupEditor does not call super() #1673

corranwebster opened this issue May 18, 2021 · 2 comments

Comments

@corranwebster
Copy link
Contributor

The helper class GroupEditor does not call super() in its __init__() and so Traits will not properly initialize:

class GroupEditor(Editor):
""" A pseudo-editor that allows a group to be managed.
"""
def __init__(self, **traits):
""" Initialise the object.
"""
self.trait_set(**traits)

(similarly for wx).

This could be part of the problem for issues involving enabled_when, etc not applying to labels or other components (eg. #758) or updating properly (or not, but it definitely and anti-pattern for Traits to do this).

@aaronayres35
Copy link
Contributor

This is a bit awkward because super would end up calling:

traitsui/traitsui/editor.py

Lines 523 to 538 in 0c30b77

def __init__(self, parent, **traits):
""" Initializes the editor object.
"""
super().__init__(**traits)
try:
self.old_value = getattr(self.object, self.name)
except AttributeError:
ctrait = self.object.base_trait(self.name)
if ctrait.type == "event" or self.name == "spring":
# Getting the attribute will fail for 'Event' traits:
self.old_value = Undefined
else:
raise
# Synchronize the application invalid state status with the editor's:
self.sync_value(self.factory.invalid, "invalid", "from")

In this case, users of GroupEditor and its subclasses do not currently specify a parent. Further, looking at the body of the above method, the GroupEditor does not have a name / there is no specific trait the editor is editing, which will lead to a break in this call as self.name will be <undefined>. Perhaps we could directly call __init__ on the base class higher up the chain (in this case HasPrivateTraits).
ie. at the start of the GroupEditor.__init__ method call HasPrivateTraits.__init__(self, **traits) or super(Editor, self).__init__(**traits) but I feel like that smells a bit.
Also, as a side note, I am unsure why parent is a required positional arg in the above code as it is unused in the body of the method?

@corranwebster
Copy link
Contributor Author

Right - I think that what is needed is a superclass of Editor which both GroupEditor and Editor inherit from. I've been looking into it and I'm not quite sure which bits of Editor that GroupEditor is using, but I think it is at least the enable/disable and visibility stuff, but also possibly the control creation/deletion code. It doesn't need the code for synchronization of traits with the UI context.

As a more immediate work-around, it might make sense to have GroupEditor.__init__() do a direct HasTraits.__init__() call, which will at least make sure that the Traits machinery is all properly hooked up.

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

No branches or pull requests

3 participants