Skip to content

Commit

Permalink
PyUnusedCodeBear: Aspectization
Browse files Browse the repository at this point in the history
Closes coala#2016
  • Loading branch information
adhikasp committed Aug 28, 2017
1 parent 2bff583 commit ac2a975
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions bears/python/PyUnusedCodeBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@
from dependency_management.requirements.PipRequirement import PipRequirement
from coalib.results.Diff import Diff
from coalib.results.Result import Result


class PyUnusedCodeBear(LocalBear):
from coalib.bearlib.aspects import map_setting_to_aspect
from coalib.bearlib.aspects.Redundancy import (
Redundancy,
UnusedImport,
UnusedLocalVariable,
)


class PyUnusedCodeBear(
LocalBear,
aspects={
'fix': [
UnusedImport,
UnusedLocalVariable,
]},
languages=['Python'],
):
LANGUAGES = {'Python', 'Python 2', 'Python 3'}
REQUIREMENTS = {PipRequirement('autoflake', '0.6.6')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Unused Code'}

@map_setting_to_aspect(
remove_all_unused_imports=UnusedImport.remove_non_standard_import,
remove_unused_variables=UnusedLocalVariable)
def run(self, filename, file,
remove_all_unused_imports: bool=True,
remove_unused_variables: bool=True):
Expand All @@ -29,7 +46,6 @@ def run(self, filename, file,
:param remove_unused_variables:
``False`` keeps unused variables
"""

corrected = autoflake.fix_code(
''.join(file),
additional_imports=None,
Expand All @@ -41,4 +57,5 @@ def run(self, filename, file,
yield Result(self,
'This file contains unused source code.',
affected_code=(diff.range(filename),),
diffs={filename: diff})
diffs={filename: diff},
aspect=Redundancy('py'))

0 comments on commit ac2a975

Please sign in to comment.