question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autoformat files on save when running them causes lines addition to code

See original GitHub issue

Editor 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

  1. 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])

  1. Press F-5 -> the script is saved, formatted and executed
  2. 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:closed
  • Created 2 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ahurlbattcommented, Aug 31, 2021

@ccordoba12 You’re right. Apologies!

1reaction
ahurlbattcommented, Aug 30, 2021

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:

  1. Create a python file with anything in it, e.g.
if __name__ == "__main__":
    print("Hello World")
  1. Save the file locally e.g. C:\Users\<user>\Desktop
  2. In Preferences --> Code style and formatting, change the code formatting provider to black.
  3. Run black on the file via the drop-down menu or CTRL+ALT+I.
  4. Observe file contents as:
if __name__ == "__main__":
    print("Hello World")
)

The amount of copied text seems to depend on the size of the file. e.g.:

def useless_function():
    pass


if __name__ == "__main__":
    print("Hello World")

becomes:

def useless_function():
    pass


if __name__ == "__main__":
    print("Hello World")
rld")

This content recreation doesn’t occur when I use black through a console (i.e. not using Spyder) or if I use autopep8 instead of black as the formatting provider.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Files not autoformatted on save when other options that work ...
If I run the code and it has not been saved, Spyder reformats it using black, saves the reformatted file, and then runs...
Read more >
VScode automatically adding new lines on save
I got here searching as to why vs code was splitting up my lines nonsensically. There's Html>Format>Wrap Line Length which causes it to...
Read more >
How to Format Code on Save in VS Code with ESlint
Configuring VS Code to auto-format code on save. ... your package.json scripts to include a script to lint files via the command line....
Read more >
How to enable auto format on save with prettier in VS Code ...
Explainer video about How to enable auto format on save in VS Code (Visual Studio Code ) editor with prettier code formatter extension...
Read more >
Prettier + Format On Save = Never worry about ... - Scott Sauber
Format on Save in VS Code. To enable Format on Save in VS Code: File; Preferences; Settings; Search for Format On Save and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found