从不同目录的预提交中运行 mypy

npk*_*npk 5 python pre-commit python-3.x mypy pre-commit.com

我的项目结构如下:

\n
project/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api_v1\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api_v2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 api_v3\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 frontend\n
Run Code Online (Sandbox Code Playgroud)\n

每个 API 目录 、api_v1api_v2api_v3都有 python 文件。

\n

仅当代码发生更改时,我才想对每个目录运行预提交。例如,mypy -p api_v1如果目录发生更改,我想运行api_v1。我知道密钥filestypes预提交,但我无法找到一种方法来运行 mypy,就好像它是从目录运行一样backend。另外,当我对其中超过 1 个目录进行更改时,我无法单独为api_v1api_v2或运行 mypy。api_v3

\n

是不可能还是我

\n

Ant*_*ile 9

预提交对文件进行操作,因此您尝试执行的操作并不完全受支持,但一切皆有可能。当不运行文件时,您将采取一些效率让步,因为您将比需要的更频繁地进行 linting

以下是如何执行此操作的粗略草图:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: ...
    hooks:
    -   id: mypy
        pass_filenames: false  # suppress the normal filename passing
        files: ^backend/api_v1/  # filter the files down to a specific subdirectory
        # pre-commit only supports running at the root of a repo since that's where
        # git hooks run.  but it also allows running arbitrary code so you can
        # step outside of those bounds
        # note that `bash` will reduce your portability slightly
        entry: bash -c 'cd backend && mypy -p api_v1 "$@"' --

    # and then repeat ...
    -   id: mypy
        pass_filenames: false
        files: ^backend/api_v2/
        entry: bash -c 'cd backend && mypy -p api_v2 "$@"' --

    # etc.
Run Code Online (Sandbox Code Playgroud)

免责声明:我写了预提交