自动修复 flake8 报告的一些问题的工具(来自 autoflake)。
项目描述
自动薄片8
介绍
autoflake8从 Python 代码中删除未使用的导入和未使用的变量。它利用pyflakes来做到这一点。
autoflake8pass默认也会删除无用的语句。
它是autoflake的一个维护分支。
与 autoflake 的区别
这个 autoflake 的分支从 autoflake 中删除了一些功能并修改了某些行为。这些更改的主要动机是 autoflake8 是为 flake8 的用户构建的,并且假设如果您使用 autoflake8,那么您也在使用 flake8。这促使删除标志
--imports和--remove-all-unused-imports:如果你想保留一个进口的副作用,使用# noqa.
此外,autoflake8还支持从 加载stdin和打印到
stdout,这使得用户可以很容易地与他们的自定义编辑器集成。
最后,一个很大的区别是autoflake8它在检测到问题/重写文件时以状态 1 退出。对于编辑器集成,
--exit-zero-even-if-changed可以使用新标志。定义该标志后,
autoflake8即使它修改文件也将返回状态 0。
在未来的计划方面,我们还计划最终停止使用正则表达式,并依靠实际的 AST 重写来修复 flake8 报告的问题。
例子
在以下示例中运行 autoflake8:
$ autoflake8 --in-place --remove-unused-variables example.py
import math
import re
import os
import random
import multiprocessing
import grp, pwd, platform
import subprocess, sys
def foo():
from abc import ABCMeta, WeakSet
try:
import multiprocessing
print(multiprocessing.cpu_count())
except ImportError as exception:
print(sys.version)
return math.pi
结果是:
import math
import sys
def foo():
try:
import multiprocessing
print(multiprocessing.cpu_count())
except ImportError:
print(sys.version)
return math.pi
安装
$ pip install --upgrade autoflake8
用作预提交挂钩
autoflake8可以用作预提交钩子。有关说明,请参阅
预提交。
样品.pre-commit-config.yaml:
- repo: https://github.com/fsouza/autoflake8
rev: v0.4.0
hooks:
- id: autoflake8
高级用法
要删除未使用的变量,请使用该--remove-unused-variables选项。
以下是选项的完整列表:
usage: autoflake8 [-h] [-c] [-r] [--exclude globs] [--expand-star-imports] [--remove-duplicate-keys] [--remove-unused-variables] [--version] [-v] [--exit-zero-even-if-changed] [-i | -s] files [files ...]
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
-r, --recursive drill down directories recursively
--exclude globs exclude file/directory names that match these comma-separated globs
--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-duplicate-keys
remove all duplicate keys in objects
--remove-unused-variables
remove unused variables
--keep-pass-statements
keep all `pass` statements
--keep-pass-after-docstring
keep `pass` statements after a newline ending on """
--version show program's version number and exit
-v, --verbose print more verbose logs (you can repeat `-v` to make it more verbose)
--exit-zero-even-if-changed
-i, --in-place make changes to files instead of printing diffs
-s, --stdout print changed text to stdout. defaults to true when formatting stdin, or to false otherwise
测试
要运行单元测试:
$ poetry run pytest
还有一个模糊测试,它针对给定 Python 文件的任何集合运行。它针对文件测试 autoflake8,并通过在文件之前和之后运行 pyflakes 来检查它的性能。如果 pyflakes 结果变得更糟,则测试失败。(这是在内存中完成的。实际文件保持不变):
$ scripts/fuzz.sh
排除特定行
即使您没有直接在该文件中使用它们,您也可能会为它们的副作用导入一些导入。
例如,这在基于 Flask 的应用程序中很常见。在其中导入导入 main 的 Python 模块(文件)app,以将它们包含在路由中。
例如:
from .endpoints import role, token, user, utils
为防止这种情况,您无需排除整个文件,您可以
# noqa在行尾添加注释,例如:
from .endpoints import role, token, user, utils # noqa
该行将指示autoflake8让该特定行保持原样。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
内置分布
autoflake8-0.4.0.tar.gz的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 32520226af273e4e0b058329b19b68ea444c2f419bd9f26d633c762c5aca3500 |
|
| MD5 | 8aab9087aa45f63cd5d4159ac13bffb6 |
|
| 布莱克2-256 | df2ba819f0965c99bb61e3bb376edbeeb62d4bd4b4f51d6ef629c2ef0fce0f7a |
autoflake8-0.4.0 -py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | f3a528a7794e9e5974b5ec342dc697c35fbc87de333603be5df6e87e828ca340 |
|
| MD5 | ac6e637cc600343bf1604aa2f83ea5fe |
|
| 布莱克2-256 | 3f1f0b6cce6d7fa8d91c04d36ef353ebe84e52cd54bd53ed7b79a1e39050b877 |