Using:
isort 5.4.1
Python file:
from __future__ import annotations
from typing import *
from pathlib import Path
isort config:
known_typing = typing
sections = FUTURE,TYPING,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
no_lines_before = TYPING
Suggested change by isort:
@@ -1,4 +1,4 @@
from __future__ import annotations
-from typing import *
from pathlib import Path
+from typing import *
Expected behaviour: Successful pass
Other settings like lines_after_imports work, but there I can't get known_{section} to work at all, neither with built-in section nor custom sections.
@darkclouder
I'm sorry you ran into this issue with your config! Is it important to you that TYPING imports are above STDLIB imports? If not, you can achieve most of what you want simply by switching the order:
sections = FUTURE,STDLIB,TYPING,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
The reason isort is confused is because all stdlib modules are the default value for your known_standard_library setting, you can see this by doing isort --show-config. So, when you then specify 'known_typing: [typing]` you end up with a config that looks like this:
known_standard_library: ["os", ..., "typing", ...]
known_typing=["typing"]
As, you can see that leads to some ambiguity as typing is both in known_standard_library and in known_typing. To make the behaviour unambiguous when an import is in two sections, isort uses a consist priority order, which is the reverse of the sections defined. So for the sections list given the priority order for looking up known values is:
If typing was below standard library it would instead be:
Of course, you can also fix the issue by setting known_standard_library yourself, without the typing module, but this is probably not worth it since the number of modules is high. Otherwise, I will try to think of a resolution to this before the next minor release.
Thanks!
~Timothy
Hi Timothy, thanks for your answer!
The reason I'd like to put typing hints above everything else is the wildcard import which imports a range of different names.
In the worst case, I want them to be overridden by an actual implementation if the name is reused by some library.
If that name will be reused in stdlib is debatable, however I generally like the order from least to most specific where I think typing hints definitely is as least specific as it can get.
Have you thought about explicit configuration always overriding default order?
So an explicit mention in a section removes it from any other section?
Best wishes
Hi @darkclouder,
Have you thought about explicit configuration always overriding default order?
So an explicit mention in a section removes it from any other section?
I think that's the right way to think about it! In this case the more I thought about it the more I realized that stdlib are a special case, as everything else doesn't have a default built into isort and therefore is by definition more explicitly defined then the stdlib definition. I've updated the logic to prefer the other sections. Long term I think isort should also add warnings or maybe even errors if modules are defined in multiple lists within a single config file.
The fix for the issue you encountered is in the develop branch and will be released to PyPI with version 5.5.0 of isort scheduled for release by September 1st.
Thanks!
~Timothy
Most helpful comment
Hi @darkclouder,
I think that's the right way to think about it! In this case the more I thought about it the more I realized that stdlib are a special case, as everything else doesn't have a default built into isort and therefore is by definition more explicitly defined then the stdlib definition. I've updated the logic to prefer the other sections. Long term I think isort should also add warnings or maybe even errors if modules are defined in multiple lists within a single config file.
The fix for the issue you encountered is in the develop branch and will be released to PyPI with version 5.5.0 of isort scheduled for release by September 1st.
Thanks!
~Timothy