isort
isort copied to clipboard
Invalid syntax output for modules that directly import __future__
trafficstars
Given this example.py:
import __future__
print(__future__.all_feature_names)
Run isort with --add-import and a future import:
$ isort --add-import 'from __future__ import annotations' example.py
Fixing /Users/chainz/tmp/example.py
…gives:
import __future__
from __future__ import annotations
print(__future__.all_feature_names)
This file is invalid syntax:
$ python example.py
File "/Users/chainz/tmp/example.py", line 2
from __future__ import annotations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file
A workaround is to use if True, so isort puts the imports like so:
from __future__ import annotations
if True:
import __future__
print(__future__.all_feature_names)
But ideally isort would special case __future__ and sort the from imports before the module import:
from __future__ import annotations
import __future__
print(__future__.all_feature_names)