Skip to content

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

Closed
@sjschaff

Description

@sjschaff

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

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