Repository: coala/coala-bears

Issue:
PyUnusedCodeBear: Implement remove_unused_variables
The remove_unused_variables is implicitly passed on:
https://github.com/coala/coala-bears/blob/master/bears/python/PyUnusedCodeBear.py#L33

should be implemented as a setting in `run()`


Discussion:
Hello! i'm a newbie, could help me out a little in solving this? :)
Hi @shreab373 
Basically you should modify PyUnusedCodeBear to take additional setting named `remove_unused_variables` from coafile. If you need an example, the variable `remove_all_unused_imports` value is already taken from setting (hint, see the `run()` arguments).
@adhikasp thanks for helping!

could you also explain this syntax https://github.com/coala/coala-bears/blob/master/bears/python/PyUnusedCodeBear.py#L18  ??
@shreab373 Its just a parameter to a function that's annotated as boolean and default value is set to `False`

you have to create another parameter of the given name with the same annotations and default values :)

Resolving patch:
diff --git a/bears/python/PyUnusedCodeBear.py b/bears/python/PyUnusedCodeBear.py
--- a/bears/python/PyUnusedCodeBear.py
+++ b/bears/python/PyUnusedCodeBear.py
@@ -15,7 +15,8 @@ class PyUnusedCodeBear(LocalBear):
     CAN_DETECT = {'Unused Code'}
 
     def run(self, filename, file,
-            remove_all_unused_imports: bool=False):
+            remove_all_unused_imports: bool=False,
+            remove_unused_variables: bool=True):
         """
         Detects unused code. By default this functionality is limited to:
 
@@ -24,13 +25,15 @@ def run(self, filename, file,
 
         :param remove_all_unused_imports:
             True removes all unused imports - might have side effects
+        :param remove_unused_variables:
+            True removes unused variables - might have side effects
         """
 
         corrected = autoflake.fix_code(
                        ''.join(file),
                        additional_imports=None,
                        remove_all_unused_imports=remove_all_unused_imports,
-                       remove_unused_variables=True
+                       remove_unused_variables=remove_unused_variables
                        ).splitlines(True)
 
         for diff in Diff.from_string_arrays(file, corrected).split_diff():


Test patch:
diff --git a/tests/python/PyUnusedCodeBearTest.py b/tests/python/PyUnusedCodeBearTest.py
--- a/tests/python/PyUnusedCodeBearTest.py
+++ b/tests/python/PyUnusedCodeBearTest.py
@@ -34,6 +34,19 @@
 sys.exit(0)
 """
 
+with_unused_variables = """
+def main():
+    x = 10
+    y = 11
+    print(y)
+"""
+
+without_unused_variables = """
+def main():
+    y = 11
+    print(y)
+"""
+
 PyAllUnusedImportTest = verify_local_bear(
                 PyUnusedCodeBear,
                 valid_files=[valid_import,
@@ -52,3 +65,15 @@
                 invalid_files=[invalid_imports,
                                invalid_from_import],
                 settings={'remove_all_unused_imports': False})
+
+PyUnusedVariablesTest = verify_local_bear(
+                PyUnusedCodeBear,
+                valid_files=[without_unused_variables],
+                invalid_files=[with_unused_variables],
+                settings={'remove_unused_variables': True})
+
+PyUnusedVariablesBearTest = verify_local_bear(
+                PyUnusedCodeBear,
+                valid_files=[with_unused_variables],
+                invalid_files=[],
+                settings={'remove_unused_variables': False})