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

Convert on_trait_change decorators to observe decorators #864

Merged
merged 32 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
28f954b
update otc decorators to be observe decorators in guiapplication.py a…
aaronayres35 Jan 22, 2021
8d69ddc
use observe decorator in palce of otc decorator in action/action_item.py
aaronayres35 Jan 22, 2021
0fdd93d
same for tasks/task_window.py
aaronayres35 Jan 22, 2021
c086a34
fix update to task window (needed a : not a .)
aaronayres35 Jan 22, 2021
9089d33
remove specification of identity comparison_mode as it is no longer n…
aaronayres35 Jan 22, 2021
cd3f943
decorator updates for tasks/action/dock_pane_toggle_group.py
aaronayres35 Jan 22, 2021
32d27b6
update decorators in tasks/action/task_toggle_group.py (seems unteste…
aaronayres35 Jan 22, 2021
479fc21
same for task_window_toggle_group.py (also seems unttested? be careful)
aaronayres35 Jan 22, 2021
a43f506
replace decorator in test_gui_application.py
aaronayres35 Jan 22, 2021
2f8f535
replace decorators in application_window.py
aaronayres35 Jan 22, 2021
60ed83c
update decorrators for ui/qt4/tasks/advanced_editor_area_pane.py (not…
aaronayres35 Jan 22, 2021
2bf7b38
update decorators for ui/qt4/tasks/dock_pane.py (note handlers were c…
aaronayres35 Jan 22, 2021
84a401e
update decorators for both toolkits editor_area_pane.py files
aaronayres35 Jan 22, 2021
00fda10
same for ui/qt4/tasks/split_editor_area_pane.py
aaronayres35 Jan 22, 2021
5ac9cef
more of the same this time for wx dock_pane.py
aaronayres35 Jan 22, 2021
56ecaf3
update decorators in qt workbench_window_layout.py and update corresp…
aaronayres35 Jan 22, 2021
3c7a809
update wx application_window.py
aaronayres35 Jan 22, 2021
7f0b8ec
update workbench_window.py
aaronayres35 Jan 22, 2021
664b17a
update decorators for workbench/action/perspective_menu_manager.py
aaronayres35 Jan 22, 2021
590d911
update decorators for workbench/action/set_active_perspective_action.py
aaronayres35 Jan 22, 2021
e6d91c4
update for workbench/action/user_perspective_action.py
aaronayres35 Jan 22, 2021
df03601
update for workbench/action/view_menu_manager.py
aaronayres35 Jan 22, 2021
604368e
update for workbench/debug/debug_view.py
aaronayres35 Jan 22, 2021
7b967f0
update to use observe decorators in examples
aaronayres35 Jan 22, 2021
a8e1066
if event isn't actually needed, define the argument as _=None instead
aaronayres35 Jan 25, 2021
ba0722a
Apply suggestions from code review
aaronayres35 Jan 25, 2021
b42fd79
Merge branch 'convert-otc-decorators-to-observe' of https://github.co…
aaronayres35 Jan 25, 2021
b9cd5c2
more suggested changes for code readability
aaronayres35 Jan 25, 2021
8f9c2b3
Apply suggestions from code review
aaronayres35 Jan 28, 2021
729be50
Apply suggestions from code review
aaronayres35 Feb 1, 2021
e7c6ea4
pass event=None where needed
aaronayres35 Feb 1, 2021
083a265
missed a few
aaronayres35 Feb 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions examples/application/python_editor/python_editor_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
Property,
Str,
cached_property,
on_trait_change,
observe,
)

from python_browser_pane import PythonBrowserPane
Expand Down Expand Up @@ -333,15 +333,16 @@ def _prompt_for_save(self):

# Trait change handlers --------------------------------------------------

@on_trait_change("window:closing")
@observe("window:closing")
def _prompt_on_close(self, event):
""" Prompt the user to save when exiting.
"""
close = self._prompt_for_save()
event.veto = not close
window = event.new
window.veto = not close

@on_trait_change("active_editor.name")
def _change_title(self):
@observe("active_editor.name")
def _change_title(self, event):
""" Update the window title when the active editor changes.
"""
if self.window.active_task == self:
Expand All @@ -350,8 +351,8 @@ def _change_title(self):
else:
self.window.title = self.name

@on_trait_change("active_editor.[line,column,selection_length]")
def _update_status(self):
@observe("active_editor.[line,column,selection_length]")
def _update_status(self, event):
if self.active_editor is not None:
editor = self.active_editor
if editor.selection_length:
Expand Down
7 changes: 4 additions & 3 deletions examples/tasks/advanced/example_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
OK,
CANCEL,
)
from traits.api import on_trait_change, Property, Instance
from traits.api import observe, Property, Instance


from example_panes import PythonScriptBrowserPane
Expand Down Expand Up @@ -185,12 +185,13 @@ def _prompt_for_save(self):

# Trait change handlers ------------------------------------------------

@on_trait_change("window:closing")
@observe("window:closing")
def _prompt_on_close(self, event):
""" Prompt the user to save when exiting.
"""
close = self._prompt_for_save()
event.veto = not close
window = event.new
window.veto = not close

# Trait property getter/setters ----------------------------------------

Expand Down
7 changes: 4 additions & 3 deletions examples/tasks/basic/example_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
OK,
CANCEL,
)
from traits.api import on_trait_change
from traits.api import observe


