Repository: spyder-ide/spyder

Issue:
KeyError when removing autosave files
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

File was changed outside of Spyder, need to reload

### Traceback
```python-traceback
Traceback (most recent call last):
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/plugin.py", line 2773, in run_file
    self.re_run_file(save_new_files=False)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/plugin.py", line 2797, in re_run_file
    all_saved = self.save_all(save_new_files=save_new_files)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/plugin.py", line 2318, in save_all
    self.get_current_editorstack().save_all(save_new_files=save_new_files)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/widgets/editor.py", line 2082, in save_all
    all_saved &= self.save(index, save_new_files=save_new_files)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/widgets/editor.py", line 1871, in save
    self._save_file(finfo, index)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/widgets/editor.py", line 1889, in _save_file
    self.autosave.remove_autosave_file(finfo.filename)
  File "/Applications/anaconda3/lib/python3.9/site-packages/spyder/plugins/editor/utils/autosave.py", line 318, in remove_autosave_file
    del self.file_hashes[autosave_filename]
KeyError: '/Users/maddiegreen/.spyder-py3/autosave/Lec2_cont.py'
```

## Versions

* Spyder version: 5.1.5 
* Python version: 3.9.12
* Qt version: 5.9.7
* PyQt5 version: 5.9.2
* Operating System: Darwin 21.5.0

### Dependencies

```

# Mandatory:
applaunchservices >=0.1.7     :  0.2.1 (OK)
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  8.2.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jsonschema >=3.2.0            :  4.4.0 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.4.4 (OK)
numpydoc >=0.6.0              :  1.2 (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.8.0 (OK)
pygments >=2.0                :  2.11.2 (OK)
pylint >=2.5.0;<2.10.0        :  2.9.6 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.2.2;<1.3.0          :  1.2.4 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.3 (OK)
qtconsole >=5.1.0             :  5.3.0 (OK)
qtpy >=1.5.0                  :  2.0.1 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  61.2.0 (OK)
sphinx >=0.6.6                :  4.4.0 (OK)
spyder_kernels >=2.1.1;<2.2.0 :  2.1.3 (OK)
textdistance >=4.2.0          :  4.2.1 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.28 (OK)
matplotlib >=2.0.0            :  3.5.1 (OK)
numpy >=1.7                   :  1.21.5 (OK)
pandas >=1.1.1                :  1.4.2 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.10.1 (OK)
```



Discussion:
Hey @madeleinegreen, thanks for reporting. We'll take care of this error in our next version (5.4.0), to be released in October. In the meantime, you can ignore it because it's harmless.

Resolving patch:
diff --git a/spyder/plugins/editor/utils/autosave.py b/spyder/plugins/editor/utils/autosave.py
--- a/spyder/plugins/editor/utils/autosave.py
+++ b/spyder/plugins/editor/utils/autosave.py
@@ -315,7 +315,15 @@ def remove_autosave_file(self, filename):
             msgbox = AutosaveErrorDialog(action, error)
             msgbox.exec_if_enabled()
         del self.name_mapping[filename]
-        del self.file_hashes[autosave_filename]
+
+        # This is necessary to catch an error when a file is changed externally
+        # but it's left unsaved in Spyder.
+        # Fixes spyder-ide/spyder#19283
+        try:
+            del self.file_hashes[autosave_filename]
+        except KeyError:
+            pass
+
         self.save_autosave_mapping()
         logger.debug('Removing autosave file %s', autosave_filename)
 


Test patch: