Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatting more than expected #785

Open
anakinxc opened this issue Dec 17, 2024 · 1 comment
Open

Reformatting more than expected #785

anakinxc opened this issue Dec 17, 2024 · 1 comment

Comments

@anakinxc
Copy link

Describe the bug

Darker yields more reformat changes than expected. See repro steps for details.

To Reproduce
Steps to reproduce the behavior:

Let's have a code snippet with inconsistent format

def test_fun():
  with open("bla") as f:
    content = str("""
    whatever
    """)

    A = 1
    B = 2
    C = 3

  return

def main():
  print("hello")  
  test_fun()

if __name__ == "__main__":
    main()

Apply some changes

diff --git a/scripts/test.py b/scripts/test.py
index d50207d076..29c1af2247 100644
--- a/scripts/test.py
+++ b/scripts/test.py
@@ -2,7 +2,7 @@
 def test_fun():
   with open("bla") as f:
     content = str("""
-    whatever
+    whatever//
     """)

     A = 1

Run darker with

python -m darker --check --diff scripts/test.py

Check darker's output diff

--- test.py	2024-12-17 03:08:39.433662 +0000
+++ test.py	2024-12-17 03:09:47.115313 +0000
@@ -1,17 +1,19 @@
+def test_fun():
+    with open("bla") as f:
+        content = str(
+            """
+    whatever//
+    """
+        )

-def test_fun():
-  with open("bla") as f:
-    content = str("""
-    whatever//
-    """)
+        A = 1
+        B = 2
+        C = 3

-    A = 1
-    B = 2
-    C = 3
+    return

-  return

 def main():
   print("hello")
   test_fun()

Expected behavior

Darker should only reformat issues in the string literal that I modified.

Screenshots
If applicable, add copy/paste the output or attach a screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: Ubuntu 22.04
  • Python version 3.12.7
  • Git version 2.45.2
  • Darker version 2.1.1
  • Black version 24.10.0
  • other reformatter versions [e.g. isort==5.10.1, flynt==1.0.1]

Additional context
Add any other context about the problem here.

@akaihola
Copy link
Owner

Hi @anakinxc, thanks for the report!

Unfortunately this is by design. The algorithm Darker uses to narrow down the set of lines to reformat is based on diff chunks. In your example case the corrected indentation forms a contiguous chunk of changed lines, and Darker is unable to figure out the smallest valid set of lines to reformat.

There has been some exploration of improvement to this behavior, but unfortunately the amount of work would be massive (have Darker understand Python syntax), and some blocking questions couldn't be resolved. See #388 for earlier discussions, this particular comment for another example of indentation reformatting, and #221 for the failed attempt to solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants