tryceratops
v2.4.1
受此博客文章的启发。我在这里描述了此工具的建筑过程。
“对于那些喜欢恐龙的人?除了块以外,清洁尝试/除外。”
概括
flake8插件 pip install tryceratops
或者
poetry add -D tryceratops
tryceratops [filename or dir...]
您可以通过运行来启用实验分析仪:
tryceratops --experimental [filename or dir...]
您可以通过使用以下方式忽略特定的违规行为: --ignore TRYXXX :
tryceratops --ignore TRY201 --ignore TRY202 [filename or dir...]
您可以通过使用以下方式排除DIRS: --exclude dir/path :
tryceratops --exclude tests --exclude .venv [filename or dir...]
您还可以自动违规:
tryceratops --autofix [filename or dir...]
flake8插件? Tryceratops也是flake8的插件,因此您可以:
❯ flake8 --select TRY src/tests/samples/violations/call_raise_vanilla.py
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY002 Create your own exception
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY003 Avoid specifying long messages outside the exception class
src/tests/samples/violations/call_raise_vanilla.py:21:9: TRY201 Simply use 'raise' without specifying exception object again
所有违规及其描述都可以在文档中找到。
到目前为止,Autofix仅支持违规行为:TRY200,TRY201和TRY400。
如果您想忽略特定文件中的违规行为,则可以:
noqa的评论添加到您要忽略的文件顶部noqa的评论添加到您要忽略的行noqa: CODE的评论例子:
def verbose_reraise_1 ():
try :
a = 1
except Exception as ex :
raise ex # noqa: TRY202您可以为设置规则设置一个pyproject.toml文件。这对于避免一遍又一遍地重复使用相同的CLI标志很有用,并有助于定义项目的结构。
例子:
[ tool . tryceratops ]
exclude = [ " samples " ]
ignore = [ " TRY002 " , " TRY200 " , " TRY300 " ]
experimental = false
check_pickable = false
allowed_base_exceptions = [ " MyAppBase " ]CLI标志始终覆盖配置文件。
如果您想使用预先承诺,请添加此信息:
- repo : https://github.com/guilatrova/tryceratops
rev : v2.4.1
hooks :
- id : tryceratops 将此精美的徽章添加到您项目的README.md中:
[ ![ try/except style: tryceratops ] ( https://img.shields.io/badge/try%2Fexcept%20style-tryceratops%20%F0%9F%A6%96%E2%9C%A8-black )] ( https://github.com/guilatrova/tryceratops ) 如果您想了解更多有关:
感谢您考虑为所有人提供更好的人!
请参阅贡献文档。
请参阅ChangElog。
麻省理工学院
感谢上帝的灵感? ☁️☀️
黑色项目的见解。