跳转到主要内容

为PyInvoke提供一些常见任务,以启动您的代码质量和测试工作流程。

项目描述

Invoke Common Tasks

为PyInvoke提供一些常见任务,以启动您的代码质量和测试工作流程。

入门

pip install invoke-common-tasks
# Or
poetry add -D invoke-common-tasks

# With Extras
pip install invoke-common-tasks[all]
# Or
poetry add --group dev invoke-common-tasks[all]

invoke-common-tasks定义了一些额外内容,其中您还可以安装与每个任务一起使用的工具。默认情况下,我们不会安装这些任务调用的工具,因为您可能具有与我们指定的不同版本的固定版本。

但是,您可以安装所有这些或不同的子集

  • 格式化 -> black, isort
  • lint -> flake8, flake8-docstrings
  • 类型检查 -> mypy
  • 测试 -> pytest, pytest-cov, coverage[toml]

所以如果您只想格式化测试

pip install invoke-common-tasks[format,test]

所有任务仍然可用,但我们不会安装相关工具。

Invoke 设置

tasks.py

from invoke_common_tasks import * # noqa

一旦您的tasks.py设置如下,invoke将具有额外的命令

λ invoke --list
Available tasks:

  build         Build wheel.
  ci            Run linting and test suite for Continuous Integration.
  format        Autoformat code for code style.
  init-config   Setup default configuration for development tooling.
  lint          Linting and style checking.
  test          Run test suite.
  typecheck     Run typechecking tooling.

您还可以运行以下命令来为每个工具初始化默认配置

invoke init-config --all

更多详细信息请参阅init-config部分。

任务

构建

假设您正在使用poetry,这将构建一个wheel(仅限wheel)。

格式化

这将应用代码格式化工具blackisort

这些只是这些命令的触发器,具体的配置细节取决于您。

在您的pyproject.toml中推荐的配置

[tool.black]
line-length = 120

[tool.isort]
profile = "black"
multi_line_output = 3
import_heading_stdlib = "Standard Library"
import_heading_firstparty = "Our Libraries"
import_heading_thirdparty = "Third Party"

lint

这将运行对blackisortflake8的检查。

您可以根据自己的喜好指定 flake8 插件及其配置。

.flake8 中的推荐配置

[flake8]
exclude = 
    venv,
    dist,
    .venv
select = ANN,B,B9,BLK,C,D,DAR,E,F,I,S,W
ignore = E203,E501,W503,D100,D104
per-file-ignores =
    tests/*: D103,S101
max-line-length = 120
max-complexity = 10
import-order-style = google
docstring-convention = google

推荐的 flake8 插件

更多 flake8 插件

https://github.com/DmytroLitvinov/awesome-flake8-extensions

类型检查

简单地运行 mypy .

推荐添加到您的 pyproject.toml 中的配置

[tool.mypy]
pretty = true
show_error_codes = true
show_column_numbers = true
show_error_context = true
exclude = [
  'tests/',
  'tasks\.py'
]
follow_imports = 'silent'
ignore_missing_imports = true

# Work your way up to these:
disallow_incomplete_defs = true
# disallow_untyped_defs = true 
# disallow-untyped-calls = true
# strict = true

测试(和覆盖率)

这将简单地运行 python3 -m pytest。以模块形式运行而不是直接运行 pytest 非常重要,因为它可以解决许多导入问题。

如果您喜欢其他配置,可以简单地不导入此任务。但所有配置和插件都留给您自行选择,这只是为了触发入口点。

pyproject.toml 中的推荐配置

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-s -vvv --color=yes --cov=. --no-cov-on-fail"

[tool.coverage.run]
omit = ["tests/*", "**/__init__.py", "tasks.py"]
branch = true

假设您已安装 pytest-covcoverage[toml]

推荐的 pytest 插件

  • pytest-xdist - 使用最大 CPU 内核并行运行测试
  • pytest-randomly - 每次以随机顺序运行测试,以检测测试之间意外的依赖关系,应隔离。每次运行都会打印出种子,如果您需要重现一个精确的种子运行。
  • pytest-cov - 建议使用 pytest 插件运行覆盖率。

其他 pytest 插件列表

https://pytest.cn/en/latest/reference/plugin_list.html

ci

这是一个没有命令但将 linttypechecktest 链接在一起的任务。

init-config

实验性:此功能仍处于预发布状态。

上述每个命令都附带了一些推荐的配置。此命令尝试自动化设置 pyproject.toml.flake8 文件中的这部分。

λ invoke init-config --help
Usage: inv[oke] [--core-opts] init-config [--options] [other tasks here ...]

Docstring:
  Setup default configuration for development tooling.

Options:
  -a, --all
  -f, --format
  -l, --lint
  -t, --test
  -y, --typecheck

待办事项

  • 添加自动生成 Sphinx 文档的任务。
  • 由于 Invoke v2+ 已发布,现在将类型检查支持添加到根 tasks.py 文件。

路线图

一旦上述 TODO 特性被勾选,并且该项目在生产环境中至少运行了 6 个月,该项目将被标记为稳定版 v1.0。

全部

一旦所有任务都已导入,您可以创建一个自定义任务作为默认任务,该任务将运行一些链式任务。

from invoke import task
from invoke_common_tasks import *

@task(pre=[format, lint, typecheck, test], default=True)
def all(c):
  """Default development loop."""
  ...

您会注意到这里的一些事情

  1. 此方法没有实现 ...
  2. 我们在 pre=[...] 参数中链接了一系列 @task
  3. 此根任务的 default=True 意味着我们可以运行 invoke all 或简单地运行 invoke

这有多酷?

贡献

您始终有权fork此项目,根据您的需要进行更改,然后

pip install https://github.com/user/repository/archive/branch.zip

Stackoverflow:从 GitHub 分支安装 pip

这样,您可以在短期内或在家中运行您自己的自定义 fork,并简单地使用此项目作为起点。这是完全可以的。

但是,如果您想将您的更改贡献回来,请打开一个 "跨分支" Pull Request。

一旦您的更改被合并并发布,您就可以恢复到使用 pip install 安装此包的规范版本。

如果您不确定如何进行更改或是否应该投入时间和精力,请打开一个问题,我们可以进行问题分类并进行讨论。

开发

git clone https://github.com/neozenith/invoke-common-tasks
cd invoke-common-tasks
poetry shell
# This will not install the dev dependencies which are optional when a consumer uses the project but are actually needed for development of this library
poetry install --all-extras

资源

现有技术

项目详细信息


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

invoke_common_tasks-0.4.0.tar.gz (10.7 kB 查看哈希值)

上传时间

构建分发

invoke_common_tasks-0.4.0-py3-none-any.whl (10.2 kB 查看哈希值)

上传时间 Python 3