python mypy via pre-commit -重复的模块名称“package.module.py”(和“package\module.py”)

bnl4lu3b  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(207)

我有下面的repo结构

my-repo/
    .github/
        linters/
            .mypy.ini
    .pre-commit-config.yaml
    mypackage/
        __init__.py
        main.py
        subpackage/
            __init__.py
            foo.py
    tests/
        conftest.py

当我通过预提交pre-commit run --all-files运行mypy时,我得到以下错误

mypackage\__init__.py: error: Duplicate module named "mypackage" (also at "mypackage\__init__.py")
mypackage\__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
mypackage\__init__.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
Found 1 error in 1 file (errors prevented further checking)

这是我使用的预提交配置

repos:
-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.3.0
    hooks:
    -   id: mypy
        args: [--config-file=.github/linters/.mypy.ini, mypackage]

当我直接运行mypy时,它可以工作:mypy --config-file=.github/linters/.mypy.ini mypackage

Success: no issues found in 23 source files

如果已经尝试了建议here,但无法让它工作。

编辑1

当我通过采用args: [--config-file=.github/linters/.mypy.ini]删除linting重复时,我仍然会遇到两种类型的错误

  • 源文件找到两次
tests\conftest.py: error: Source file found twice under different module names: "conftest" and "tests.conftest"
  • 找不到模块的实现或库存根(仅适用于第三方导入的模块),例如
mypackage\foo.py:1: error: Cannot find implementation or library stub for module named "uvicorn"  [import]
wqsoz72f

wqsoz72f1#

pre-commit的工作原理是将文件传递给hooks --你通过在命令行上列出包 * 并传递文件名来对文件进行双重检查--简单地删除你的重复:

repos:
-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.3.0
    hooks:
    -   id: mypy
        args: [--config-file=.github/linters/.mypy.ini]

免责声明:我写了预提交

相关问题