mypy
mypy copied to clipboard
--install-types can mask failure details
Bug Report
When no .mypy_cache folder exists and a user runs mypy --install-types --non-interactive ./, mypy may fail on fundamental issues but suggest that the failure was because of "no mypy cache directory," confusing the user.
To Reproduce
I've created a minimum reproducible example. Please find it here.
I summarize the example below.
Expected Behavior
For the source tree
.
├── a
│ └── lib.py
├── b
│ └── lib.py
└── main.py
where all files are empty except main.py, which might contain
def f(i: int = 0):
i = "test" # This is a mypy violation.
print(i)
if __name__ == "__main__":
f()
we expect running mypy --install-types --non-interactive ./ at the root of the source tree to either (1) generate the .mypy_cache and then report any typing errors or (2) explain why it cannot generate the cache.
Actual Behavior
mypy fails because b.lib duplicates a.lib by name in the same module. However, mypy does not report the duplicate module error. Instead, it only reports error: --install-types failed (no mypy cache directory).
This behavior is particularly confusing in CI, where our environment has no preexisting .mypy_cache and where an unsuspecting user may accidentally cause the duplicate-module behavior. mypy fails and suggests it's because of the missing cache directory.
(Deeper demonstration is in the reproducible example linked above. In short, if mypy finds an empty .mypy_cache folder, it will report the duplicate module issue. If we place __init__.py in a and b and remove .mypy_cache, mypy will install the cache correctly and report the typing error.)
My Environment
- Ubuntu 20
- Python 3.6 (this older version is from the production codebase in which we found this issue)
- mypy 0.910
Related to #10600 .
I'd like to add that I had the same issue when sqlalchemy-stubs was not installed.
Minified example
> mypy --install-types --non-interactive -c " a"
error: --install-types failed (no mypy cache directory)
If you create the cache directory (empty) then it has:
> mypy --install-types --non-interactive -c " a"
<string>:1:2: error: unexpected indent [syntax]
Found 1 error in 1 file (errors prevented further checking)
Why does this check even exist?
Still an issue with mypy==1.10.0. For me a error: Duplicate module named "xxx" (also at ".\yyy\xxx.py") error was hidden that way.
Hi there. Thanks for creating this issue ticket. We would like to add that the same problem is also tripping us on a CI/GHA integration.
- https://github.com/crate/crate-operator/pull/549#issuecomment-2147287636
Hi there. Do we expect this issue to be resolved at any moment with any mypy releases? Or the only workaround now is to create .mypy_cache/ in our pipelines? Thanks!
A follow-up to @jlin880 ...
mkdir .mypy_cache helps me get around mypy complaining about it not being there but I shouldn't have to do this.
I've created a pull request, https://github.com/python/mypy/pull/17485, that fixes this issue. Turns out it's as simple as removing an early exit.
Okay, i found how to fix it. Just create init.py in both folders (a and b) and mypy will be able to check your code
@CHuKeR
Without looking at it too hard to be absolutely sure, I think your proposed solution fixes the error in the example code. I.E., it makes both folders into packages so it's no longer a problem that "b.lib duplicates a.lib by name in the same module".
However, that was merely an example error, which tk-woven created on purpose, to illustrate how a wrong error message, error: --install-types failed (no mypy cache directory), occurs instead of an informative error message. To fix the no mypy cache directory problem, you have to use my pull request, mentioned above.
So, you are correct, but that's not the problem we need solved! 😁
I just catch same problem in my project and after spending a severals hours found this solution. And i tried your... Okay, not your, sorry, I tried mypy . --install-types --non-interactive
But the problem is very annoying and I hope that my advice can help someone.