isort icon indicating copy to clipboard operation
isort copied to clipboard

`add_imports` + `append_only` inserts future import in illegal position

Open asottile-sentry opened this issue 3 years ago • 1 comments

#!/usr/bin/env python3

if __name__ == '__main__':
    import gevent.monkey
    gevent.monkey.patch_all()

import sys

print('silly example', file=sys.stderr)
$ isort --diff --append-only --add-import 'from __future__ import annotations' t.py
--- /tmp/y/t.py:before	2022-09-14 11:34:27.398068
+++ /tmp/y/t.py:after	2022-09-14 11:34:31.624185
@@ -4,6 +4,8 @@
     import gevent.monkey
     gevent.monkey.patch_all()
 
+from __future__ import annotations
+
 import sys
 
 print('silly example', file=sys.stderr)

asottile-sentry avatar Sep 14 '22 15:09 asottile-sentry

fwiw reorder-python-imports seems to get this right:

$ reorder-python-imports --add-import 'from __future__ import annotations' t.py
Reordering imports in t.py
$ diff -u t.py{.bak,}
--- t.py.bak	2022-09-14 11:38:00.148863504 -0400
+++ t.py	2022-09-14 11:38:07.424282984 -0400
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+from __future__ import annotations
 
 if __name__ == '__main__':
     import gevent.monkey

asottile-sentry avatar Sep 14 '22 15:09 asottile-sentry