from example_panes import PythonEditorPane, PythonScriptBrowserPane
Expand Down Expand Up @@ -148,9 +148,10 @@ def _prompt_for_save(self):
return self._prompt_for_save()
return True

@on_trait_change("window:closing")
@observe("window:closing")
def _prompt_on_close(self, event):
""" Prompt the user to save when exiting.
"""
window = event.new
if not self._prompt_for_save():
event.veto = True
window.veto = True
10 changes: 5 additions & 5 deletions pyface/action/action_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
""" An action manager item that represents an actual action. """


from traits.api import Any, Instance, List, Property, Str, on_trait_change
from traits.api import Any, Instance, List, Property, Str, observe


from pyface.action.action import Action
Expand Down Expand Up @@ -72,11 +72,11 @@ def _enabled_changed(self, trait_name, old, new):
def _visible_changed(self, trait_name, old, new):
self.action.visible = new

@on_trait_change("_wrappers.control")
def _on_destroy(self, object, name, old, new):
@observe("_wrappers:items:control")
def _on_destroy(self, event):
""" Handle the destruction of the wrapper. """
if name == "control" and new is None:
self._wrappers.remove(object)
if event.new is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a decent number of cases like this, where previously we used . not : but then only do something when the second trait is the one changed?

With these types of changes the tests were all still passing and I believe behavior is preserved (code is cleaner IMO as well), but if we want to play it safe or if a reviewer sees why these might not be equivalent, I am happy to adjust

self._wrappers.remove(event.object)

# ------------------------------------------------------------------------
# 'ActionItem' interface.
Expand Down
13 changes: 3 additions & 10 deletions pyface/data_view/data_models/row_table_data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
from collections.abc import Sequence

from traits.api import ComparisonMode, Instance, List, observe
from traits.api import Instance, List, observe
from traits.observation.api import trait

