Isort: Specify order of sections

Created on 25 Jun 2016  路  4Comments  路  Source: PyCQA/isort

If I specify some modules in forced_separate, I would also like a way to specify where that block of imports should appear relative to other blocks. For example, my current configuration is

[isort]
forced_separate = django
combine_as_imports = true
default_section = THIRDPARTY
include_trailing_comma = true
known_first_party = callisto
line_length = 79
multi_line_output = 5
not_skip = __init__.py

which results in

import bugsnag
from ratelimit.decorators import ratelimit

from callisto.evaluation.models import EvalRow

from .forms import SubmitToMatchingFormSet, SubmitToSchoolForm
from .matching import find_matches
from .models import EmailNotification, MatchReport, Report
from .report_delivery import PDFFullReport

from django.conf import settings
from django.contrib.auth import get_user_model
from django.http import HttpResponseForbidden
from django.shortcuts import render
from django.utils.html import conditional_escape

but I'd rather it be

import bugsnag
from ratelimit.decorators import ratelimit

from django.conf import settings
from django.contrib.auth import get_user_model
from django.http import HttpResponseForbidden
from django.shortcuts import render
from django.utils.html import conditional_escape

from callisto.evaluation.models import EvalRow

from .forms import SubmitToMatchingFormSet, SubmitToSchoolForm
from .matching import find_matches
from .models import EmailNotification, MatchReport, Report
from .report_delivery import PDFFullReport

as per django's coding style import rules.

question

Most helpful comment

Ah, yes, so this isn't a bug with isort, but rather docs that need updating. I didn't know about the sections directive. I started on http://isort.readthedocs.io/en/latest/#configuring-isort, then followed the link to the wiki, which doesn't mention sections. I thought what was on there was all I had to work with. If I had found my way to http://isort.readthedocs.io/en/latest/#custom-sections-and-ordering, I would've been fine.

All 4 comments

I think the issue is with forced_separate and the sections setting not playing nicely together. I'm able to get @kevindaum's preferred ordering with a config that has sections=FUTURE,STDLIB,THIRDPARTY,DJANGO,FIRSTPARTY,LOCALFOLDER and no forced_separate setting, but if I add forced_separate=django to that the Django imports are forced to the bottom (below LOCALFOLDER) no matter what I do.

Not sure if this is a bug, since known_django = django has the effect of forcing those imports to a separate section anyway, but it's a bit confusing and maybe could be cleared up in the forced_separate documentation.

Ah, yes, so this isn't a bug with isort, but rather docs that need updating. I didn't know about the sections directive. I started on http://isort.readthedocs.io/en/latest/#configuring-isort, then followed the link to the wiki, which doesn't mention sections. I thought what was on there was all I had to work with. If I had found my way to http://isort.readthedocs.io/en/latest/#custom-sections-and-ordering, I would've been fine.

This one had me baffled for a bit. I had to remove the forced_separate option to get the desired effect like @kelseyq suggested.

Thanks everyone for getting to the bottom of this!
I've updated the documentation to match the current behavior

Thanks!

~Timohty

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ionelmc picture ionelmc  路  3Comments

darkclouder picture darkclouder  路  3Comments

whg517 picture whg517  路  3Comments

lee-kagiso picture lee-kagiso  路  4Comments

jack1142 picture jack1142  路  3Comments