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

How to configure? #169

Closed Integralist closed 4 years ago

Integralist commented 4 years ago

This is a silly question but I'm using vim's nvie/vim-flake8 which runs the currently available flake8 command + extensions whenever editing a file.

I've created a ~/.config/flake8 file with the following content:

[flake8]
max-line-length = 120

[flake8-import-order]
import-order-style = edited

But no matter what I set the import-order-style to, my vim editor keeps showing the same error for the imports.

Specifically I have the following imports...

from bf_metrics.rig import metrics
from nsq import Message
from structlog import get_logger

It sees them all as Third-Party and expects a line break between them.

Maybe the configuration is correct/working but I just don't have a style selected that's matching what I'm looking for 🤔

I ideally want to find a style that is:

<standard library modules - grouped together>

<third-party modules - grouped together>

<local modules - grouped together>

Would this need to be a custom style?

pgjones commented 4 years ago

You will need to name the application (local) module names, docs.

Integralist commented 4 years ago

Apologies for the late reply @pgjones but coming back to this I changed my ~/.config/flake8 to use pep8 format (e.g. "only enforces groups without enforcing the order within the groups").

I then setup an example script that imports both asyncio and time but had asyncio imported after the time module and the validator complained that time should come after asyncio (which suggests the configuration isn't being picked up as the pep8 format shouldn't care about that) 🤔

Integralist commented 4 years ago

I also used a local tox.ini file to override behaviours like so:

[flake8-import-order]
import-order-style = edited

But discovered that my local files were being identified as 'third-party' and not local?

Here is the file tree:

.
├── example.py
├── foo
│   └── bar.py
└── tox.ini
pgjones commented 4 years ago

You need to name the application (local) module names, docs, otherwise it assumes they are third party.

I've not come across ~/.config/flake8 so I'm not sure why that isn't picked up/used.

It looks like pep8 is the style you want.

sigmavirus24 commented 4 years ago

I've not come across ~/.config/flake8 so I'm not sure why that isn't picked up/used.

Flake8 is certainly picking it up but we only read config from [flake8] and we don't pass the file onto plugins. For y'all to get the settings forwarded to you, @Integralist would have to move the setting.

Integralist commented 4 years ago

@sigmavirus24 silly question but regarding...

we only read config from [flake8] and we don't pass the file onto plugins

Does that mean my setting of import-order-style isn't picked up by flake8 or something else entirely?

sigmavirus24 commented 4 years ago

[flake8] and [flake8-import-order] are two distinctly named config sections. Flake8 only looks at its own. Thus yes, by putting import-order-style anywhere else in ~/.config/flake8 Flake8 is not seeing that or considering it.

Integralist commented 4 years ago

Awesome, thanks!

Tomorrow I'll try moving that setting to [flake8] and see if this plugin starts to pick up the changed settings (unless I've misunderstood completely, which is very possible 😬)

Integralist commented 4 years ago

This worked! Thanks 🙂