Skip to content

follow_imports doesn't take affect for "spurious duplicate modules" #13757

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

Closed
sjschaff opened this issue Sep 28, 2022 · 2 comments · Fixed by #13758
Closed

follow_imports doesn't take affect for "spurious duplicate modules" #13757

sjschaff opened this issue Sep 28, 2022 · 2 comments · Fixed by #13758
Labels
bug mypy got something wrong

Comments

@sjschaff
Copy link

sjschaff commented Sep 28, 2022

Bug Report
I'm currently attempting to run mypy on a new project I'm setting up for my company. It has a dependency on a shared module which in its current state is not ready for type checking. Ideally I could just have mypy ignore that module, however I'm having some difficulty due to a perhaps somewhat unusual structure (described below). Fundamentally, the issue is a file named 'config.py' next to a directory name 'config' in the same location. This causes mypy to read the wrong "submodule", which would be fine for now, if not for that fact that it also cannot be ignored with follow_imports=skip. This results in an error in my project
error: Module "shared_module.config" has no attribute "CONFIG"

To Reproduce

mypy-repro.zip

I've attached a minimal repro project, but here is a summary:
Project Structure

  • project
    • my_module
      • mypy.ini
      • this_breaks.py
      • this_works.py
    • shared_module
      • config (not a submodule, no python code here)
        • a.json
        • b.json
        • etc.
      • config.py
      • foobar.py

config.py

CONFIG = {'foo': 'bar'}

foobar.py (empty file)

this_breaks.py

from shared_module.config import CONFIG

this_works.py

from shared_module.foobar import DOESNT_EXIST

mypy.ini

[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/../
namespace_packages = True

[mypy-shared_module.*]
follow_imports = skip

Expected Behavior
Mypy sees the import from shared_module.config import CONFIG, treats it as Any, and moves along
Mypy sees the import from shared_module.foobar import DOESNT_EXIST, treats it as Any, and moves along

Actual Behavior
Mypy sees the import from shared_module.foobar import DOESNT_EXIST, treats it as Any, and moves along exactly what I was hoping for (sanity checks that my ini file is setup correctly, obviously wont actually run in python)

Mypy sees the import from shared_module.config import CONFIG, does not ignore it, and additionally attempts to find CONFIG in the config folder, rather than config.py (The python interpreter correctly imports from config.py)
both are problems, but the former (not ignoring my import) is my primary concern for now.

I have also tried these to no avail

[mypy-shared_module.config]
[mypy-shared_module.config.*]

In the short term, I can use # type: ignore on my imports but this is far from ideal

$ mypy this_works.py
Success: no issues found in 1 source file

$ mypy this_breaks.py
this_breaks.py:1: error: Module "shared_module.config" has no attribute "CONFIG"
Found 1 error in 1 file (checked 1 source file)

Your Environment

macOS 12.6, conda

  • Mypy version used: 0.91, 0.981
  • Mypy command-line flags: (none, see ini file)
  • Mypy configuration options from mypy.ini (and other config files): (see above)
  • Python version used: 3.8.8, 3.8.13
@sjschaff sjschaff added the bug mypy got something wrong label Sep 28, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Sep 28, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Sep 28, 2022
@hauntsaninja
Copy link
Collaborator

Thanks for the reproducible issue report. This is a little weird, but looks like it's an easy fix.

In the meantime you can use this slightly weird workaround:

[mypy-shared_module.config.*]
follow_imports = skip
follow_imports_for_stubs = true

@sjschaff
Copy link
Author

Thanks for the quick reply, that workaround works great. Any thoughts on why mypy is picking up the empty directory instead of the file, while the python interpreter does it the other way around? (Although to be fair, I'm not sure if this is well defined behavior for python anyways)

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

Successfully merging a pull request may close this issue.

2 participants