Isort: Imports treated as wrong type

Created on 24 Apr 2020  路  4Comments  路  Source: PyCQA/isort

Some modules my organization uses have classes with all-uppercase names for devices with acronym names. Unfortunately, this causes them to look like constants in the eyes of PEP8 and therefore isort rules. For example: (both BeckhoffAxis and IMS are classes)

Expected sort:

from .epics_motor import BeckhoffAxis, IMS

Current sort:

from .epics_motor import IMS, BeckhoffAxis

I ran a few other tests, and as far as I can tell, isort is reading IMS as a constant and therefore moving it to the front of the list. I saw that this can be turned off using the order_by_type setting but we want this left on so that other types that we import are sorted separately.

Would it be possible to either:

  • Support known_type settings like known_class similar to how the known_{section} settings are used?
  • Have the type detected from the source file instead of by the name? (I understand why this option is tricky and can lead to other issues, but maybe just as an option when dealing with local imports?)
enhancement

Most helpful comment

This is implemented in develop and will be released in version 5.3.0 via the following new options:

constants=,classes=,variables=

Thanks!

~Timothy

All 4 comments

The second option is unlikely to ever be supported because of how much magic that would require. However, I think the first is a great idea and would happily accept a pull request that supported it.

Thanks!

~Timothy

Related to #1259, I think at least updating the docs to state that it's not actually ordering by type, but really it's using case-sensitivity, would help explain what's going on.

This is implemented in develop and will be released in version 5.3.0 via the following new options:

constants=,classes=,variables=

Thanks!

~Timothy

5.3.0 has now been released to PyPI

Thanks!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings