Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit mirrors-mypy does not report correct error #15634

Closed
ZhiyuanChen opened this issue Jul 10, 2023 · 8 comments
Closed

pre-commit mirrors-mypy does not report correct error #15634

ZhiyuanChen opened this issue Jul 10, 2023 · 8 comments
Labels
bug mypy got something wrong

Comments

@ZhiyuanChen
Copy link

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 ZhiyuanChen added the bug mypy got something wrong label Jul 10, 2023
@ikonst
Copy link
Contributor

ikonst commented Jul 10, 2023

@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 .?)

@ZhiyuanChen
Copy link
Author

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 .

@ikonst
Copy link
Contributor

ikonst commented Jul 11, 2023

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
Copy link
Contributor

ikonst commented Jul 12, 2023

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?)

@ZhiyuanChen
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@hauntsaninja
Copy link
Collaborator

I can't see that Google drive link and the pre-commit issue link no longer works.

See also #13916

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants