Autoformat files on save when running them causes lines addition to code
See original GitHub issueEditor replicates lines of code at end of script
I have already written about this on stackoverflow (https://stackoverflow.com/questions/68472061/spyder-suddenly-copies-code-to-end-of-file), but at the time I could not reproduce the bug. Here is what I know today with an example to reproduce everything.
With certain script constellations, the last couple of lines of a script are replicated at the end of the script without user intervention when pressing F-5 (Run script). This can lead to a lot of time lost, e.g. when the main function is replicated at the end of a script.
Steps to Reproduce the Problem
- Copy the following script to the spyder editor. Note that the code is not yet formatted and there are a couple of long lines towards then end.
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 25 15:19:45 2021
@author: tobia
"""
import statistics
from edatest import Edatest
def get_quartiles(l, method):
l = sorted(l)
q0, q4 = min(l), max(l)
q2 = statistics.median(l)
if method == "MS":
p1 = int((len(l) + 1) / 4 + 0.5)
q1 = l[p1 - 1]
p1 = int(3 * (len(l) + 1) / 4) - 1
q3 = l[p1]
else:
len_odd = len(l) % 2 == 1
mid_point = len(l) // 2
left = l[0:mid_point]
if len_odd:
right = l[mid_point + 1 :]
else:
right = l[mid_point:]
if (method == "T") and (len(l) % 2 == 1): # odd number of elements
left.append(q2)
right = [q2] + right
# print(f"left: {left}, right: {right}")
q1 = statistics.median(left)
q3 = statistics.median(right)
return [q0, q1, q2, q3, q4]
if __name__ == "__main__":
Test = Edatest()
Test.assert_equals(get_quartiles([4, 1, 7, 8, 3, 6, 5, 2], "T"), [1, 2.5, 4.5, 6.5, 8])
Test.assert_equals(get_quartiles([4, 1, 7, 8, 3, 6, 5, 2], "MS"), [1, 2, 4.5, 7, 8])
Test.assert_equals(get_quartiles([2, 6, 1, 8, 5, 9, 7, 4, 3], "MM"), [1, 2.5, 5, 7.5, 9])
Test.assert_equals(
get_quartiles([2, 6, 1, 8, 5, 9, 7, 4, 3], "MS"), [1, 3, 5, 7, 9]
)
Test.assert_equals(get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "T"), [1, 3, 5.5, 8, 10])
Test.assert_equals(get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "MM"), [1, 3, 5.5, 8, 10])
Test.assert_equals(get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "MS"), [1, 3, 5.5, 8, 10])
Test.assert_equals(get_quartiles([3, 9, 11, 2, 4, 1, 8, 6, 10, 5, 7], "T"), [1, 3.5, 6, 8.5, 11])
Test.assert_equals(get_quartiles([3, 9, 11, 2, 4, 1, 8, 6, 10, 5, 7], "MM"), [1, 3, 6, 9, 11])
- Press F-5 -> the script is saved, formatted and executed
- Press F-5 again: the script is again executed and terminates with an error message
File "C:\Users\tobia\OneDrive\Documents\Python Scripts\edabit\quartiles\quartiles.py", line 63
)
^
SyntaxError: unmatched ')'
because someone added a couple of lines to the end of the script. In this case it is the following lines:
get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "T"), [1, 3, 5.5, 8, 10]
)
Test.assert_equals(
get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "MM"), [1, 3, 5.5, 8, 10]
)
Test.assert_equals(
get_quartiles([10, 3, 1, 8, 6, 4, 7, 5, 2, 9], "MS"), [1, 3, 5.5, 8, 10]
)
Test.assert_equals(
get_quartiles([3, 9, 11, 2, 4, 1, 8, 6, 10, 5, 7], "T"), [1, 3.5, 6, 8.5, 11]
)
Test.assert_equals(
get_quartiles([3, 9, 11, 2, 4, 1, 8, 6, 10, 5, 7], "MM"), [1, 3, 6, 9, 11]
)
which are the ~15 last lines of the script, which were just copied to the end. 4. You can go back by pressing Ctrl-Z twice, and try again from there.
It is interesting to note that one can observe this behaviour in real-time when watching the scroll bar at the right side of the window. Its size changes twice, presumably the first time when the code is reformatted, and then again, when the last couple of lines are replicated.
Versions
Spyder version: 5.0.5 Python 3.8.10 64-bit | Qt 5.9.7 | PyQt5 5.9.2 | Windows 10
I have observed this behaviour also in previous versions of spyder, but could only reproduce it now.
Dependencies
For the script above to run edatest needs to be installed (https://pypi.org/project/edatest/), however this is not central to the bug, only required to run the script.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
@ccordoba12 You’re right. Apologies!
Hi all. I’m having the same problem, but I’m not using OneDrive, and it’s specifically when I format using Black.
Steps to recreate:
black
.black
on the file via the drop-down menu orCTRL
+ALT
+I
.The amount of copied text seems to depend on the size of the file. e.g.:
becomes:
This content recreation doesn’t occur when I use
black
through a console (i.e. not using Spyder) or if I useautopep8
instead ofblack
as the formatting provider.