用于重新排序python导入的工具

reorder-python-imports的Python项目详细描述


Build StatusCoverage Status

对python导入重新排序

用于自动重新排序python导入的工具。类似于isort,但是 更多使用静态分析。

安装

pip install reorder-python-imports

控制台脚本

有关完整的选项集,请参阅reorder-python-imports --help

reorder-python-imports将文件名作为位置参数

常用选项:

  • --py##-plussee below
  • --add-import/--remove-importsee below
  • --replace-importsee below
  • --application-directories:默认情况下,reorder-python-imports假设 您的项目植根于.。如果这不是真的,告诉它你的 导入根Live。例如,当使用流行的布局时 使用--application-directories=.:src(注意:多条路径是分开的 使用:)。

作为预提交挂钩

有关说明,请参见pre-commit

样本.pre-commit-config.yaml

-repo:https://github.com/asottile/reorder_python_importsrev:v1.6.1hooks:-id:reorder-python-imports

它是做什么的?

将导入分为三个部分

importsysimportpyramidimportreorder_python_imports

变成

importsysimportpyramidimportreorder_python_imports

importfrom导入

之前导入
fromosimportpathimportsys

变成

importsysfromosimportpath

拆分from导入

fromos.pathimportabspath,exists

变成

fromos.pathimportabspathfromos.pathimportexists

删除重复导入

importosimportos.pathimportsysimportsys

变成

importos.pathimportsys

使用# noreorder

包含# noreorder注释的行及其后的行将 被忽视。此外,任何出现在非空白之后的导入 将忽略非注释行。

例如,这些将不会更改:

importsystry:# not import, not whitespaceimportfooexceptImportError:pass
importsysimportreorder_python_importsimportmatplotlib# noreordermatplotlib.use('Agg')importmatplotlib.pyplotasplt
# noreorderimportsysimportpyramidimportreorder_python_imports

为什么是这种风格?

reorder-python-imports选择的样式只有一个目标:reduce merge 冲突。

通过每行有一个导入,多个贡献者可以 从单个模块添加/删除导入而不会导致冲突。

考虑以下导致合并冲突的示例:

# 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_importprint('Hello world')

假设我不再关心支持Python2.5,我可以删除 from __future__ import with_statement--remove-import 'from __future__ import with_statement'

$ cat test.py
from __future__ import with_statementwith 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重写)。

此重写避免NameErrors,因为它仅在以下情况下发生:

  • 导入的符号前后相同
  • 导入是一个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提供了一些选项来帮助使用旧python“破釜沉舟” 通过删除__future__自动导入的版本。每个选项都意味着 所有旧版本。

  • --py22-plusnested_scopes
  • --py23-plusgenerators
  • --py26-pluswith_statement
  • --py3-plusdivisionabsolute_importprint_functionunicode_literals
  • --py37-plusgenerator_stop

删除/重写过时的six导入

使用--py3-plusreorder-python-imports还将删除/重写导入 来自six。重写遵循与 replacing imports以上。

例如:

+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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何为没有域的主机创建SSL证书?   数字的java正则表达式   java将\t\n显示为节点的原因是什么?   在java 8中按多个字段名分组   java Spring。使用@Configuration注释配置类。拥有多个配置类是否正确?   在java中设置Jasper报表的字体   eclipse Java双括号初始化   java如何测试活动性失败?   java使用JSch列出SFTP中的前N个文件   java Android结合了两个LayoutParams   java简单计算器应用程序防止崩溃   java在Eclipse4中禁用拖放部分   JavaFX过滤JDK Bug系统   java如何为所有实体实现通用spring jpa存储库   未使用正确的方法处理java捕获的异常