Repository: python/mypy

Issue:
follow_imports doesn't take affect for "spurious duplicate modules"
<!--
If you're not sure whether what you're experiencing is a mypy bug, please see the "Question and Help" form instead.
Please consider:

- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html
- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported
- asking on gitter chat: https://gitter.im/python/typing
-->

**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"`

<!--
If you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues

If the project you encountered the issue in is open source, please provide a link to the project.
-->


**To Reproduce**

[mypy-repro.zip](https://github.com/python/mypy/files/9669337/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
```python
CONFIG = {'foo': 'bar'}
```
foobar.py (empty file)

this_breaks.py
```python
from shared_module.config import CONFIG
```

this_works.py
```python
from shared_module.foobar import DOESNT_EXIST
```

mypy.ini
```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

<!--
How did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.
Write down what you thought would happen. If you expected no errors, delete this section.
-->

**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
```ini
[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

<!-- What went wrong? Paste mypy's output. -->
```bash
$ 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**

<!-- Include as many relevant details about the environment you experienced the bug in -->
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

<!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->



Discussion:

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -2592,11 +2592,11 @@ def find_module_and_diagnose(
         if (
             root_source  # Honor top-level modules
             or (
-                not result.endswith(".py")  # Stubs are always normal
-                and not options.follow_imports_for_stubs
-            )  # except when they aren't
-            or id in mypy.semanal_main.core_modules
-        ):  # core is always normal
+                result.endswith(".pyi")  # Stubs are always normal
+                and not options.follow_imports_for_stubs  # except when they aren't
+            )
+            or id in mypy.semanal_main.core_modules  # core is always normal
+        ):
             follow_imports = "normal"
         if skip_diagnose:
             pass


Test patch: