isort always skips __init__.py files

Created on 30 Mar 2014  路  8Comments  路  Source: PyCQA/isort

I want to sort imports in my __init__.py files, too.
In settings, __init__.py is configured to be skipped as default:(https://github.com/timothycrosley/isort/blob/develop/isort/settings.py#L45)
When defining a custom settings file like this:

[settings]
line_length=100
skip=foobar.py

isort still skips __init__.py files in addition to skips defined by myself. I cannot turn of skipping these, only by changing settings.py of isort itself.

Either __init__.py should be removed from default skip settings, or its default value should be ignored if configfile contains a skip section, too.

Most helpful comment

This was fixed properly in v4.3.5.

not_skip = __init__.py is no longer required.

All 8 comments

Agree on this one, I want to sort __init__ aswell and we should be able to do it by modifying the config file.

Yes, isort should handle __init__.py files, too. Maybe isort should ignore empty ones, though. When using the -a command line flag these would else be spammed with useless imports.

Sorry for the delay, I agree 100% there was an intention to allow this behavior but it's incorrectly implemented. I will get a fix in for this soon. Thanks so much for reporting the issue!

Thanks again everyone for reporting and weighing in on this issue. I'm sorry a fix was delayed for so long. I've fixed it in release 3.7.0 - with the introduction of a not_skip option defined in the wiki: https://github.com/timothycrosley/isort/wiki/isort-Settings. This allows you to explicitly not_skip files such as init.py independent of what might be set in user or default settings. Additionally @Bengt based on your advice I skip over empty files when adding imports unless 'force_adds' is set to true.

Thanks!

~Tim

did this ever get fixed? in 2019 this is still a problem

@manoadamro You can add the line not_skip = __init__.py to process __init__.py. Here is an example:

https://github.com/django/django/blob/master/setup.cfg#L17

This was fixed properly in v4.3.5.

not_skip = __init__.py is no longer required.

For those of you still using an older version of isort (Murphy's law dictates I currently have to use v4.3.4!), the not_skip trick will only work if you do not have the recursive switch on. I do not know well that code, but it is using two completely different paths to check if a file should be skipped depending if that flag is on or off. If the recursive path flag is on, the config file is read, then the dictionary is update with the arguments. If you do not specify any skip argument, it looks like the default value of __init__.py makes its way back to the config... The workaround I found is to specify to skip an empty string, or just a space char: isort -s " ".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ionelmc picture ionelmc  路  3Comments

ionelmc picture ionelmc  路  3Comments

adaamz picture adaamz  路  3Comments

donjar picture donjar  路  3Comments

jedie picture jedie  路  3Comments