Isort: Sorting problems with typing.IO

Created on 6 Jan 2021  路  3Comments  路  Source: PyCQA/isort

First I would like to thank the isort project, I've been using it with all my Python projects and it's been great :)

isort with default settings changes

from typing import Any, IO

into

from typing import IO, Any

Digging into the code I realised that this is because isort treats IO as a constant, and adding classes=IO to the .isort.cfg file solves it. But I wonder if there is a better way for this to be documented / solved?

question

Most helpful comment

@donjar If you don't want your imports sorted by type you can use this option to disable it:
https://pycqa.github.io/isort/docs/configuration/options/#order-by-type

All 3 comments

Hi @donjar,

Glad you've found the isort project useful! I'm sorry you've encountered this corner case, and you are not alone in finding this behaviour counter-intuitive. As a general rule, isort can not depend on your local environment for it's logic, instead being simple file + config in, sorted file out. This means it cant say look at that variable and check it's type directly. However, for this exact case, and others within the standard library, it likely might make sense to pre define these edge cases (such as typing.IO is a class). What are your thoughts on this?

Thanks for your reply! Before encountering this I actually do not realise that isort puts constants before others by default, and I do not see this mentioned in the Readme as well. Maybe as a start we can clarify that? Because I certainly do expect that my imports are sorted alphabetically regardless of constants and classes.

If isort wishes to keep this defaults, I would definitely think that it makes sense to pre-define these edge cases in the stdlib.

@donjar If you don't want your imports sorted by type you can use this option to disable it:
https://pycqa.github.io/isort/docs/configuration/options/#order-by-type

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pawamoy picture pawamoy  路  3Comments

darkclouder picture darkclouder  路  3Comments

ionelmc picture ionelmc  路  3Comments

ionelmc picture ionelmc  路  3Comments

cjerdonek picture cjerdonek  路  3Comments