mypy icon indicating copy to clipboard operation
mypy copied to clipboard

pre-commit mirrors-mypy does not report correct error

Open ZhiyuanChen opened this issue 2 years ago • 3 comments

Bug Report

(A clear and concise description of what the bug is.)

Mypy complains duplicate module error when build directory is not excluded.

package/__init__.py: error: Duplicate module named "package" (also at "./build/lib/package/__init__.py")
package/__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
package/__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)

However, when running mypy with pre-commit, the error message is incorrect and misleading.

package/inference.py: error: Duplicate module named "package.inference" (also at "package/inference.py")
package/inference.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
package/inference.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)

see also: https://github.com/pre-commit/mirrors-mypy/issues/86

@asottile has confirmed it is an error with mypy.

To Reproduce

  1. generate build file
python -m build --sdist --wheel --no-isolation 
  1. run mypy
mypy --install-types
pre-commit run --all-files

Note that only one of my repository has this strange behaviour. Unfortunately, this repo is related to an ongoing manuscript and will not be publicly available until the review stage.

Your Environment

  • Mypy version used: mypy 1.3.0 (compiled: yes)
  • Mypy command-line flags: --install-types
  • Python version used: 3.10.11
[tool.mypy]
ignore_missing_imports = true

I suspect this issue relates to the --scripts-are-modules flag, as pre-commit set is as a default argument.

ZhiyuanChen avatar Jul 10 '23 15:07 ZhiyuanChen

@asottile has confirmed it is an error with mypy.

I wouldn't go that far. He only said that the mypy hook is simple.

My hunch is that it's not --scripts-are-modules, but rather how pre-commit passes filenames explicitly.

When you invoke mypy manually, how exactly you invoke it? (e.g. mypy .?)

ikonst avatar Jul 10 '23 17:07 ikonst

When you invoke mypy manually, how exactly you invoke it? (e.g. mypy .?)

Sorry, it looks like I forgot about the . when pasting.

Yes, mypy -install-types .

ZhiyuanChen avatar Jul 11 '23 03:07 ZhiyuanChen

The main difference is that pre-commit only asks mypy to check the files that are in your git tree (it gives mypy a list of specific files), whereas mypy itself given a directory (like .) walks it on its own and makes no exceptions for files not in git.

ikonst avatar Jul 11 '23 05:07 ikonst

Can you use the identity hook (replace mypy temporarily with identity) to see what pre-commit calls mypy with?

I'm failing to reproduce your issue locally, but I'm not certain about some details of your setup.

(btw, is your build/ somehow added to git, or firmly outside of it?)

ikonst avatar Jul 12 '23 05:07 ikonst

Can you use the identity hook (replace mypy temporarily with identity) to see what pre-commit calls mypy with?

Sorry I am a bit busy recently, I will try to work on it as soon as I have the time.

I'm failing to reproduce your issue locally, but I'm not certain about some details of your setup.

I'll try to pack the directory when possible.

(btw, is your build/ somehow added to git, or firmly outside of it?)

No, they are firmly outside of git.

ZhiyuanChen avatar Jul 12 '23 07:07 ZhiyuanChen

The main difference is that pre-commit only asks mypy to check the files that are in your git tree (it gives mypy a list of specific files), whereas mypy itself given a directory (like .) walks it on its own and makes no exceptions for files not in git.

This may explain the difference in error message ... As build/ directory is excluded by .gitignore. But if they are not checked... I suppose there won't be this issue. I'm quite certain the issue can be resolved by removing build/

ZhiyuanChen avatar Jul 12 '23 07:07 ZhiyuanChen

Hi, sorry for this late update.

I encountered a very similar error when running in rebase/cherry-pick. I have made a copy for your reference. Note that this time, deleting build/ won't work.

https://drive.google.com/drive/folders/11yUV7hDRrovbMv-PHUdg_K_TbJvjByb1?usp=sharing

ZhiyuanChen avatar Aug 24 '23 14:08 ZhiyuanChen