Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to prevent isort from sorting subfolders #1724

Closed
epogrebnyak opened this issue May 8, 2021 · 3 comments
Closed

how to prevent isort from sorting subfolders #1724

epogrebnyak opened this issue May 8, 2021 · 3 comments
Labels
question Further information is requested

Comments

@epogrebnyak
Copy link

epogrebnyak commented May 8, 2021

Many thanks for supporting isort!

I run into a curious issue: when running isort over a a folder where there was an environment subfolder .env and isort picked up sorting the imports in all the installed packages, which resulted in further error.

When I run black . the formatting affects just .py files in my project, does not affect files form .gitignore.

When I run isort . the changes affect everything in subfolders. How do I prevent this behavior? I studied documentation and isort --help, but could not think of a good option.

So far I resort to isort specific_file.py

@ucodery
Copy link
Collaborator

ucodery commented May 13, 2021

Hi @epogrebnyak, the documentation can be a little intimidating at first read. Have you seen this section https://pycqa.github.io/isort/docs/configuration/options/? There are a few configuration skip settings that could help you, notably extend_skip can be provided with directory names. Also skip_gitignore can be set to true (false by default) which sounds like it might be the easier fix for your specific situation as black is not needing extra exclude options.

@timothycrosley timothycrosley added the question Further information is requested label May 13, 2021
@epogrebnyak
Copy link
Author

epogrebnyak commented May 13, 2021

Hi @epogrebnyak, the documentation can be a little intimidating at first read. Have you seen this section https://pycqa.github.io/isort/docs/configuration/options/? There are a few configuration skip settings that could help you, notably extend_skip can be provided with directory names. Also skip_gitignore can be set to true (false by default) which sounds like it might be the easier fix for your specific situation as black is not needing extra exclude options.

Thanks for the guidance! I'm currently doing isort . --check-only --skip-glob .*, where .* is a pattern for hidden directory like .env , etc .venv. Maybe there is other way to configure it too.

As a suggestion - can similar skpping pattern be incorporated to isort default as in black? A lot of people use two tools together, reasonable to extect similar behaviour. I can try dig into configuration for black default if anyone endorses that.

@ucodery
Copy link
Collaborator

ucodery commented May 14, 2021

Glad that worked! .* is a great pattern for skipping, I'll just note that using --skip-glob will make isort only skip what you specified, while --extend-skip-glob will add your pattern to other files already ignored by isort. isort has quite a few skips by default; from the docs (.bzr, .direnv, .eggs, .git, .hg, .mypy_cache, .nox, .pants.d, .svn, .tox, .venv, _build, buck-out, build, dist, node_modules, venv) that's pretty nice!

Again, I'll note that the option --skip-gitignore will use your .gitignore just like black does by default. You can run isort <your options here> --show-files ./ to see all the files isort thinks it needs to read, without modifying them. This can maybe help you find the right combination of options (careful, this can be verbose!). You could compare with black --check --verbose but the output won't be one-to-one.

Black by default excludes these paths (.git, .hg, .mypy_cache, .nox, .tox, .venv, _build, buck-out, build, dist) actually less than isort! The difference is that using .gitignore is default for black, but opt-in for isort.

As an action item from this @timothycrosley probably if an isort-black inter-op doc is ever created (#1518) there should be mention that --skip-gitignore should be used to get both tools checking the same files. (and how to not clobber this ignore by using e.g. --skip later on)

@ucodery ucodery closed this as completed May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants