删除未使用的导入和未使用的变量

autoflake的Python项目详细描述


Build status

简介

autoflake从python代码中删除未使用的导入和未使用的变量。IT 利用pyflakes来做这件事。

默认情况下,autoflake只删除属于 标准图书馆。(其他模块可能有副作用 自动删除是不安全的。)删除未使用的变量也被禁用 默认情况下。

autoflake还删除无用的pass语句。

示例

在以下示例上运行autoflake:

$ autoflake --in-place --remove-unused-variables example.py
importmathimportreimportosimportrandomimportmultiprocessingimportgrp,pwd,platformimportsubprocess,sysdeffoo():fromabcimportABCMeta,WeakSettry:importmultiprocessingprint(multiprocessing.cpu_count())exceptImportErrorasexception:print(sys.version)returnmath.pi

结果

importmathimportsysdeffoo():try:importmultiprocessingprint(multiprocessing.cpu_count())exceptImportError:print(sys.version)returnmath.pi

安装

$ pip install --upgrade autoflake

高级用法

允许autoflake删除其他未使用的导入(除了 使用--imports选项。IT 接受以逗号分隔的名称列表:

$ autoflake --imports=django,requests,urllib3 <filename>

删除所有未使用的导入(无论它们是否来自标准 库),使用--remove-all-unused-imports选项。

要删除未使用的变量,请使用--remove-unused-variables选项。

下面是选项的完整列表:

usage: autoflake [-h] [-i] [-r] [--exclude globs] [--imports IMPORTS]
                 [--expand-star-imports] [--remove-all-unused-imports]
                 [--remove-duplicate-keys] [--remove-unused-variables]
                 [--version]
                 files [files ...]

Removes unused imports and unused variables as reported by pyflakes.

positional arguments:
  files                 files to format

optional arguments:
  -h, --help            show this help message and exit
  -c, --check           return error code if changes are needed
  -i, --in-place        make changes to files instead of printing diffs
  -r, --recursive       drill down directories recursively
  --exclude globs       exclude file/directory names that match these comma-
                        separated globs
  --imports IMPORTS     by default, only unused standard library imports are
                        removed; specify a comma-separated list of additional
                        modules/packages
  --expand-star-imports
                        expand wildcard star imports with undefined names;
                        this only triggers if there is only one star import in
                        the file; this is skipped if there are any uses of
                        `__all__` or `del` in the file
  --remove-all-unused-imports
                        remove all unused imports (not just those from the
                        standard library)
  --ignore-init-module-imports
                        exclude __init__.py when removing unused imports
  --remove-duplicate-keys
                        remove all duplicate keys in objects
  --remove-unused-variables
                        remove unused variables
  --version             show program's version number and exit

测试

运行单元测试:

$ ./test_autoflake.py

还有一个fuzz测试,它针对给定python的任何集合运行 文件夹。它根据文件测试autoflake,并通过 前后对文件运行pyflakes。如果pyflakes 结果变糟了。(这是在记忆中完成的。实际的文件是 保持原样。):

$ ./test_fuzz.py --verbose

不包括特定行

可能是因为它们的副作用,你有一些进口的,甚至 如果你不直接在文件中使用它们。

这在基于烧瓶的应用中很常见。在你进口的地方 导入主app的python模块(文件),以便将它们包含在 路线。

例如:

from.endpointsimportrole,token,user,utils

因为这些导入不是直接使用的,如果您使用 --remove-all-unused-imports,它们将被删除。

为了防止这种情况,在不排除整个文件的情况下,可以添加 # noqa行末的注释,例如:

from.endpointsimportrole,token,user,utils# noqa

该行将指示autoflake让该特定行保持原样。

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

推荐PyPI第三方库


热门话题
如何在Java中创建固定大小的泛型数组?   javascript Paypal Braintree订阅付款   使用BufferedReader和PrintWriter实现java数据持久化?   类似于iCloud的java唯一google id   java使用网格布局和抓取组合框   java我的while循环无限运行,当我检查它时,它说while循环没有主体,而我认为它显然没有主体   java LWJGL碰撞3D OpenGL   java将Tibco RV切换到WebSphere MQ?   java如何使用Postman从curl发布REST   java是超级的,在通用通配符中是独占的吗?   在swing 1.5中,java在指定时间后自动关闭非模态对话框   java PrimeFaces饼图在JSF 2.0中不显示   java如何在Spring MVC中提供带有xml配置的默认bean实现?   java在eclipse中使用按钮关闭JFrame   java Sqoop jar已弃用   Java中的Getter方法