Repository: spyder-ide/spyder

Issue:
Unchecking Display variables and attributes in outline does not work
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ ] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

Unchecking the "Display variables and attributes" option in the outline pane does not work until Spyder is restarted.

### What steps reproduce the problem?

1. Check the "Display variables and attributes" option in the outline pane
2. Uncheck the "Display variables and attributes" option in the outline pane
3. The variables and attributes are still displayed in the outline pane

### What is the expected output? What do you see instead?

Unchecking the "Display variables and attributes" option should work as expected.

### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

No error/trace message.

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 5.4.5  (conda)
* Python version: 3.11.6 64-bit
* Qt version: 5.15.8
* PyQt5 version: 5.15.9
* Operating System: macOS-13.6-arm64-arm-64bit

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
# Mandatory:
applaunchservices >=0.3.0                                                   :  0.3.0 (OK)
atomicwrites >=1.2.0                                                        :  1.4.1 (OK)
chardet >=2.0.0                                                             :  5.2.0 (OK)
cloudpickle >=0.5.0                                                         :  3.0.0 (OK)
cookiecutter >=1.6.0                                                        :  2.4.0 (OK)
diff_match_patch >=20181111                                                 :  20230430 (OK)
intervaltree >=3.0.2                                                        :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0,!=8.11.0,!=8.12.0,!=8.12.1 :  8.16.1 (OK)
jedi >=0.17.2,<0.19.0                                                       :  0.18.2 (OK)
jellyfish >=0.7                                                             :  1.0.1 (OK)
jsonschema >=3.2.0                                                          :  4.19.1 (OK)
keyring >=17.0.0                                                            :  24.2.0 (OK)
nbconvert >=4.0                                                             :  7.9.2 (OK)
numpydoc >=0.6.0                                                            :  1.5.0 (OK)
parso >=0.7.0,<0.9.0                                                        :  0.8.3 (OK)
pexpect >=4.4.0                                                             :  4.8.0 (OK)
pickleshare >=0.4                                                           :  0.7.5 (OK)
psutil >=5.3                                                                :  5.9.5 (OK)
pygments >=2.0                                                              :  2.16.1 (OK)
pylint >=2.5.0,<3.0                                                         :  2.17.7 (OK)
pylint_venv >=3.0.2                                                         :  3.0.3 (OK)
pyls_spyder >=0.4.0                                                         :  0.4.0 (OK)
pylsp >=1.7.4,<1.8.0                                                        :  1.7.4 (OK)
pylsp_black >=1.2.0,<3.0.0                                                  :  1.3.0 (OK)
qdarkstyle >=3.0.2,<3.2.0                                                   :  3.1 (OK)
qstylizer >=0.2.2                                                           :  0.2.2 (OK)
qtawesome >=1.2.1                                                           :  1.2.3 (OK)
qtconsole >=5.4.2,<5.5.0                                                    :  5.4.4 (OK)
qtpy >=2.1.0                                                                :  2.4.1 (OK)
rtree >=0.9.7                                                               :  1.1.0 (OK)
setuptools >=49.6.0                                                         :  68.2.2 (OK)
sphinx >=0.6.6                                                              :  7.2.6 (OK)
spyder_kernels >=2.4.4,<2.5.0                                               :  2.4.4 (OK)
textdistance >=4.2.0                                                        :  4.5.0 (OK)
three_merge >=0.1.1                                                         :  0.1.1 (OK)
watchdog >=0.10.3                                                           :  3.0.0 (OK)
zmq >=22.1.0                                                                :  25.1.1 (OK)

# Optional:
cython >=0.21                                                               :  None (NOK)
matplotlib >=3.0.0                                                          :  None (NOK)
numpy >=1.7                                                                 :  1.26.0 (OK)
pandas >=1.1.1                                                              :  2.1.1 (OK)
scipy >=0.17.0                                                              :  None (NOK)
sympy >=0.7.3                                                               :  None (NOK)
```



Discussion:
Hi @remisalmon thank you for the feedback! I think I was able to reproduce this :+1: Seems like a workaround, for the moment, that could prevent having to restart Spyder is to edit the file in a way that causes the outline to be recalculated 🤔 :

![outline](https://github.com/spyder-ide/spyder/assets/16781833/b385ccdd-5aed-471f-b5d6-5decc9b220c9)

Should we try to address this for 5.5.0 or leave it for 6.x @ccordoba12 ?
Please give a try this week to see if we can include a fix in 5.5.

Resolving patch:
diff --git a/spyder/plugins/outlineexplorer/widgets.py b/spyder/plugins/outlineexplorer/widgets.py
--- a/spyder/plugins/outlineexplorer/widgets.py
+++ b/spyder/plugins/outlineexplorer/widgets.py
@@ -608,8 +608,7 @@ def update_tree(self, items, editor):
         if not must_update:
             # Compare with current tree to check if it's necessary to update
             # it.
-            changes = tree - current_tree
-            if tree and len(changes) == 0:
+            if tree == current_tree:
                 logger.debug(
                     f"Current and new trees for file {editor.fname} are the "
                     f"same, so no update is necessary"


Test patch:
diff --git a/spyder/plugins/outlineexplorer/tests/test_widgets.py b/spyder/plugins/outlineexplorer/tests/test_widgets.py
--- a/spyder/plugins/outlineexplorer/tests/test_widgets.py
+++ b/spyder/plugins/outlineexplorer/tests/test_widgets.py
@@ -217,6 +217,37 @@ def test_go_to_last_item(create_outlineexplorer, qtbot):
     assert outlineexplorer.treewidget.currentItem().text(0) == 'method1'
 
 
+@flaky(max_runs=10)
+def test_display_variables(create_outlineexplorer, qtbot):
+    """
+    Test that clicking on the 'Display variables and attributes' button located in the
+    toolbar of the outline explorer is working as expected by updating the tree widget.
+
+    Regression test for spyder-ide/spyder#21456.
+    """
+    outlineexplorer, _ = create_outlineexplorer('text')
+
+    editor = outlineexplorer.treewidget.current_editor
+    state = outlineexplorer.treewidget.display_variables
+
+    editor_id = editor.get_id()
+
+    initial_tree = outlineexplorer.treewidget.editor_tree_cache[editor_id]
+
+    outlineexplorer.treewidget.toggle_variables(not state)
+
+    first_toggle_tree = outlineexplorer.treewidget.editor_tree_cache[editor_id]
+
+    assert first_toggle_tree != initial_tree
+
+    outlineexplorer.treewidget.toggle_variables(state)
+
+    second_toggle_tree = outlineexplorer.treewidget.editor_tree_cache[editor_id]
+
+    assert (second_toggle_tree != first_toggle_tree) and (
+        second_toggle_tree == initial_tree)
+
+
 if __name__ == "__main__":
     import os
     pytest.main(['-x', os.path.basename(__file__), '-v', '-rw'])