coala / coala-bears

Bears for coala
https://coala.io/
GNU Affero General Public License v3.0
295 stars 580 forks source link

PyImportSortBear does not support additional sections #1874

Open koniiiik opened 7 years ago

koniiiik commented 7 years ago

Vanilla isort supports custom sections with the known_<name> and sections configuration arguments. This is, as far as I can tell, currently not supported by the PyImportSortBear.

It's a bit tricky, since vanilla isort uses what basically amounts to variable kwargs for the section definitions, where each key has a list of module names associated with it, and I don't know if that's really possible with coala. I've come up with the following syntax that might be sufficient to express the custom isort sections in a coafile:

additional_import_sections = django:django, pandas:pandas;numpy
import_section_order = FUTURE, STDLIB, DJANGO, THIRDPARTY, PANDAS, FIRSTPARTY, LOCALFOLDER

With a possibility to similarly support custom section headings:

additional_import_section_headings = django:Custom heading for the Django section, pandas:Another one for pandas

Thoughts? Suggestions for a different syntax?

EDIT by @Makman2: ByImportSortBear --> PyImportSortBear :)

Makman2 commented 7 years ago

Those settings don't sound bad.

Maybe we can merge the import_section_order into additional_import_sections by exploiting the order of those section definitions?

additional_import_section_headings = django:Custom heading for the Django section, pandas:Another one for pandas

Does this setting only create aliases for those sections?

koniiiik commented 7 years ago

Maybe we can merge the import_section_order into additional_import_sections by exploiting the order of those section definitions?

Thing is, import_section_order would correspond to the sections isort setting, which includes the predefined ones (FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER), and (some of) those already have separate config options in the bear for lists of known package names that belong in them.

Edit for clarity: So if the order of sections in additional_import_sections defined the order of all sections, it would be necessary to also list standard, firstparty, and thirdparty in there, which would clash with the existing settings known_first_party_imports, known_third_party_imports, and known_standard_library_imports.

Does this setting only create aliases for those sections?

I'm sorry, I don't understand the question. This would correspond to the import_heading_<name> isort settings, which add comment headings above the respective sections.