Repository navigation
Description
This is a boiled-down version of a test interaction seen on a real project using TraitsUI and the UITester.
The test class has three tests: test1, test2 and test3, with test3 identical to test1. When run on my machine, the tests run in the order test1, test2, test3. But test1 passes, while test3 logs an exception (though it also passes), so either there's some source of non-determinism in test1 / test3, or something about test2 is altering global state.
Most of the ingredients here seem to be essential to reproducing the interaction, including the UITester, the FileEditor, the GuiTestAssistant and the ProgressEditor.
Here's the script:
import unittest
from pathlib import Path
from pyface.ui.qt.util.gui_test_assistant import GuiTestAssistant
from traits.api import Directory, Enum, HasStrictTraits, Instance, Int, observe
from traits.observation.events import TraitChangeEvent
from traitsui.api import ProgressEditor, UItem, View
from traitsui.testing.api import UITester
class ProgressBar(HasStrictTraits):
value = Int()
traits_view = View(UItem("value", editor=ProgressEditor()))
class Model1(HasStrictTraits):
directory = Directory()
model_directory = Instance(Path)
state = Enum(["Idle", "Refreshing"])
@observe("model_directory")
def _refresh(self, event: TraitChangeEvent) -> None:
if self.state != "Idle":
raise RuntimeError("Cannot refresh when not in idle state.")
self.state = "Refreshing"
@observe("directory")
def _update_model_directory(self, event: TraitChangeEvent) -> None:
self.model_directory = Path(self.directory)
traits_view = View(UItem("directory", enabled_when=f"state == 'Idle'"))
class Model2(HasStrictTraits):
progress_bar = Instance(ProgressBar, ())
traits_view = View(UItem("progress_bar", style="custom"))
class TestModels(GuiTestAssistant, unittest.TestCase):
def test1(self) -> None:
view = Model1()
with UITester(auto_process_events=False).create_ui(view):
view.directory = Path.cwd()
def test2(self) -> None:
view = Model2()
with UITester(auto_process_events=False).create_ui(view):
pass
def test3(self) -> None:
view = Model1()
with UITester(auto_process_events=False).create_ui(view):
view.directory = Path.cwd()Here's the output on my machine (macOS/M3, EDM environment, Python 3.11.14, TraitsUI 8.0.0-3, traits 7.1.0-1, pyside6 6.10.0-2)
~/Desktop % edm run -e testenv -- python -m unittest -v weird_test.py
test1 (weird_test.TestModels.test1) ... ok
test2 (weird_test.TestModels.test2) ... ok
test3 (weird_test.TestModels.test3) ... Exception occurred in traits notification handler for event object: TraitChangeEvent(object=<weird_test.Model1 object at 0x114ef4810>, name='model_directory', old=PosixPath('/Users/mdickinson/Desktop'), new=PosixPath('.'))
Traceback (most recent call last):
File "/Users/mdickinson/.edm/envs/testenv/lib/python3.11/site-packages/traits/observation/_trait_event_notifier.py", line 122, in __call__
self.dispatcher(handler, event)
File "/Users/mdickinson/.edm/envs/testenv/lib/python3.11/site-packages/traits/observation/observe.py", line 43, in dispatch_same
handler(event)
File "/Users/mdickinson/Desktop/weird_test.py", line 24, in _refresh
raise RuntimeError("Cannot refresh when not in idle state.")
RuntimeError: Cannot refresh when not in idle state.
ok
----------------------------------------------------------------------
Ran 3 tests in 0.377s
OK
Activity
mdickinson commentedon Mar 18, 2026
After running into this in a few more places, I suspect this of being primarily a bug in the
FileEditor.mdickinson commentedon Mar 19, 2026
The following text was AI-generated (OpenCode / Claude Opus 4.6), and was the result of me asking for an investigation into the issue.
Root Cause Analysis
After tracing through the code, here's what I believe is happening.
Setup
The model has:
directory = Directory()— the user-facing traitmodel_directory = Directory()— internal copystate = Str("Idle")@observe("directory")→_update_model_directory: setsmodel_directory = Path(directory)@observe("model_directory")→_refresh: checksstate == "Idle", setsstate = "Refreshing", does work, setsstate = "Idle"The View has
enabled_when="state == 'Idle'"on the directoryItem. TheDirectoryEditor(aFileEditorsubclass) hasauto_set = False(seetraitsui/editors/file_editor.py:50), so the QtSimpleEditorconnectsQLineEdit.editingFinishedtoupdate_object(seetraitsui/qt/file_editor.py:71).Handler registration order
This is the crux of the problem. When the UI is created, the various notification handlers end up registered in a specific order on the model's
directorytrait's notifier list:@observe("directory")→_update_model_directory— registered during the model's__init__(by_init_trait_observersinctraits.c:741), which appends aTraitEventNotifierto the trait's notifier list._update_editor— registered later, duringEditor.prepare()via a dynamicon_trait_changecall (traitsui/editor.py:248-249). Since the model object already exists, this handler is appended after the@observehandler.For the
statetrait,_evaluate_whenis registered as an anytrait handler viaobject.on_trait_change(self._evaluate_when, dispatch="ui")attraitsui/ui.py:440. Anytrait handlers go into the object-level notifier list (onotifiers), which fires after all trait-level notifiers (tnotifiers). On the main thread,dispatch="ui"is synchronous.The notification chain when
directory = Path.cwd()is setHere's the exact sequence of events in
test3:Step 1:
view.directory = Path.cwd()is called. The value is written to the object's__dict__. Then notifiers fire in list order.Step 2: First notifier:
@observe("directory")→_update_model_directoryfires. This setsmodel_directory = Path(cwd).Step 3: Inside step 2,
model_directorychanges. Its notifiers fire:@observe("model_directory")→_refreshfires. This setsstate = "Refreshing".Step 4: Inside step 3,
statechanges. Its notifiers fire:onotifier_evaluate_whenfires (registered as an anytrait handler). Since we're on the main thread,dispatch="ui"is synchronous.Step 5:
_evaluate_when→_do_evaluate_when→_evaluate_conditionevaluatesenabled_when="state == 'Idle'". Sincestate == "Refreshing", this isFalse. The editor'senabledwasTrue, so it setseditor.enabled = False.Step 6:
editor.enabled = Falsetriggers_enabled_changed(traitsui/qt/editor.py:83) →_enabled_changed_helper→self.control.setEnabled(False). The control is the parentQWidget(traitsui/qt/file_editor.py:56). Disabling the parent propagates to the childQLineEdit.Step 7: If the QLineEdit has focus, disabling it causes it to lose focus, which triggers the
editingFinishedsignal.Step 8:
editingFinished→update_object()(traitsui/qt/file_editor.py:96-106). This readsself._file_name.text(). But_update_editor(the handler that updates the QLineEdit text to reflect the newdirectoryvalue) has not yet been called — it's later in the notifier list (registered after@observe). The QLineEdit still shows its old/stale text (empty string""for a fresh editor).Step 9:
update_objectsetsself.value = file_name(the stale empty string). This property setter sets the model trait, so effectivelydirectory = "".Step 10: Traits allows re-entrant trait sets — there are no re-entrancy guards (confirmed in
ctraits.c'ssetattr_trait, which writes the value and callscall_notifierswithout any depth check). So a new notification chain fires depth-first:@observe("directory")→_update_model_directoryfires again → setsmodel_directory = Path("") = PosixPath(".")@observe("model_directory")→_refreshfires, butstateis still"Refreshing"(from step 3, which hasn't finished yet) → RuntimeError:_refreshcalled whenstate != 'Idle'Why test1 passes but test3 fails
The entire mechanism depends on whether the QLineEdit has focus at the moment
setEnabled(False)is called (step 7).editingFinishedis only emitted when the QLineEdit loses focus, not on disable per se — but disabling a focused widget causes focus loss.test1: The UI was just created. The QLineEdit does not have focus (the dialog is freshly shown, and Qt hasn't assigned focus to the line edit).
setEnabled(False)does not cause focus loss, soeditingFinisheddoesn't fire. The chain completes cleanly.test2: Creates and destroys a
ProgressEditor. TheProgressDialog.update()callsQApplication.processEvents()(pyface/ui/qt/progress_dialog.py:165).GuiTestAssistant.tearDown()also processes the event loop. These operations process pending events and alter global window/focus state.test3: Identical to test1, but the global Qt focus state left behind by test2's window lifecycle means the QLineEdit receives focus when the dialog is shown. Now when
setEnabled(False)is called during the notification chain, the QLineEdit loses focus →editingFinishedfires → the bug manifests.Summary
The root cause is Qt widget side-effects (signal emission from focus changes) firing during a Traits notification chain, causing re-entrant trait modifications with stale data:
@observe("directory")fires before_update_editor(which would update the QLineEdit text), because it was registered first.@observechain,enabled_whenevaluation synchronously disables the QLineEdit.editingFinished, which reads stale text from the QLineEdit and writes it back to the model._refreshwhilestateis already"Refreshing"→ RuntimeError.mdickinson commentedon Mar 19, 2026
For clarity, the analysis in the comment above was AI-generated: it came from OpenCode / Claude Opus 4.6. I (actual me, Mark) haven't yet looked over it for plausibility.