Python导入重排工具
项目描述
reorder-python-imports
自动重排Python导入的工具。类似于isort
,但使用了更多的静态分析。
安装
pip install reorder-python-imports
控制台脚本
请参考reorder-python-imports --help
以获取完整选项集。
reorder-python-imports
接受文件名作为位置参数
常见选项
--py##-plus
: 见下文.--add-import
/--remove-import
: 见下文.--replace-import
: 见下文.--application-directories
: 默认情况下,reorder-python-imports
假定您的项目根目录为.
。如果这不是真的,请告诉它导入根目录所在的位置。例如,当使用流行的./src
布局时,您会使用--application-directories=.:src
(注意:多个路径使用:
分隔)。--unclassifiable-application-module
: (可多次指定)被认为是应用程序模块的模块名称。此设置旨在用于像C模块这样的东西,这些模块可能不会始终出现在文件系统中。
作为pre-commit钩子
有关说明,请参阅pre-commit
示例.pre-commit-config.yaml
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.13.0
hooks:
- id: reorder-python-imports
它做什么?
将导入分为三个部分
import sys
import pyramid
import reorder_python_imports
变为(标准库,第三方,第一方)
import sys
import pyramid
import reorder_python_imports
import
导入在from
导入之前
from os import path
import sys
变为
import sys
from os import path
分割from
导入
from os.path import abspath, exists
变为
from os.path import abspath
from os.path import exists
删除重复导入
import os
import os.path
import sys
import sys
变为
import os.path
import sys
使用# noreorder
包含和紧随包含# noreorder
注释的行将被忽略。此外,任何出现在非空白非注释行之后的导入也将被忽略。
例如,这些将不会改变
import sys
try: # not import, not whitespace
import foo
except ImportError:
pass
import sys
import reorder_python_imports
import matplotlib # noreorder
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# noreorder
import sys
import pyramid
import reorder_python_imports
为什么这种风格?
reorder-python-imports
选择的风格只有一个目标:减少合并冲突。
通过每行只有一个导入,多个贡献者可以添加/移除单个模块中的导入,而不会产生冲突。
考虑以下会导致合并冲突的示例
# developer 1
-from typing import Dict, List
+from typing import Any, Dict, List
# developer 2
-from typing import Dict, List
+from typing import Dict, List, Tuple
与reorder-python-imports
强制执行的样式没有冲突
+from typing import Any
from typing import Dict
from typing import List
+from typing import Tuple
添加/移除导入
假设我想在我的代码库中强制执行absolute_import
。我可以使用:--add-import 'from __future__ import absolute_import'
。
$ cat test.py
print('Hello world')
$ reorder-python-imports --add-import 'from __future__ import absolute_import' test.py
Reordering imports in test.py
$ cat test.py
from __future__ import absolute_import
print('Hello world')
假设我不再关心支持Python 2.5,我可以使用--remove-import 'from __future__ import with_statement'
移除from __future__ import with_statement
。
$ cat test.py
from __future__ import with_statement
with open('foo.txt', 'w') as foo_f:
foo_f.write('hello world')
$ reorder-python-imports --remove-import 'from __future__ import with_statement' test.py
Reordering imports in test.py
$ cat test.py
with open('foo.txt', 'w') as foo_f:
foo_f.write('hello world')
替换导入
导入可以被自动替换为其他导入(如果它们提供相同的名称)。这对于提取兼容性库(如six
)很有用(请参阅下面的自动six
重写)。
这种重写避免了NameError
,因此它只发生在以下情况下:
- 导入的符号在导入前后相同
- 导入是
from
导入
参数指定为orig.mod=new.mod
或带有可选的已检查属性orig.mod=new.mod:attr
。已检查属性对于重命名模块中的某些导入而不是整个模块很有用。
例如
# full module move
--replace-import six.moves.queue=queue
# specific attribute move
--replace-import six.moves=io:StringIO
移除过时的__future__
导入
CLI提供了一些选项,可以帮助通过自动移除__future__
导入来“烧毁”与旧Python版本的桥梁。每个选项都意味着所有较旧版本。
--py22-plus
:nested_scopes
--py23-plus
:generators
--py26-plus
:with_statement
--py3-plus
:division
、absolute_import
、print_function
、unicode_literals
--py37-plus
:generator_stop
移除/重写过时的six
导入
使用--py3-plus
,reorder-python-imports
还将移除/重写来自six
的导入。重写遵循与上面替换导入相同的规则。
例如
+import queue
+from io import StringIO
+from urllib.parse import quote_plus
+
import six.moves.urllib.parse
-from six.moves import queue
-from six.moves import range
-from six.moves import StringIO
-from six.moves.urllib.parse import quote_plus
重写mock导入
使用--py3-plus
,reorder-python-imports
还将重写各种mock
导入。
-from mock import patch
+from unittest.mock import patch
重写mypy_extensions
和typing_extension
导入
使用--py36-plus
和更高版本,reorder-python-imports
还将重写从typing
移植的mypy_extensions
和typing_extensions
导入。
-from mypy_extensions import TypedDict
+from typing import TypedDict
重写PEP 585类型导入
使用--py39-plus
和更高版本,reorder-python-imports
将替换在PEP 585中从typing模块移出的导入。
-from typing import Sequence
+from collections.abc import Sequence
项目详情
下载文件
下载适合您平台的应用文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建分发
reorder_python_imports-3.13.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 994235fe9273373af6df7290de6a362a2426eb9bb800f5197367fe54b081f4d9 |
|
MD5 | 8d8688773720082939cd01c86e4bc7aa |
|
BLAKE2b-256 | aef863ecf759c9149d7d7a8b612ebfe74901164dde9adcb1c40975ddc713db1c |
reorder_python_imports-3.13.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cc20b1fe4f7fde98f3725d7e4588ae8a553f4824d72e1c5c3a2e3eb2bf382bca |
|
MD5 | 2bc6b3fb4aa92af01ae5d07291d9d1ab |
|
BLAKE2b-256 | dcfd55685465f3ebd39553f0d1f078fb4f6410fd747144f438ca0b3fe08ad400 |