Removes unused imports and unused variables as reported by pyflakes

Overview

autoflake

Build status

Introduction

autoflake removes unused imports and unused variables from Python code. It makes use of pyflakes to do this.

By default, autoflake only removes unused imports for modules that are part of the standard library. (Other modules may have side effects that make them unsafe to remove automatically.) Removal of unused variables is also disabled by default.

autoflake also removes useless pass statements.

Example

Running autoflake on the below example:

$ autoflake --in-place --remove-unused-variables example.py
import math
import re
import os
import random
import multiprocessing
import grp, pwd, platform
import subprocess, sys


def foo():
    from abc import ABCMeta, WeakSet
    try:
        import multiprocessing
        print(multiprocessing.cpu_count())
    except ImportError as exception:
        print(sys.version)
    return math.pi

results in

import math
import sys


def foo():
    try:
        import multiprocessing
        print(multiprocessing.cpu_count())
    except ImportError:
        print(sys.version)
    return math.pi

Installation

$ pip install --upgrade autoflake

Advanced usage

To allow autoflake to remove additional unused imports (other than than those from the standard library), use the --imports option. It accepts a comma-separated list of names:

$ autoflake --imports=django,requests,urllib3 <filename>

To remove all unused imports (whether or not they are from the standard library), use the --remove-all-unused-imports option.

To remove unused variables, use the --remove-unused-variables option.

Below is the full listing of options:

usage: autoflake [-h] [-i] [-r] [--exclude globs] [--imports IMPORTS]
                 [--expand-star-imports] [--remove-all-unused-imports]
                 [--remove-duplicate-keys] [--remove-unused-variables]
                 [--version]
                 files [files ...]

Removes unused imports and unused variables as reported by pyflakes.

positional arguments:
  files                 files to format

optional arguments:
  -h, --help            show this help message and exit
  -c, --check           return error code if changes are needed
  -i, --in-place        make changes to files instead of printing diffs
  -r, --recursive       drill down directories recursively
  --exclude globs       exclude file/directory names that match these comma-
                        separated globs
  --imports IMPORTS     by default, only unused standard library imports are
                        removed; specify a comma-separated list of additional
                        modules/packages
  --expand-star-imports
                        expand wildcard star imports with undefined names;
                        this only triggers if there is only one star import in
                        the file; this is skipped if there are any uses of
                        `__all__` or `del` in the file
  --remove-all-unused-imports
                        remove all unused imports (not just those from the
                        standard library)
  --ignore-init-module-imports
                        exclude __init__.py when removing unused imports
  --remove-duplicate-keys
                        remove all duplicate keys in objects
  --remove-unused-variables
                        remove unused variables
  --version             show program's version number and exit

Tests

To run the unit tests:

$ ./test_autoflake.py

There is also a fuzz test, which runs against any collection of given Python files. It tests autoflake against the files and checks how well it does by running pyflakes on the file before and after. The test fails if the pyflakes results change for the worse. (This is done in memory. The actual files are left untouched.):

$ ./test_fuzz.py --verbose

Excluding specific lines

It might be the case that you have some imports for their side effects, even if you are not using them directly in that file.

That is common, for example, in Flask based applications. In where you import Python modules (files) that imported a main app, to have them included in the routes.

For example:

from .endpoints import role, token, user, utils

As those imports are not being used directly, if you are using the option --remove-all-unused-imports, they would be removed.

To prevent that, without having to exclude the entire file, you can add a # noqa comment at the end of the line, like:

from .endpoints import role, token, user, utils  # noqa

That line will instruct autoflake to let that specific line as is.

