Skip to content

--install-types can mask failure details #10768

Closed

Description

@tk-woven

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 .

Activity

  1. berislav-harbr commented on Dec 10, 2021

    @berislav-harbr

    I'd like to add that I had the same issue when sqlalchemy-stubs was not installed.

  2. KotlinIsland commented on Mar 31, 2022

    @KotlinIsland
    Contributor

    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?

  3. Dobatymo commented on May 17, 2024

    @Dobatymo

    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.

  4. amotl commented on Jun 4, 2024

    @amotl

    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.

  5. jlin880 commented on Jun 11, 2024

    @jlin880

    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!

  6. alanbchristie commented on Jun 19, 2024

    @alanbchristie

    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.

  7. added a commit that references this issue on Jul 2, 2024
  8. 5 remaining items

  9. CHuKeR commented on Nov 28, 2024

    @CHuKeR

    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.

  10. added a commit that references this issue on Dec 30, 2024
    7b61945
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions