-1

I have the following repo-structure

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

When I run mypy via pre-commit pre-commit run --all-files I get the following error

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)

This is the pre-commit config I use

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

It works when I run mypy directly: mypy --config-file=.github/linters/.mypy.ini mypackage

Success: no issues found in 23 source files

If have already tried the suggestions here but couldn't get it to work.

EDIT 1

When I remove the linting duplication by adopting args: [--config-file=.github/linters/.mypy.ini] I still get two types of errors

  • source file found twice
tests\conftest.py: error: Source file found twice under different module names: "conftest" and "tests.conftest"
  • Cannot find implementation or library stub for module (only for 3rd party imported modules), e.g.
mypackage\foo.py:1: error: Cannot find implementation or library stub for module named "uvicorn"  [import]
tenticon
  • 2,639
  • 4
  • 32
  • 76

1 Answers1

2

pre-commit works by passing files to hooks -- you're double-linting the files by listing the package on the commandline and passing filenames -- simply remove your duplication:

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

disclaimer: I wrote pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207