Comments
  • Remove duplicate dictionary keys

    Remove duplicate dictionary keys

    This will automatically remove duplicate dictionary keys with the --remove-duplicate-keys argument.

    Closes https://github.com/myint/autoflake/issues/27

    Example

    --- original/../test.py
    +++ fixed/../test.py
    @@ -1,15 +1,9 @@
     print({
    -  'b': 456,
    -  'a': 123,
    -  'b': 7834,
       'a': 'wow',
    -  'b': 456,
    -  'c': 'hello',
       'c': 'hello2',
    -  'b': 'hiya',
       'b': 'hiya',
     })
     
    -print({ 'a': 123, 'a': 567 })
    -print({ 'b': 123, 'b': 567, 'b': 897, 'c': 'hello' })
    -print({ 'a': 'first value', 'c': 123, 'd': 'hjfkds', 'c': 567, 'g': 'hi', 'c': 'this value will stay', 'd': 'so will this one' })
    +print({ 'a': 567 })
    +print({ 'b': 897, 'c': 'hello' })
    +print({ 'a': 'first value', 'g': 'hi', 'c': 'this value will stay', 'd': 'so will this one' })
    
    opened by andrewda 32
  • Add stdin and stdout options

    Add stdin and stdout options

    Currently autoflake reads from a file by taking a filename, and outputs a diff with a patch to make. This works well for some command line usage, but doesn't integrate so well with replacing a buffer in Vim. This could be improved by offering an option to read the file from stdin, and an option for outputting the entire file contents, with the patch applied, to stdout. stdin support could either by via flag, or by reading a special filename -. Supposing there was a flag for stdout support, then autoflake could automatically remove unused imports in Vim with both flags like so.

    silent 0,$!autoflake --stdin --output-file
    

    That command would take the contents of the current buffer, pass it to autoflake via stdin, get the new file after applying the patch back out again, and replace the current buffer with that text.

    At the moment, I shall have to save my current buffer to a file, run autoflake on that temporary file and save the diff to another temporary file, and then use the :diffpatch command to apply the patch to the current buffer, in order to integrate autoflake with Vim.

    enhancement help wanted 
    opened by w0rp 21
  • load config from pyproject.toml or setup.cfg

    load config from pyproject.toml or setup.cfg

    Simple implementation for the currently missing functionality to load configuration from certain files:

    • [x] pyproject.toml
    • [x] setup.cfg (closes #68)

    to do:

    • [x] pytests
    opened by akeeman 18
  • Prevent `from * import *` to be splitted

    Prevent `from * import *` to be splitted

    This will prevent a from something import a, b, c to be splitted.

    Test code

    from math import sqrt, log, tan, cos
    import os, sys, collections
    
    print(log(2))
    print(tan(5))
    print(os)
    print(sys)
    

    Result

    --- original/tes.py
    +++ fixed/tes.py
    @@ -1,5 +1,6 @@
    -from math import sqrt, log, tan, cos
    -import os, sys, collections
    +from math import log, tan
    +import os
    +import sys
    
     print(log(2))
     print(tan(5))
    

    Closes https://github.com/myint/autoflake/issues/7

    opened by adhikasp 16
  • Add option to keep the useless passes

    Add option to keep the useless passes

    Adds a --keep-useless-pass argument to the CLI.

    Also had to change the behavior that replaces a removed import with pass to return None instead and filter out Nones

    opened by jobevers 13
  • Add support for parallel execution (autopep8's `--jobs` opt)

    Add support for parallel execution (autopep8's `--jobs` opt)

    Hello. Today I discovered this project existed, and I immediately integrated it in my own projects. Differently from autopep8 tool, I noticed it does not support the --jobs CLI option, so I decided to submit this PR. I ran this patched version of autoflake against psutil code base, and it resulted in more than a 2x speedup (my laptop has 8 logical cores).

    Standard:

    ~/svn/autoflake {parallel-exec}$ time python3 autoflake.py --expand-star-imports --remove-all-unused-imports --remove-duplicate-keys --remove-unused-variables -r /home/giampaolo/svn/psutil
    
    real	0m2,446s
    user	0m2,394s
    sys	0m0,052s
    

    Using --jobs opt:

    ~/svn/autoflake {parallel-exec}$ time python3 autoflake.py --jobs=0 --expand-star-imports --remove-all-unused-imports --remove-duplicate-keys --remove-unused-variables  -r /home/giampaolo/svn/psutil
    
    real	0m0,972s
    user	0m4,130s
    sys	0m0,213s
    

    About the patch: I had to turn argparse.Namespace into a dict because multiprocessing module is not able to serialize it.

    EDIT: fixed it ~~Unfortunately ŧest_autoflake.py reports 4 failures that I'm not sure how to fix. Hope this helps and thanks for this great tool.~~

    opened by giampaolo 11
  • Add option to expand single star import

    Add option to expand single star import

    This patch add new feature to automatically expand a star (wildcard) import to specify all names that used inside the code.

    A sample code like this

    from math import *
    sin(1)
    cos(0)
    

    will be changed into

    from math import cos, sin
    sin(1)
    cos(0)
    

    Note that there are still 2 bugs regarding this feature, which mainly caused by related upstream bug from pyflakes:

    1. A function/names that declared but later deleted by del command will raise a false positive that the names is undeclared and could possibly come from a star import (if present). https://github.com/PyCQA/pyflakes/issues/175
    2. pyflakes is "inconsistent" on defining an undefined var in case of __all__is used (like in module API files).
    from foo import * # contain function_1 and function_2
    
    __all__ = ['function_1', 'function_2', 'function_3']
    
    function_2() # just use it somewhere to trigger pyflake
    
    def function_3:
        return 'something'
    

    pyflakes will complain that function_2 is undefined and possibly come from module foo. The import then will be expanded into...

    from foo import function_2
    

    But then pyflakes will complain function_1 is undefined because it's used in __all__.

    Closes https://github.com/myint/autoflake/issues/14

    opened by adhikasp 9
  • Imports removed incorrectly with Generic class type annotation

    Imports removed incorrectly with Generic class type annotation

    Example code where a used import is incorrectly removed by autoflake:

    from __future__ import annotations
    
    from .scheduled_events import ScheduledEventUser
    
    class _AsyncIterator(AsyncIterator[T]):
        ...
    
    class ScheduledEventUserIterator(_AsyncIterator["ScheduledEventUser"]):
        ...
    

    Expected: The ScheduledEventUser import should not be removed

    Actual behavior: ScheduledEventUser gets removed even though it is used

    (Source file for context - https://github.com/nextcord/nextcord/blob/master/nextcord/iterators.py)

    opened by DenverCoder1 8
  • Removing import in __init__.py

    Removing import in __init__.py

    I have a python package with a structure like:

    .
    ├── setup.py
    └── mypackage
        ├── __init__.py
        └── mypackage.py
    
    

    The contents of __init__.py are as follows:

    from .mypackage import *
    

    Autoflake is removing all contents from __init__.py breaking the package install. To fix the issue I'm asking autoflake to ignore __init__.py

    bug 
    opened by jordan-bonecutter 7
  • check hangs forever when parallel execution is on

    check hangs forever when parallel execution is on

    Hello. There is the following issue with parallel execution.

    Suppose there are two files with unused imports:

    test_parallel/test1.py

    import sys
    

    test_parallel/test2.py

    import sys
    

    When run a check executed in parallel (-j 0 by default), it detects the unused imports, but then it hangs forever. And it needs keyboard interrupt to exit:

    $ autoflake test_parallel --check
    
    test_parallel/test1.py: Unused imports/variables detected
    test_parallel/test2.py: Unused imports/variables detected
    ^CProcess SpawnPoolWorker-10:
    Process SpawnPoolWorker-9:
    Process SpawnPoolWorker-8:
    Process SpawnPoolWorker-6:
    Process SpawnPoolWorker-5:
    Process SpawnPoolWorker-4:
    Process SpawnPoolWorker-2:
    Process SpawnPoolWorker-1:
    Traceback (most recent call last):
    

    Once child process exits after errors found, the main process just sits there waiting for something to be returned from the child process. But child have already exited, so there is nothing to return resulting in a forever wait.

    bug 
    opened by elenlee 7
  • 1.4: pytest is failing

    1.4: pytest is failing

    Looks like it some issue with test suite

    + /usr/bin/pytest -ra
    =========================================================================== test session starts ============================================================================
    platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
    rootdir: /home/tkloczko/rpmbuild/BUILD/autoflake-1.4
    collected 119 items
    
    test_autoflake.py ..........F..FF....FF.....F......................F.............FFF.F..............F.......F...FF.F......F.F............                            [100%]
    
    ================================================================================= FAILURES =================================================================================
    ________________________________________________________________________ UnitTests.test_filter_code ________________________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code>
    
            def test_filter_code(self):
    >           self.assertEqual(
                    """\
        import os
        pass
        os.foo()
        """,
                    ''.join(autoflake.filter_code("""\
        import os
        import re
        os.foo()
        """)))
    E   AssertionError: 'import os\npass\nos.foo()\n' != 'import os\nimport re\nos.foo()\n'
    E     import os
    E   - pass
    E   + import re
    E     os.foo()
    
    test_autoflake.py:157: AssertionError
    ____________________________________________________________ UnitTests.test_filter_code_multiline_from_imports _____________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code_multiline_from_imports>
    
            def test_filter_code_multiline_from_imports(self):
    >           self.assertEqual(
                    r"""\
        import os
        pass
        from os.path import (
            join,
        )
        join('a', 'b')
        pass
        os.foo()
        from os.path import \
            isdir
        isdir('42')
        """,
                    ''.join(autoflake.filter_code(r"""\
        import os
        import re
        from os.path import (
            exists,
            join,
        )
        join('a', 'b')
        from os.path import \
            abspath, basename, \
            commonpath
        os.foo()
        from os.path import \
            isfile \
            , isdir
        isdir('42')
        """)))
    E   AssertionError: "\\\nimport os\npass\nfrom os.path import (\n    join,\n)[78 chars]')\n" != "\\\nimport os\nimport re\nfrom os.path import (\n    joi[83 chars]')\n"
    E     \
    E     import os
    E   - pass
    E   + import re
    E     from os.path import (
    E         join,
    E     )
    E     join('a', 'b')
    E     pass
    E     os.foo()
    E     from os.path import \
    E         isdir
    E     isdir('42')
    
    test_autoflake.py:443: AssertionError
    _______________________________________________________________ UnitTests.test_filter_code_multiline_imports _______________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code_multiline_imports>
    
            def test_filter_code_multiline_imports(self):
    >           self.assertEqual(
                    r"""\
        import os
        pass
        import os
        os.foo()
        """,
                    ''.join(autoflake.filter_code(r"""\
        import os
        import re
        import os, \
            math, subprocess
        os.foo()
        """)))
    E   AssertionError: '\\\nimport os\npass\nimport os\nos.foo()\n' != '\\\nimport os\nimport re\nimport os, \\\n    subprocess\nos.foo()\n'
    E     \
    E     import os
    E   - pass
    E   + import re
    E   - import os
    E   + import os, \
    E   ?          +++
    E   +     subprocess
    E     os.foo()
    
    test_autoflake.py:427: AssertionError
    ______________________________________________________ UnitTests.test_filter_code_should_ignore_non_standard_library _______________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code_should_ignore_non_standard_library>
    
            def test_filter_code_should_ignore_non_standard_library(self):
    >           self.assertEqual(
                    """\
        import os
        import my_own_module
        pass
        from my_package import another_module
        from my_package import subprocess
        from my_blah.my_blah_blah import blah
        os.foo()
        """,
                    ''.join(autoflake.filter_code("""\
        import os
        import my_own_module
        import re
        from my_package import another_module
        from my_package import subprocess
        from my_blah.my_blah_blah import blah
        os.foo()
        """)))
    E   AssertionError: 'impo[24 chars]ule\npass\nfrom my_package import another_modu[84 chars]()\n' != 'impo[24 chars]ule\nimport re\nfrom my_package import another[89 chars]()\n'
    E     import os
    E     import my_own_module
    E   - pass
    E   + import re
    E     from my_package import another_module
    E     from my_package import subprocess
    E     from my_blah.my_blah_blah import blah
    E     os.foo()
    
    test_autoflake.py:491: AssertionError
    ___________________________________________________________ UnitTests.test_filter_code_should_ignore_semicolons ____________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code_should_ignore_semicolons>
    
            def test_filter_code_should_ignore_semicolons(self):
    >           self.assertEqual(
                    r"""\
        import os
        pass
        import os; import math, subprocess
        os.foo()
        """,
                    ''.join(autoflake.filter_code(r"""\
        import os
        import re
        import os; import math, subprocess
        os.foo()
        """)))
    E   AssertionError: '\\\nimport os\npass\nimport os; import math, subprocess\nos.foo()\n' != '\\\nimport os\nimport re\nimport os; import math, subprocess\nos.foo()\n'
    E     \
    E     import os
    E   - pass
    E   + import re
    E     import os; import math, subprocess
    E     os.foo()
    
    test_autoflake.py:476: AssertionError
    _____________________________________________________________ UnitTests.test_filter_code_with_indented_import ______________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_filter_code_with_indented_import>
    
            def test_filter_code_with_indented_import(self):
    >           self.assertEqual(
                    """\
        import os
        if True:
            pass
        os.foo()
        """,
                    ''.join(autoflake.filter_code("""\
        import os
        if True:
            import re
        os.foo()
        """)))
    E   AssertionError: 'import os\nif True:\n    pass\nos.foo()\n' != 'import os\nif True:\n    import re\nos.foo()\n'
    E     import os
    E     if True:
    E   -     pass
    E   +     import re
    E     os.foo()
    
    test_autoflake.py:170: AssertionError
    _________________________________________________________________________ UnitTests.test_fix_code __________________________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_fix_code>
    
            def test_fix_code(self):
    >           self.assertEqual(
                    """\
        import os
        import math
        from sys import version
        os.foo()
        math.pi
        x = version
        """,
                    autoflake.fix_code("""\
        import os
        import re
        import abc, math, subprocess
        from sys import exit, version
        os.foo()
        math.pi
        x = version
        """))
    E   AssertionError: 'import os\nimport math\nfrom sys import version\nos.foo()\n[18 chars]on\n' != 'import os\nimport re\nimport abc\nimport math\nimport subpr[60 chars]on\n'
    E     import os
    E   + import re
    E   + import abc
    E     import math
    E   + import subprocess
    E     from sys import version
    E     os.foo()
    E     math.pi
    E     x = version
    
    test_autoflake.py:573: AssertionError
    _________________________________________________________________ UnitTests.test_fix_code_with_from_and_as _________________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_fix_code_with_from_and_as>
    
            def test_fix_code_with_from_and_as(self):
    >           self.assertEqual(
                    """\
        from collections import namedtuple as xyz
        xyz
        """,
                    autoflake.fix_code("""\
        from collections import defaultdict, namedtuple as xyz
        xyz
        """))
    E   AssertionError: 'from collections import namedtuple as xyz\nxyz\n' != 'from collections import defaultdict, namedtuple as xyz\nxyz\n'
    E   - from collections import namedtuple as xyz
    E   + from collections import defaultdict, namedtuple as xyz
    E   ?                        +++++++++++++
    E     xyz
    
    test_autoflake.py:593: AssertionError
    _______________________________________________________ UnitTests.test_fix_code_with_from_and_as_and_escaped_newline _______________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_fix_code_with_from_and_as_and_escaped_newline>
    
            def test_fix_code_with_from_and_as_and_escaped_newline(self):
                """Make sure stuff after escaped newline is not lost."""
                result = autoflake.fix_code("""\
        from collections import defaultdict, namedtuple \\
            as xyz
        xyz
        """)
                # We currently leave lines with escaped newlines as is. But in the
                # future this we may parse them and remove unused import accordingly.
                # For now, we'll work around it here.
                result = re.sub(r' *\\\n *as ', ' as ', result)
    
    >           self.assertEqual(
                    """\
        from collections import namedtuple as xyz
        xyz
        """,
                    autoflake.fix_code(result))
    E               AssertionError: 'from collections import namedtuple as xyz\nxyz\n' != 'from collections import defaultdict, namedtuple as xyz\nxyz\n'
    E               - from collections import namedtuple as xyz
    E               + from collections import defaultdict, namedtuple as xyz
    E               ?                        +++++++++++++
    E                 xyz
    
    test_autoflake.py:705: AssertionError
    ____________________________________________________________ UnitTests.test_fix_code_with_from_and_depth_module ____________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_fix_code_with_from_and_depth_module>
    
            def test_fix_code_with_from_and_depth_module(self):
    >           self.assertEqual(
                    """\
        from distutils.version import StrictVersion
        StrictVersion('1.0.0')
        """,
                    autoflake.fix_code("""\
        from distutils.version import LooseVersion, StrictVersion
        StrictVersion('1.0.0')
        """))
    E   AssertionError: "from[17 chars]n import StrictVersion\nStrictVersion('1.0.0')\n" != "from[17 chars]n import LooseVersion, StrictVersion\nStrictVersion('1.0.0')\n"
    E   - from distutils.version import StrictVersion
    E   + from distutils.version import LooseVersion, StrictVersion
    E   ?                              ++++++++++++++
    E     StrictVersion('1.0.0')
    
    test_autoflake.py:645: AssertionError
    ________________________________________________________________ UnitTests.test_fix_code_with_indented_from ________________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_fix_code_with_indented_from>
    
            def test_fix_code_with_indented_from(self):
    >           self.assertEqual(
                    """\
        def z():
            from ctypes import POINTER, byref
            POINTER, byref
            """,
                    autoflake.fix_code("""\
        def z():
            from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
            POINTER, byref
            """))
    E       AssertionError: 'def [24 chars]port POINTER, byref\n    POINTER, byref\n    ' != 'def [24 chars]port c_short, c_uint, c_int, c_long, pointer, [36 chars]    '
    E         def z():
    E       -     from ctypes import POINTER, byref
    E       +     from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
    E             POINTER, byref
    
    test_autoflake.py:666: AssertionError
    __________________________________________________________________ UnitTests.test_standard_package_names ___________________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_standard_package_names>
    
        def test_standard_package_names(self):
    >       self.assertIn('os', list(autoflake.standard_package_names()))
    E       AssertionError: 'os' not found in []
    
    test_autoflake.py:77: AssertionError
    __________________________________________________________ UnitTests.test_without_ignore_init_module_imports_flag __________________________________________________________
    
    self = <test_autoflake.UnitTests testMethod=test_without_ignore_init_module_imports_flag>
    
        def test_without_ignore_init_module_imports_flag(self):
            # Need a temp directory in order to specify file name as __init__.py
            temp_directory = tempfile.mkdtemp(dir='.')
            temp_file = os.path.join(temp_directory, '__init__.py')
            try:
                with open(temp_file, 'w') as output:
                    output.write('import re\n')
    
                p = subprocess.Popen(
                    list(AUTOFLAKE_COMMAND) + [temp_file],
                    stdout=subprocess.PIPE)
                result = p.communicate()[0].decode('utf-8')
    
    >           self.assertIn('import re', result)
    E           AssertionError: 'import re' not found in ''
    
    test_autoflake.py:568: AssertionError
    __________________________________________________________________________ SystemTests.test_diff ___________________________________________________________________________
    
    self = <test_autoflake.SystemTests testMethod=test_diff>
    
            def test_diff(self):
                with temporary_file("""\
        import re
        import os
        import my_own_module
        x = 1
        """) as filename:
                    output_file = io.StringIO()
                    autoflake._main(argv=['my_fake_program', filename],
                                    standard_out=output_file,
                                    standard_error=None)
    >               self.assertEqual("""\
        -import re
        -import os
         import my_own_module
         x = 1
        """, '\n'.join(output_file.getvalue().split('\n')[3:]))
    E   AssertionError: '-import re\n-import os\n import my_own_module\n x = 1\n' != ' import re\n-import os\n import my_own_module\n x = 1\n'
    E   - -import re
    E   ? ^
    E   +  import re
    E   ? ^
    E     -import os
    E      import my_own_module
    E      x = 1
    
    test_autoflake.py:1328: AssertionError
    _____________________________________________________________ SystemTests.test_diff_with_encoding_declaration ______________________________________________________________
    
    self = <test_autoflake.SystemTests testMethod=test_diff_with_encoding_declaration>
    
            def test_diff_with_encoding_declaration(self):
                with temporary_file("""\
        # coding: iso-8859-1
        import re
        import os
        import my_own_module
        x = 1
        """) as filename:
                    output_file = io.StringIO()
                    autoflake._main(argv=['my_fake_program', filename],
                                    standard_out=output_file,
                                    standard_error=None)
    >               self.assertEqual("""\
         # coding: iso-8859-1
        -import re
        -import os
         import my_own_module
         x = 1
        """, '\n'.join(output_file.getvalue().split('\n')[3:]))
    E   AssertionError: ' # coding: iso-8859-1\n-import re\n-import os\n import my_own_module\n x = 1\n' != ' # coding: iso-8859-1\n import re\n-import os\n import my_own_module\n x = 1\n'
    E      # coding: iso-8859-1
    E   - -import re
    E   ? ^
    E   +  import re
    E   ? ^
    E     -import os
    E      import my_own_module
    E      x = 1
    
    test_autoflake.py:1354: AssertionError
    _______________________________________________________________________ SystemTests.test_end_to_end ________________________________________________________________________
    
    self = <test_autoflake.SystemTests testMethod=test_end_to_end>
    
            def test_end_to_end(self):
                with temporary_file("""\
        import fake_fake, fake_foo, fake_bar, fake_zoo
        import re, os
        x = os.sep
        print(x)
        """) as filename:
                    process = subprocess.Popen(AUTOFLAKE_COMMAND +
                                               ['--imports=fake_foo,fake_bar',
                                                filename],
                                               stdout=subprocess.PIPE)
    >               self.assertEqual("""\
        -import fake_fake, fake_foo, fake_bar, fake_zoo
        -import re, os
        +import fake_fake
        +import fake_zoo
        +import os
         x = os.sep
         print(x)
        """, '\n'.join(process.communicate()[0].decode().split('\n')[3:]))
    E   AssertionError: '-imp[84 chars]ort fake_zoo\n+import os\n x = os.sep\n print(x)\n' != '-imp[84 chars]ort fake_zoo\n+import os\n+import re\n x = os.sep\n print(x)\n'
    E     -import fake_fake, fake_foo, fake_bar, fake_zoo
    E     -import re, os
    E     +import fake_fake
    E     +import fake_zoo
    E     +import os
    E   + +import re
    E      x = os.sep
    E      print(x)
    
    test_autoflake.py:1526: AssertionError
    ________________________________________________________________________ SystemTests.test_in_place _________________________________________________________________________
    
    self = <test_autoflake.SystemTests testMethod=test_in_place>
    
            def test_in_place(self):
                with temporary_file("""\
        import foo
        x = foo
        import subprocess
        x()
    
        try:
            import os
        except ImportError:
            import os
        """) as filename:
                    output_file = io.StringIO()
                    autoflake._main(argv=['my_fake_program', '--in-place', filename],
                                    standard_out=output_file,
                                    standard_error=None)
                    with open(filename) as f:
    >                   self.assertEqual("""\
        import foo
        x = foo
        x()
    
        try:
            pass
        except ImportError:
            pass
        """, f.read())
    E   AssertionError: 'import foo\nx = foo\nx()\n\ntry:\n    pass\nexcept ImportError:\n    pass\n' != 'import foo\nx = foo\nimport subprocess\nx()\n\ntry:\n    pass\[28 chars]ss\n'
    E     import foo
    E     x = foo
    E   + import subprocess
    E     x()
    E
    E     try:
    E         pass
    E     except ImportError:
    E         pass
    
    test_autoflake.py:1379: AssertionError
    _____________________________________________________________ SystemTests.test_in_place_with_with_useless_pass _____________________________________________________________
    
    self = <test_autoflake.SystemTests testMethod=test_in_place_with_with_useless_pass>
    
            def test_in_place_with_with_useless_pass(self):
                with temporary_file("""\
        import foo
        x = foo
        import subprocess
        x()
    
        try:
            pass
            import os
        except ImportError:
            pass
            import os
            import sys
        """) as filename:
                    output_file = io.StringIO()
                    autoflake._main(argv=['my_fake_program', '--in-place', filename],
                                    standard_out=output_file,
                                    standard_error=None)
                    with open(filename) as f:
    >                   self.assertEqual("""\
        import foo
        x = foo
        x()
    
        try:
            pass
        except ImportError:
            pass
        """, f.read())
    E   AssertionError: 'import foo\nx = foo\nx()\n\ntry:\n    pass\nexcept ImportError:\n    pass\n' != 'import foo\nx = foo\nimport subprocess\nx()\n\ntry:\n    pass\[28 chars]ss\n'
    E     import foo
    E     x = foo
    E   + import subprocess
    E     x()
    E
    E     try:
    E         pass
    E     except ImportError:
    E         pass
    
    test_autoflake.py:1467: AssertionError
    ========================================================================= short test summary info ==========================================================================
    FAILED test_autoflake.py::UnitTests::test_filter_code - AssertionError: 'import os\npass\nos.foo()\n' != 'import os\nimport re\nos.foo()\n'
    FAILED test_autoflake.py::UnitTests::test_filter_code_multiline_from_imports - AssertionError: "\\\nimport os\npass\nfrom os.path import (\n    join,\n)[78 chars]')\n" !...
    FAILED test_autoflake.py::UnitTests::test_filter_code_multiline_imports - AssertionError: '\\\nimport os\npass\nimport os\nos.foo()\n' != '\\\nimport os\nimport re\nimpo...
    FAILED test_autoflake.py::UnitTests::test_filter_code_should_ignore_non_standard_library - AssertionError: 'impo[24 chars]ule\npass\nfrom my_package import another_modu[...
    FAILED test_autoflake.py::UnitTests::test_filter_code_should_ignore_semicolons - AssertionError: '\\\nimport os\npass\nimport os; import math, subprocess\nos.foo()\n' !=...
    FAILED test_autoflake.py::UnitTests::test_filter_code_with_indented_import - AssertionError: 'import os\nif True:\n    pass\nos.foo()\n' != 'import os\nif True:\n    imp...
    FAILED test_autoflake.py::UnitTests::test_fix_code - AssertionError: 'import os\nimport math\nfrom sys import version\nos.foo()\n[18 chars]on\n' != 'import os\nimport re...
    FAILED test_autoflake.py::UnitTests::test_fix_code_with_from_and_as - AssertionError: 'from collections import namedtuple as xyz\nxyz\n' != 'from collections import defa...
    FAILED test_autoflake.py::UnitTests::test_fix_code_with_from_and_as_and_escaped_newline - AssertionError: 'from collections import namedtuple as xyz\nxyz\n' != 'from col...
    FAILED test_autoflake.py::UnitTests::test_fix_code_with_from_and_depth_module - AssertionError: "from[17 chars]n import StrictVersion\nStrictVersion('1.0.0')\n" != "from...
    FAILED test_autoflake.py::UnitTests::test_fix_code_with_indented_from - AssertionError: 'def [24 chars]port POINTER, byref\n    POINTER, byref\n    ' != 'def [24 chars]p...
    FAILED test_autoflake.py::UnitTests::test_standard_package_names - AssertionError: 'os' not found in []
    FAILED test_autoflake.py::UnitTests::test_without_ignore_init_module_imports_flag - AssertionError: 'import re' not found in ''
    FAILED test_autoflake.py::SystemTests::test_diff - AssertionError: '-import re\n-import os\n import my_own_module\n x = 1\n' != ' import re\n-import os\n import my_own_m...
    FAILED test_autoflake.py::SystemTests::test_diff_with_encoding_declaration - AssertionError: ' # coding: iso-8859-1\n-import re\n-import os\n import my_own_module\n x = ...
    FAILED test_autoflake.py::SystemTests::test_end_to_end - AssertionError: '-imp[84 chars]ort fake_zoo\n+import os\n x = os.sep\n print(x)\n' != '-imp[84 chars]ort fake_zo...
    FAILED test_autoflake.py::SystemTests::test_in_place - AssertionError: 'import foo\nx = foo\nx()\n\ntry:\n    pass\nexcept ImportError:\n    pass\n' != 'import foo\nx = ...
    FAILED test_autoflake.py::SystemTests::test_in_place_with_with_useless_pass - AssertionError: 'import foo\nx = foo\nx()\n\ntry:\n    pass\nexcept ImportError:\n    pass\...
    ====================================================================== 18 failed, 101 passed in 4.12s ======================================================================
    
    bug 
    opened by kloczek 7
  • ignore-pass-statements introduces unnecessary pass statements

    ignore-pass-statements introduces unnecessary pass statements

    If "--ignore-pass-statements" is used, removed imports are being replaced with pass.

    >>> import autoflake
    >>> 
    >>> print(autoflake.__version__)
    2.0.0
    >>> 
    >>> code = """
    ... import os
    ... 
    ... print("hello")
    ... """
    >>> 
    >>> fixed_code = autoflake.fix_code(code, ignore_pass_statements=True)
    >>> print(fixed_code)
    
    pass
    
    print("hello")
    
    >>> 
    

    This looks like a bug.

    bug 
    opened by yarnabrina 3
  • --config does not work with project.toml

    --config does not work with project.toml

    When using --config a .cfg file is expected. It should also work with a pyproject.toml.

    See https://github.com/PyCQA/autoflake/blob/0373337022756fd930f01e6549a1b0a224e60376/autoflake.py#L1196

    enhancement 
    opened by alexander-born 0
  • Automatically resolving flake8-conda: C0301: line too long?

    Automatically resolving flake8-conda: C0301: line too long?

    Context

    While running a flake8-conda check using pre-commit, the outcome prompts me:

    code/project1/src/Datapoints.py:8:0: C0301: Line too long (137/100) (line-too-long)
    code/project1/src/Datapoints.py:12:0: C0301: Line too long (181/100) (line-too-long)
    code/project1/src/Datapoints.py:24:0: C0301: Line too long (130/100) (line-too-long)
    code/project1/src/Datapoints.py:25:0: C0301: Line too long (123/100) (line-too-long)
    code/project1/src/Datapoints.py:38:0: C0301: Line too long (173/100) (line-too-long)
    code/project1/src/Datapoints.py:108:0: C0301: Line too long (104/100) (line-too-long)
    code/project1/src/Datapoints.py:113:0: C0301: Line too long (102/100) (line-too-long)
    code/project1/src/Datapoints.py:123:0: C0301: Line too long (103/100) (line-too-long)
    

    An example for line 24 and 25, is:

            # Source: https://www.cips.org/supply-management/news/2016/november/logistics-industry-forecast-to-be-worth-155tn-by-2023/
            # NOTE: this article seems an unreliable source and is outdated, hence the 0.15 should possibly be changed/updated.
            self.logistics_market_share_dhl_fedex_ups = 0.15
    

    Question

    Is it possible to tell flake8-conda to automatically resolve the line-lengths where possible?

    Expected Output

    For example, I would expect it is (at least) able to rewrite the last commented line to:

            # Source: 
            # https://www.cips.org/supply-management/news/2016/november/logistics-industry-forecast-to-be-worth-155tn-by-2023/
            # NOTE: this article seems an unreliable source and is outdated, hence
            # the 0.15 should possibly be changed/updated.
            self.logistics_market_share_dhl_fedex_ups = 0.15
    
    opened by a-t-0 0
  • Remove duplicate keys doesn't remove always

    Remove duplicate keys doesn't remove always

    I noticed if the keys are on the same line, autoflake leaves them alone:

    x = {"A": 1, "A": 1}
    

    is untouched vs

    ```python
    x = {
        "A": 1, 
        "A": 1,
    }
    

    is fixed.

    opened by thejcannon 0
  • Adds / in start of file path and hence doesn't find a file on Windows

    Adds / in start of file path and hence doesn't find a file on Windows

    I'm synchronizing settings between Mac and Windows. I synchronize the same keybindings for both. On Mac everything is working, on Windows I get an error:

    [Errno 22] Invalid argument: '/c:/Users/AndrewA/Downloads/a.py'
    
    opened by IntoLighter 0
Owner
Steven Myint
Mars rover flight software developer
Steven Myint
A tool (and pre-commit hook) to automatically add trailing commas to calls and literals.

add-trailing-comma A tool (and pre-commit hook) to automatically add trailing commas to calls and literals. Installation pip install add-trailing-comm

Anthony Sottile 264 Dec 20, 2022
Code generation and code search for Python and Javascript.

Codeon Code generation and code search for Python and Javascript. Similar to GitHub Copilot with one major difference: Code search is leveraged to mak

null 51 Dec 8, 2022
Awesome autocompletion, static analysis and refactoring library for python

Jedi - an awesome autocompletion, static analysis and refactoring library for Python Jedi is a static analysis tool for Python that is typically used

Dave Halter 5.3k Dec 29, 2022
A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.

pyupgrade A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language. Installation pip install pyupgrade As a pre

Anthony Sottile 2.4k Jan 8, 2023
Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Tô Đức (Watson) 4 Feb 7, 2022
Codes of CVPR2022 paper: Fixing Malfunctional Objects With Learned Physical Simulation and Functional Prediction

Fixing Malfunctional Objects With Learned Physical Simulation and Functional Prediction Figure 1. Teaser. Introduction This paper studies the problem

Yining Hong 32 Dec 29, 2022
VSCode extension to sort and refactor python imports using reorder-python-imports.

reorder-python-imports VSCode extension to sort and refactor python imports using reorder-python-imports. Unlike other import organizers, reorder-pyth

Ryan Butler 3 Aug 26, 2022
Tool for pinpointing circular imports in Python. Find cyclic imports in any project

Pycycle: Find and fix circular imports in python projects Pycycle is an experimental project that aims to help python developers fix their circular de

Vadim Kravcenko 311 Dec 15, 2022
This script allows you to retrieve all functions / variables names of a Python code, and the variables values.

Memory Extractor This script allows you to retrieve all functions / variables names of a Python code, and the variables values. How to use it ? The si

Venax 2 Dec 26, 2021
Generates all variables from your .tf files into a variables.tf file.

tfvg Generates all variables from your .tf files into a variables.tf file. It searches for every var.variable_name in your .tf files and generates a v

null 1 Dec 1, 2022
A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.

flake8-bugbear A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycode

Python Code Quality Authority 869 Dec 30, 2022
Custom 64 bit shellcode encoder that evades detection and removes some common badchars (\x00\x0a\x0d\x20)

x64-shellcode-encoder Custom 64 bit shellcode encoder that evades detection and removes some common badchars (\x00\x0a\x0d\x20) Usage Using a generato

Cole Houston 2 Jan 26, 2022
Removes commented-out code from Python files

eradicate eradicate removes commented-out code from Python files. Introduction With modern revision control available, there is no reason to save comm

Steven Myint 146 Dec 13, 2022
Signature remover is a NLP based solution which removes email signatures from the rest of the text.

Signature Remover Signature remover is a NLP based solution which removes email signatures from the rest of the text. It helps to enchance data conten

Forges Alterway 8 Jan 6, 2023
Removes all archived super productivity tasks. Just run the python script.

delete-archived-sp-tasks.py Removes all archived super productivity tasks. Just run the python script. This is helpful to do a cleanup every 3-6 month

Ben Herbst 1 Jan 9, 2022
The LaTeX and Python code for generating the paper, experiments' results and visualizations reported in each paper is available (whenever possible) in the paper's directory

This repository contains the software implementation of most algorithms used or developed in my research. The LaTeX and Python code for generating the

João Fonseca 3 Jan 3, 2023
A simple bot which using an API , detects reported discord scams and kicks the user if possible while deleting the message

A simple bot which using an API , detects reported discord scams and kicks the user if possible while deleting the message

Vioshim 3 Nov 16, 2022
Tool to automatically fix some issues reported by flake8 (forked from autoflake).

autoflake8 Introduction autoflake8 removes unused imports and unused variables from Python code. It makes use of pyflakes to do this. autoflake8 also

francisco souza 27 Sep 8, 2022
Automates the fixing of problems reported by yamllint by parsing its output

yamlfixer yamlfixer automates the fixing of problems reported by yamllint by parsing its output. Usage This software automatically fixes some errors a

OPT Nouvelle Caledonie 26 Dec 26, 2022