PyCQA / flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
GNU Lesser General Public License v3.0
278 stars 72 forks source link

Automatically optimize imports #99

Closed advance512 closed 5 years ago

advance512 commented 7 years ago

PyCharm has such a feature, but it doesn't do it as precisely as flake8-import-order can.

I suggest either adding an "Auto-optimize" feature that modifies the order the imports in the top-level import block (via --in-place flag), or an "optimized suggestion" that is outputted.

The current situation where I have to run flake8 several times to try and find the "correct" order (sometimes I don't notice lexicographic order, for example, or where flake8-import-order expects a newline to be found) is not ideal.

pgjones commented 7 years ago

This is a good idea, which I'll need to think of how to implement it. I don't think there is any precedent of flake8 plugins doing this though, how did you image it would be used?

advance512 commented 7 years ago

We all know that flake8 (and its plugins) are not designed to modify source code, only analyze it and warn about issues encountered.

Now, flake8 uses pep8 (now pycodestyle) for code style checks. pycodestyle has an automated counterpart - autopep8. While pycodestyle doesn't allow automatic fixing, autopep8 does and in fact I use it as part of my linting flow: autopep8 --in-place.

I'm not sure if you could do the automatic fixing as a flake8 plugin, but I do suggest adding a new option: --recommend, that outputs the optimal import block (once!) if any warnings are encountered. If this is a problem due to the way flake8 communicates with its plugins, perhaps a new standalone app can be offered to automatically optimize imports, like autopep8.

sigmavirus24 commented 7 years ago

@advance512 you're information is quite out of date. Flake8 no longer leans on pycodestyle to run checks.

Flake8 has also started building out a way to do automatic fixing via plugins. We're looking for feedback though because we understand that some things need to be prioritized over others, so we need a pluggable system for plugins to say "I need to be run before something that fixes error code X" and a way for us to handle that.

I'm replying from my phone, so please go find the appropriate issue on Flake8's issue tracker and help us out there.

advance512 commented 7 years ago

@sigmavirus24 - oh, sorry about that. I am relying on https://github.com/PyCQA/flake8 for information - is the codebase (and README) there out of date?

Could I get a link to the appropriate issue on the issue tracker? Couldn't find it here: https://gitlab.com/pycqa/flake8/issues

sigmavirus24 commented 7 years ago

@advance512 I misread your comment.

Flake8 used to aggregate plugins and let pycodestyle run the checks. Flake8 now aggregates plugins and runs the checks itself. It does use pycodestyle's checks for PEP-0008 checking.

I'll dig in and find the appropriate issue on the issue tracker later.

sigmavirus24 commented 7 years ago

So I think the issue was https://gitlab.com/pycqa/flake8/issues/84 but GitLab seems to have completely lost that issue out of nowhere. I don't know why but it's quite surprising (especially compared to the fact that GitHub's 'anti-spam' effort involves hiding issues on random people's repositories when those issues have spam without the repo owner's consent... rather than addressing the source of the spam).

So, with that in mind, I rewrote a bunch of the details into https://gitlab.com/pycqa/flake8/issues/286. Please participate there (unless issue 84 turns back up and then I'll try and link them somehow :))

advance512 commented 7 years ago

Any updates?

taion commented 6 years ago

For just fixing imports specifically, there's also isort: https://pypi.python.org/pypi/isort. It doesn't quite have identical semantics to flake8-import-order, but it might be a useful starting point.