autoflake icon indicating copy to clipboard operation
autoflake copied to clipboard

Incorrect replacement of unused variable with pass when followed by comment or empty line

Open rcstanciu opened this issue 4 years ago • 5 comments

When replacing an unused variable, autoflake is messed up by seeing an empty line or a comment, and considers that it's an empty function. This results in replacing the unused variable with pass.

Minimal Reproductible Example

1. With comment

cat << EOF > test.py | autoflake --remove-unused-variables test.py
def main():
        unused_variable = 2020
        # Harmless comment
        print('Harmless print')
EOF
--- original/test_file.py
+++ fixed/test_file.py
@@ -1,4 +1,4 @@
 def main():
-    unused_variable = 2020
+    pass
     # Harmless comment
     print('Harmless print')

2. With empty line

cat << EOF > test.py | autoflake --remove-unused-variables test.py
def main():
        unused_variable = 2020

        print('Harmless print')
EOF
--- original/test.py
+++ fixed/test.py
@@ -1,4 +1,4 @@
 def main():
-        unused_variable = 2020
+        pass
 
         print('Harmless print')

rcstanciu avatar Dec 11 '20 15:12 rcstanciu

I can confirm that this issue is still there. Also for imports:

def some_function(self, args):
    from tensorflow import reduce_logsumexp

    result = args[0]
--- original/test.py
+++ fixed/test.py
@@ -53,7 +53,7
     def some_function(self, args):
-        from tensorflow import reduce_logsumexp
+        pass
 
         result = args[0]

jneeven avatar Sep 27 '22 08:09 jneeven

In my case, this behavior only happens when I pass the --ignore-pass-statements flag.

p-enel avatar Nov 18 '22 18:11 p-enel

Right to the above comment, in which in my case, I want to pass the flag so I can keep the pass statements for abstract base classes. Great if this could be fixed

chantel-marie-diaz avatar Nov 18 '22 18:11 chantel-marie-diaz

I'm seeing this behavior as well when using the --ignore-pass-statements flag

noam-better avatar Feb 16 '23 15:02 noam-better

I am also seeing this behavior when I use the --ignore-pass-statements flag. Is there a plan to fix this?

joeyagreco avatar May 14 '23 08:05 joeyagreco