Skip to content

Convert dynamic on_trait_change to observe part 2 #880

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

Merged
merged 30 commits into from
Feb 5, 2021

Conversation

aaronayres35
Copy link
Contributor

@aaronayres35 aaronayres35 commented Feb 4, 2021

closes #637
closes #732

This PR makes the last round of updates to replace use of on_trait_change with observe. With the changes in this PR, on_trait_change is no longer present anywhere in the code base. This PR also makes an update to the documentation section on timers. Please confirm the changes made there are reasonable.

Note to reviewer, be cautious: some of the code undergoing changes in this PR is not covered by the test suite. I made some knowingly broken changes and ran the test suite only to find everything still passing.

This PR also drive by removes some commented out code from the code base.

Comment on lines 352 to 357
smobs(self._on_model_content_changed, "content_changed", remove=True)
smobs(
self._on_model_structure_changed, "structure_changed", remove=True
)
smobs(self._on_row_sort, "row_sorted", remove=True)
smobs(self._on_column_sort, "column_sorted", remove=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

although these are observers on the model not the grid itself I assume they should still be removed when the grid is disposed (notably because the handlers are methods on a mid disposal grid object)

@@ -134,7 +134,7 @@ def _create_tree(self, parent):
content_provider=DefaultTreeContentProvider(),
)

tree_viewer.on_trait_change(self._on_selection_changed, "selection")
tree_viewer.observe(self._on_selection_changed, "selection")
Copy link
Contributor Author

@aaronayres35 aaronayres35 Feb 4, 2021

Choose a reason for hiding this comment

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

I can't comment on the line itself, but this file has from pyface.ui.wx.split_dialog import SplitDialog but there is no split_dialog module in pyface/ui/wx ...
And that imported SplitDialog is used as the base class for the PreferenceDialog class defined in this module.

@@ -136,7 +136,7 @@ def _qt4_add_tools(self, parent, tool_bar, controller):
# Is a separator required?
if previous_non_empty_group is not None and group.separator:
separator = tool_bar.addSeparator()
group.on_trait_change(
group.observe(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: I don't think these are getting removed

Comment on lines +131 to +134
if old:
old.observe(method, trait(name, optional=True), remove=True)
if new:
new.observe(method, trait(name, optional=True))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

commenting on this to draw attention to a reviewer. Previously the observer was never getting hooked up if name was None. This caused pyface/action/tests/test_listening_action/TestListeningAction/test_destroy to fail because we ultimately tried to unhook it in the destroy call.
The previous commit fixed this issue as well, but this seemed like the more correct way to go about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

i'm not entirely sure under what circumstances name is None will be True here. Ohh. Wait. Never mind. if name will be False if name is an empty string - which is possible.

@aaronayres35
Copy link
Contributor Author

ref: #876, #877
this PR did not make these fixes on the fly either and so when addressing those issues this PR should be used as a reference to ensure that all cases are addressed.

@aaronayres35 aaronayres35 changed the title [WIP] Convert dynamic on_trait_change to observe part 2 Convert dynamic on_trait_change to observe part 2 Feb 4, 2021
Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

Almost LGTM

if self.enabled_name:
self.object.observe(
self._enabled_update,
trait(self.enabled_name, optional=True),
Copy link
Contributor

Choose a reason for hiding this comment

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

i think this line warrants a comment - we already have a conditional so im not sure why we use the optional=True in the call to trait - and i'm not even sure what trait does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah i meant to delete the conditionals, they are no longer needed once we have the optional=True

if self.visible_name:
self.object.observe(
self._visible_update,
trait(self.visible_name, optional=True),
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above.

Comment on lines +131 to +134
if old:
old.observe(method, trait(name, optional=True), remove=True)
if new:
new.observe(method, trait(name, optional=True))
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm not entirely sure under what circumstances name is None will be True here. Ohh. Wait. Never mind. if name will be False if name is an empty string - which is possible.

Comment on lines -1391 to -1393
# because wx is retarded we have to check this as well -- why
# the blazes can't they come up with one Q#$%@$#% API to manage
# selections??? makes me want to put the smack on somebody.
Copy link
Contributor

Choose a reason for hiding this comment

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

why'd you remove this 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol 😆

Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

LGTM with a few more suggestions.

Co-authored-by: Poruri Sai Rahul <[email protected]>
@rahulporuri
Copy link
Contributor

Still LGTM. Merge once green!

@aaronayres35 aaronayres35 merged commit 05eefd4 into master Feb 5, 2021
@aaronayres35 aaronayres35 deleted the replace-dynamic-otc-observe2 branch February 5, 2021 13:38
self._visible_update, self.visible_name, remove=True
self.object.observe(
self._visible_update,
trait(self.visible_name, optional=True),
Copy link
Contributor

@kitchoi kitchoi Feb 10, 2021

Choose a reason for hiding this comment

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

trait here assumes the first argument is a trait name (see doc), so an extended name like "child.is_visible" is going to fail because "child.is_visible" as a trait name is not defined on object. The optional=True may mask that lost behaviour?

rahulporuri pushed a commit that referenced this pull request Mar 26, 2021
This was missed in the `on_trait_change` to `observe` changes
introduced in #880 . The methods were previously receiving
`obj` as an argument to the trait change handler which should
have been replaced with `event.object` in #880.
rahulporuri pushed a commit that referenced this pull request Mar 26, 2021
This was missed in the `on_trait_change` to `observe` changes
introduced in #880 . The methods were previously receiving
`obj` as an argument to the trait change handler which should
have been replaced with `event.object` in #880.
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

Successfully merging this pull request may close these issues.

Replace on_trait_change with observe Update examples to use traits observation framework
3 participants