PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.43k stars 574 forks source link

'skip' option does not support ignore directories, similar like flake8 does #282

Closed luzfcb closed 9 years ago

luzfcb commented 9 years ago

I would set isort to ignore all the django migration files. is there any way to do this?

timothycrosley commented 9 years ago

Hi @luzfcb,

isort supports directory ignoring directories with the skip settings. From the command line you can do --skip migrations and it will skip and occurrence of a file within a folder named 'migrations' recursively.

Hope you find this helpful.

Thanks!

~Timothy

Micromegass commented 4 years ago

Is there also a way to do this in the settings file? I am trying to configure a pre-commit hook and a command line argument doesn't really solve this.

timothycrosley commented 4 years ago

HI @Micromegass,

Good question! Almost without exception settings that can be set via CLI can be set in a config file and vice versa. The mapping of the options is on the online documentation, here is the one for skip: https://timothycrosley.github.io/isort/docs/configuration/options/#skip and here is the documentation for using config files: https://timothycrosley.github.io/isort/docs/configuration/config_files/.

So an example .isort.cfg config file to skip migrations would look like:

[settings]
skip=migrations
Micromegass commented 4 years ago

Thank you for your quick reply and help.

I already tried various options with the documentation but isort keeps ignoring my settings and keeps formatting the migrations. That's why I came here.

I am running a django project and at the root-folder I have a setup.cfg in which I tried the settings. I also created an isort.cfg with the following settings, but it still keeps formatting the migrations:

[settings]
known_third_party = allauth,crispy_forms,django,environ,factory,geopy,leaflet,numpy,pandas,pytest,requests,rest_framework,rest_framework_jwt,selenium,sphinx_theme,storages
line_length = 89
multi_line_output = 3
include_trailing_comma = True
skip=migrations

I also tried this with skip:migrations/** and with skip_glob but it still formats my migrations.... Is there something I am doing wrong? Or an alternative way to achieve what I am trying?

timothycrosley commented 4 years ago

Hi @Micromegass,

I'm happy to do anything I can to help you resolve this issue! Your config looks correct to me.

Some follow up questions:

Thanks!

~Timothy

timothycrosley commented 4 years ago

@Micromegass

Just striked me what the issue may likely be! skip doesn't take effect by default for files you explicitly pass in, and precommit directly passes in files. If you want isort to skip these files, simply set filter_files to True within your config.

Thanks!

~Timothy

timothycrosley commented 4 years ago

@Micromegass one final update, if your issue is related to precommit, I've updated isorts official precommit configuration to filter files by default, and I've released a new version that improves this behaviour (5.2.1)

Micromegass commented 4 years ago

So first of all thank you so much for this great help and support, I really do appreciate it.

According to your suggestions, I tried the following:

I will try more things, but this is as far as I've gotten right now. Thanks again for all your effort and I hope I was clear enough in my explanations.

timothycrosley commented 4 years ago

Hi @Micromegass,

It looks like your stuff on the old 4.x.x version somehow! Can you confirm what isort --version gives you? Happy to work with you till we get your issue resolved :)

~Timothy

Micromegass commented 4 years ago

Thank you :). And you are right, the versions are off... I have isort installed in a virtual environment and isort --version gives me version 5.0.7. Even though I specified version 5.2.1 in my pre-commit-config.yml...

So I removed isort from my virtual env. And then I cleaned the cache of pre-commit.... But still the same problem

Micromegass commented 4 years ago

And if it helps here is my pre-commit config-file:


repos:
  - repo: https://github.com/asottile/seed-isort-config
    rev: v1.9.3
    hooks:
    - id: seed-isort-config
  - repo: https://github.com/pre-commit/mirrors-isort
    rev: v5.2.1
    hooks:
    - id: isort
  - repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.8
      exclude: |
        (?x)(
            migrations/|
            config/|
            _build/|
            buck-out/|
            build/|
            dist/
        )

So I assume that this takes the correct version (5.2.1) without me even having to install isort locally... I had it installed because I was using it manually before

timothycrosley commented 4 years ago

I think we're close! I'd recommend that you do the following:

  1. make sure you only have one config (doesn't matter which it is, sounds like you got to that step and currently it's .isort.cfg)
  2. ensure skip=migrations is in that config.
  3. Replace
    - repo: https://github.com/pre-commit/mirrors-isort
    rev: v5.2.1
    hooks:
    - id: isort

    with

    - repo: https://github.com/timothycrosley/isort
    rev: 5.2.1
    hooks:
    - id: isort
  4. Remove seed-isort-config it should no longer be necessary on the 5.x.x release and could lead to some weird inconsistencies.
  5. Retry.
  6. If it's still not working try adding skip_glob=*/migrations/* and filter_files=true to the config
timothycrosley commented 4 years ago

Another thing to keep in mind @Micromegass, after changing .isort.cfg you have to check it in for precommit to pick it up.

Micromegass commented 4 years ago

@timothycrosley you my friend, are a hero.

I worked by removing seed-isort-config and adding https://github.com/timothycrosley/isort as the repo. The rest was no longer needed. Thanks so much, I am very happy that I can use this great project of yours now in my projects. Also stumbled across your documentation project which I will soon check out more :)

So thank you a bunch for the fantastic and continuous help and support! Really appreciated

drjasonharrison commented 3 years ago

Thank you for this excellent discussion, I chose to modify my .pre-commit-config.yaml rather than using a .isort.cfg file.

This is what I found works in my .pre-commit-config.yaml to avoid isort and pre-commit from modifying files under my external/ folder:

-   repo: https://github.com/pre-commit/mirrors-isort
    rev: 'v5.6.4'  # Use the revision sha / tag you want to point at
    hooks:
    -   id: isort
        args: [--profile, black, --skip, external, --filter-files]