Isort: force_to_top doesn't work with 'from' imports

Created on 5 Apr 2019  路  3Comments  路  Source: PyCQA/isort

Input file:

import functools
from typing import Dict


print('Hello world!')

Now I run isort with --top argument:

isort --top typing test.py

Output:

~ $ cat test.py 
import functools
from typing import Dict


print('Hello world!')

Not what I expected. BUT if I import this way:

import functools
import typing

print('Hello world!')

Now isort respects --top argument and places import typing first:

~ $ cat test.py 
import typing
import functools

print('Hello world!')

I expected force_to_top option to work with from B import A imports.
I use isort v.4.3.16 with Python 3.7.

question

All 3 comments

By default, isort sorts from B import A and import A statements separately, so the STDLIB section is split into two subsections, an imports section and a from section. You should find that typing is forced to the top of the from section:

from typing import Dict
import functools
from abc import ABCMeta

becomes

import functools
from typing import Dict
from abc import ABCMeta

Try the force_sort_within_sections option: https://github.com/timothycrosley/isort/wiki/isort-Settings

@PaperclipBadger's response is correct,

closing this issue as the functionality does work, though it could certainly use more complete documentation.

Thanks!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnthagen picture johnthagen  路  3Comments

ghickman picture ghickman  路  3Comments

akaihola picture akaihola  路  3Comments

donjar picture donjar  路  3Comments

kevindaum picture kevindaum  路  4Comments