找不到名为的模块的实现或库存根

问题描述 投票:0回答:1

我有

mypy
预提交挂钩

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.8.0
    hooks:
      - id: mypy
        args:
          - --config-file=./.styleconfigs/mypy.ini
        additional_dependencies:
          [
            "types-requests",
            "types-python-dateutil",
            "types-redis",
            "types-ujson",
            "types-cachetools",
            "pydantic==2.5.3"
          ]

项目结构

project_dir/
   |-- src/
   |   |-- apps/
   |   |   |-- app1/
   |   |   |   |-- __init__.py
   |   |   |   |-- mod1.py
   |   |   |-- app2/
   |   |   |   |-- __init__.py
   |   |   |   |-- mod2.py
   |   |   |-- __init__.py
   |   |-- __init__.py
   |-- .styleconfigs/
   |   |-- mypy.ini
   |-- pre-commit-config.yaml

src/apps/app1/mod1.py

from apps.app2.mod2 import foo

我尝试从

pre-commit run -a
运行
project_dir
并收到错误
Cannot find implementation or library stub for module named "apps.app2.mod2"

但是当我运行命令

cd src && mypy --config-file ../.styleconfigs/mypy.ini .
时它可以正常工作。

python mypy pre-commit-hook python-3.11 pre-commit.com
1个回答
0
投票

git hooks(因此

pre-commit
)始终在存储库根目录下的工作目录下运行。所以你会想要与它进行比较,而不是
cd
src

您通常可以通过其配置文件调整 mypy 的选项以尊重源根目录,而与执行无关


免责声明:我写了预提交

© www.soinside.com 2019 - 2024. All rights reserved.