Skip to content

Commit

Permalink
PyUnusedCodeBear: Implement remove_unused_variable
Browse files Browse the repository at this point in the history
Pass remove_unused_variables explicitly
to the run() method and add the corresponding
doc string and tests

Closes coala#1584
  • Loading branch information
Shreya Bhandare committed Apr 11, 2017
1 parent e0121a5 commit 7ef64db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bears/python/PyUnusedCodeBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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():
Expand Down
25 changes: 25 additions & 0 deletions tests/python/PyUnusedCodeBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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})

0 comments on commit 7ef64db

Please sign in to comment.