Isort: "import x as y" lost

Created on 1 Nov 2017  路  4Comments  路  Source: PyCQA/isort

running isort on

import os
import os as _os

results in

import os as _os

Ran into this problem in a large file that imported the same thing twice in different places with different names. Appreciate that this is bad code, but it would only be obvious after sorting, and isort breaks it.

bug

All 4 comments

@timothycrosley should this be enabled by flag? or by default?

I think this should be configurable, and probably off by default

Sorry, but the fix doesn't make a lot of sense to me.

Given this file:

import os
import os as _os
import sys as _sys
_sys.stdout.write("pathsep on %s is %s\n" % (os.name, _os.pathsep))

The default behaviour (isort 4.3 without parameters) results in

import os as _os
import sys as _sys

_sys.stdout.write("pathsep on %s is %s\n" % (os.name, _os.pathsep))

which is broken code. I do not understand why breaking code should be the default behaviour.

Running isort -k adds an unused import:

import os
import os as _os
import sys
import sys as _sys

_sys.stdout.write("pathsep on %s is %s\n" % (os.name, _os.pathsep))

and while that code still works I don't want to sprinkle unused imports all across my project just to avoid breaking code.

This is fixed in develop and will make it's way in isort 5.0.0

Thanks!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexandreYang picture AlexandreYang  路  3Comments

kevindaum picture kevindaum  路  4Comments

johnthagen picture johnthagen  路  3Comments

pradyunsg picture pradyunsg  路  3Comments

ghickman picture ghickman  路  3Comments