Cannot find implementation or library stub for module named

265 Views Asked by At

I have mypy pre-commit hook

  - 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 structure

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

I try to run pre-commit run -a from project_dir and receive errors Cannot find implementation or library stub for module named "apps.app2.mod2".

But when I run command cd src && mypy --config-file ../.styleconfigs/mypy.ini . it's work correctly.

1

There are 1 best solutions below

1
anthony sottile On

git hooks (and therefore pre-commit) always run with a working directory at the root of the repository. so you'll want to compare against that instead of cd into src

you can usually adjust mypy's options to respect your source root independent of execution through its configuration file


disclaimer: I wrote pre-commit