from pyface.data_view.abstract_data_model import (
Expand All @@ -35,20 +35,13 @@ class RowTableDataModel(AbstractDataModel):
"""

#: A sequence of objects to display as rows.
data = Instance(
Sequence,
comparison_mode=ComparisonMode.identity,
allow_none=False,
)
data = Instance(Sequence, allow_none=False)

#: An object which describes how to map data for the row headers.
row_header_data = Instance(AbstractDataAccessor, allow_none=False)

#: An object which describes how to map data for each column.
column_data = List(
Instance(AbstractDataAccessor, allow_none=False),
comparison_mode=ComparisonMode.identity,
)
column_data = List(Instance(AbstractDataAccessor, allow_none=False))

#: The index manager that helps convert toolkit indices to data view
#: indices.
Expand Down
14 changes: 4 additions & 10 deletions pyface/data_view/i_data_view_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging

from traits.api import (
Bool, ComparisonMode, Enum, HasStrictTraits, Instance, List, Property,
Bool, Enum, HasStrictTraits, Instance, List, Property,
TraitError, Tuple, cached_property,
)

Expand Down Expand Up @@ -53,10 +53,7 @@ class IDataViewWidget(IWidget):
selection = List(Tuple)

#: Exporters available for the DataViewWidget.
exporters = List(
Instance(AbstractDataExporter),
comparison_mode=ComparisonMode.identity,
)
exporters = List(Instance(AbstractDataExporter))


class MDataViewWidget(HasStrictTraits):
Expand All @@ -81,10 +78,7 @@ class MDataViewWidget(HasStrictTraits):
selection = Property(depends_on='_selection[]')

#: Exporters available for the DataViewWidget.
exporters = List(
Instance(AbstractDataExporter),
comparison_mode=ComparisonMode.identity,
)
exporters = List(Instance(AbstractDataExporter))

# Private traits --------------------------------------------------------

Expand All @@ -93,7 +87,7 @@ class MDataViewWidget(HasStrictTraits):

#: The selected indices in the view. This should never be mutated, any
#: changes should be by replacement of the entire list.
_selection = List(Tuple, comparison_mode=ComparisonMode.identity)
_selection = List(Tuple)

# ------------------------------------------------------------------------
# MDataViewWidget Interface
Expand Down
22 changes: 11 additions & 11 deletions pyface/gui_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Tuple,
Undefined,
Vetoable,
on_trait_change,
observe,
)

from .application import Application
Expand Down Expand Up @@ -280,22 +280,22 @@ def _about_dialog_default(self):

# Trait listeners --------------------------------------------------------

@on_trait_change("windows:activated")
def _on_activate_window(self, window, trait, old, new):
@observe("windows:items:activated")
def _on_activate_window(self, event):
""" Listener that tracks currently active window.
"""
if window in self.windows:
self.active_window = window
if event.object in self.windows:
self.active_window = event.object

@on_trait_change("windows:deactivated")
def _on_deactivate_window(self, window, trait, old, new):
@observe("windows:items:deactivated")
def _on_deactivate_window(self, event):
""" Listener that tracks currently active window.
"""
self.active_window = None

@on_trait_change("windows:closed")
def _on_window_closed(self, window, trait, old, new):
@observe("windows:items:closed")
def _on_window_closed(self, event):
""" Listener that ensures window handles are released when closed.
"""
if window in self.windows:
self.windows.remove(window)
if event.object in self.windows:
self.windows.remove(event.object)
14 changes: 7 additions & 7 deletions pyface/tasks/action/dock_pane_toggle_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cached_property,
Instance,
List,
on_trait_change,
observe,
Property,
Str,
)
Expand Down Expand Up @@ -66,13 +66,13 @@ def _get_name(self):
def _get_tooltip(self):
return "Toggles the visibility of the %s pane." % self.name

@on_trait_change("dock_pane.visible")
def _update_checked(self):
@observe("dock_pane.visible")
def _update_checked(self, event):
if self.dock_pane:
self.checked = self.dock_pane.visible

@on_trait_change("dock_pane.closable")
def _update_visible(self):
@observe("dock_pane.closable")
def _update_visible(self, event):
if self.dock_pane:
self.visible = self.dock_pane.closable

Expand Down Expand Up @@ -119,8 +119,8 @@ def get_manager(self):

# Private interface ----------------------------------------------------

@on_trait_change("dock_panes[]")
def _dock_panes_updated(self):
@observe("dock_panes.items")
def _dock_panes_updated(self, event):
"""Recreate the group items when dock panes have been added/removed.
"""

Expand Down
6 changes: 3 additions & 3 deletions pyface/tasks/action/task_toggle_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


from pyface.action.api import Action, ActionItem, Group
from traits.api import Any, List, Instance, Property, Str, on_trait_change
from traits.api import Any, List, Instance, Property, Str, observe


from pyface.tasks.task import Task
Expand Down Expand Up @@ -65,8 +65,8 @@ def _get_name(self):
def _get_tooltip(self):
return "Switch to the %s task." % self.name

@on_trait_change("task.window.active_task")
def _update_checked(self):
@observe("task.window.active_task")
def _update_checked(self, event):
if self.task:
window = self.task.window
self.checked = (
Expand Down
10 changes: 5 additions & 5 deletions pyface/tasks/action/task_window_toggle_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


from pyface.action.api import Action, ActionItem, Group
from traits.api import Any, Instance, List, Property, Str, on_trait_change
from traits.api import Any, Instance, List, Property, Str, observe


class TaskWindowToggleAction(Action):
Expand Down Expand Up @@ -47,12 +47,12 @@ def _get_name(self):
return self.window.title
return ""

@on_trait_change("window:activated")
def _window_activated(self):
@observe("window:activated")
def _window_activated(self, event):
self.checked = True

@on_trait_change("window:deactivated")
def _window_deactivated(self):
@observe("window:deactivated")
def _window_deactivated(self, event):
self.checked = False


Expand Down
14 changes: 7 additions & 7 deletions pyface/tasks/task_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Property,
Str,
Vetoable,
on_trait_change,
observe,
)


Expand Down Expand Up @@ -431,13 +431,13 @@ def __active_state_changed(self, state):
self.status_bar_manager = state.status_bar_manager
self.tool_bar_managers = state.tool_bar_managers

@on_trait_change("central_pane.has_focus, dock_panes.has_focus")
def _focus_updated(self, obj, name, old, new):
if name == "has_focus" and new:
self.active_pane = obj
@observe("central_pane:has_focus, dock_panes:items:has_focus")
def _focus_updated(self, event):
if event.new:
self.active_pane = event.object

@on_trait_change("_states[]")
def _states_updated(self):
@observe("_states.items")
def _states_updated(self, event):
self.tasks = [state.task for state in self._states]


Expand Down
11 changes: 5 additions & 6 deletions pyface/tasks/tasks_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Property,
Str,
cached_property,
on_trait_change,
observe,
)

from pyface.gui_application import GUIApplication
Expand Down Expand Up @@ -172,15 +172,14 @@ def _get_task_factory(self, id):

# Destruction utilities ---------------------------------------------------

@on_trait_change("windows:closed")
def _on_window_closed(self, window, trait, old, new):
@observe("windows:items:closed")
def _on_window_closed(self, event):
""" Listener that ensures window handles are released when closed.
"""
window = event.object
if getattr(window, "active_task", None) in self.tasks:
self.tasks.remove(window.active_task)
super(TasksApplication, self)._on_window_closed(
window, trait, old, new
)
super(TasksApplication, self)._on_window_closed(event)

# Trait initializers and property getters ---------------------------------

Expand Down
6 changes: 3 additions & 3 deletions pyface/tests/test_gui_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tempfile import mkdtemp
import unittest

from traits.api import Bool, on_trait_change
from traits.api import Bool, observe

from ..application_window import ApplicationWindow
from ..gui_application import GUIApplication
Expand Down Expand Up @@ -104,10 +104,10 @@ def _prepare_exit(self):
if self.exit_prepared_error:
raise Exception("Exit preparation failed")

@on_trait_change("windows:opening")
@observe("windows:items:opening")
def _on_activate_window(self, event):
if self.veto_open_window:
event.veto = self.veto_open_window
event.new.veto = self.veto_open_window


@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
Expand Down
Loading