Giter VIP home page Giter VIP logo

Comments (5)

johncalls avatar johncalls commented on July 20, 2024 10

There exists a possible workaround in combination with isort:

  • Reformat every import into one line: isort -rc -sl .
  • Use autoflake to remove unused imports
  • Reformat the imports the way you want them to be using isort -rc -m x . (x being one of the different multi-line modes isort offers for imports)

from autoflake.

bersbersbers avatar bersbersbers commented on July 20, 2024 1

Just adding here that autoflake is also unable to detect single-line imports with parentheses:

from re import sub
from re import (subn)

First line is detected, second one is not. This will surely be fixed by a fix of this issue, but until then, maybe (just maybe) it makes sense to tackle it independently in the meantime.

from autoflake.

myint avatar myint commented on July 20, 2024

If I recall, for simplicity, autoflake just ignores cases where parentheses are used for imports. Pull requests to support this case are welcome.

from autoflake.

nad2000 avatar nad2000 commented on July 20, 2024
>>> autoflake.fix_code("from moduele import a, b\na.abc = 1\n", remove_all_unused_imports=True)  
'from moduele import a\na.abc = 1\n'                                                             
                                                                                                 
>>> autoflake.fix_code("from moduele import (a, b)\na.abc = 1\n", remove_all_unused_imports=True)
'from moduele import (a, b)\na.abc = 1\n'                                                        

I took a look at the source. The parser/autflake handles a sinle line at a time. So it doesn't suppor muli-line structures :(

from autoflake.

dzieciou avatar dzieciou commented on July 20, 2024

There exists a possible workaround in combination with isort:

  • Reformat every import into one line: isort -rc -sl .
  • Use autoflake to remove unused imports
  • Reformat the imports the way you want them to be using isort -rc -m x . (x being one of the different multi-line modes isort offers for imports

This workaround works great in most cases but fails in one, when I have to use it together with git precommit hooks:

$ git commit

isort....................................................................Failed
- hook id: isort
- files were modified by this hook

Fixing /projects/hooks/builder.py

autoflake................................................................Passed
isort....................................................................Failed
- hook id: isort
- files were modified by this hook

Fixing /projects/hooks/builder.py

This happens because even files formatted correctly are updated by first run of isort. As a result even correctly formatted files will not be commited.

from autoflake.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.