跳转到主要内容

Python导入重排工具

项目描述

build status pre-commit.ci status

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: divisionabsolute_importprint_functionunicode_literals
  • --py37-plus: generator_stop

移除/重写过时的six导入

使用--py3-plusreorder-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-plusreorder-python-imports还将重写各种mock导入。

-from mock import patch
+from unittest.mock import patch

重写mypy_extensionstyping_extension导入

使用--py36-plus和更高版本,reorder-python-imports还将重写从typing移植的mypy_extensionstyping_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 (11.3 kB 查看哈希值)

上传时间 源代码

构建分发

reorder_python_imports-3.13.0-py2.py3-none-any.whl (11.6 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页