Repository: python/mypy

Issue:
Cache causes mypy to fail every 2nd run for module importing from aws_cdk
**Bug Report**

When `mypy` is run multiple times on a Python module containing `from aws_cdk import core` it will fail every 2nd time due to the error "Skipping analyzing 'aws_cdk': found module but no type hints or library stubs".

Disabling the mypy cache with `--no-incremental` will result in `mypy` passing every time.

**To Reproduce**

1. Install `mypy` and `aws-cdk.core`.
2. Create a Python module with `from aws_cdk import core`.
3. Run `mypy` on the module and it will pass.
4. Run `mypy` on the module again and it will fail. Subsequent runs will cycle between passing and failing.

```
$ python3.8 -m venv .venv && source .venv/bin/activate
(.venv) $ pip install mypy==0.790 aws-cdk.core==1.80.0
(.venv) $ echo 'from aws_cdk import core' > repro.py
(.venv) $ mypy repro.py
Success: no issues found in 1 source file
(.venv) $ mypy repro.py
repro.py:1: error: Skipping analyzing 'aws_cdk': found module but no type hints or library stubs
repro.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
(.venv) $ mypy repro.py
Success: no issues found in 1 source file
(.venv) $ mypy repro.py
repro.py:1: error: Skipping analyzing 'aws_cdk': found module but no type hints or library stubs
repro.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
```

**Expected Behavior**

Running `mypy` on a module containing only `from aws_cdk import core` should always pass.

**Actual Behavior**

Every second time `mypy` is run on the module it will report an error.

**Your Environment**

- Mypy version used: 0.790
- Mypy command-line flags: none
- Mypy configuration options from `mypy.ini` (and other config files): none
- Python version used: 3.8.2
- Operating system and version: macOS
- `aws-cdk.core` version used: 1.80.0

The problem may be related to how the package that is installed is `aws-cdk.core` but the namespace being imported from is `aws_cdk`. A workaround is to change:

```python
from aws_cdk import core
```

to:

```python
import aws_cdk.core as core
```

The first form is what is generated by [the `cdk` tool](https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_create_app) and used in the cdk [example code](https://github.com/aws-samples/aws-cdk-examples/tree/master/python).


Discussion:
Huh, that's an interesting one. I can repro, including with master. But weirdly, not with my editable install of mypy.
Here's a side by side of `mypy -v` from a successful and a failing run:

<details>

```
LOG:  Mypy Version:           0.800+dev.dc251783ccb9baa48fc62d109a88854ac514816d			LOG:  Mypy Version:           0.800+dev.dc251783ccb9baa48fc62d109a88854ac514816d
LOG:  Config File:            Default									LOG:  Config File:            Default
LOG:  Configured Executable:  /Users/shantanu/.virtualenvs/wut/bin/python				LOG:  Configured Executable:  /Users/shantanu/.virtualenvs/wut/bin/python
LOG:  Current Executable:     /Users/shantanu/.virtualenvs/wut/bin/python				LOG:  Current Executable:     /Users/shantanu/.virtualenvs/wut/bin/python
LOG:  Cache Dir:              .mypy_cache								LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               False									LOG:  Compiled:               False
LOG:  Found source:           BuildSource(path='repro.py', module='repro', has_text=False, base_	LOG:  Found source:           BuildSource(path='repro.py', module='repro', has_text=False, base_
LOG:  Metadata fresh for repro: file repro.py							   |	LOG:  Could not load cache for repro: repro.meta.json
												   >	LOG:  Metadata not found for repro
												   >	LOG:  Parsing repro.py (repro)
												   >	LOG:  Metadata not found for aws_cdk
												   >	LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk (aws_cdk)
LOG:  Metadata fresh for aws_cdk.core: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for aws_cdk.core: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for builtins: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for builtins: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for abc: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/	LOG:  Metadata fresh for abc: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/
LOG:  Metadata fresh for datetime: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for datetime: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for enum: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for enum: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for typing: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag	LOG:  Metadata fresh for typing: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag
LOG:  Metadata fresh for jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for typing_extensions: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/	LOG:  Metadata fresh for typing_extensions: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/
LOG:  Metadata fresh for aws_cdk.core._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9	LOG:  Metadata fresh for aws_cdk.core._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9
LOG:  Metadata fresh for aws_cdk.cx_api: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/sit	LOG:  Metadata fresh for aws_cdk.cx_api: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/sit
LOG:  Metadata fresh for constructs: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa	LOG:  Metadata fresh for constructs: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa
LOG:  Metadata fresh for sys: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/	LOG:  Metadata fresh for sys: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/
LOG:  Metadata fresh for _typeshed: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac	LOG:  Metadata fresh for _typeshed: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac
LOG:  Metadata fresh for ast: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/	LOG:  Metadata fresh for ast: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/
LOG:  Metadata fresh for io: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m	LOG:  Metadata fresh for io: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m
LOG:  Metadata fresh for types: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package	LOG:  Metadata fresh for types: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package
LOG:  Metadata fresh for time: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for time: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for collections: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p	LOG:  Metadata fresh for collections: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p
LOG:  Metadata fresh for jsii.__meta__: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site	LOG:  Metadata fresh for jsii.__meta__: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site
LOG:  Metadata fresh for jsii._runtime: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site	LOG:  Metadata fresh for jsii._runtime: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site
LOG:  Metadata fresh for aws_cdk.cloud_assembly_schema._jsii: file /Users/shantanu/.virtualenvs/	LOG:  Metadata fresh for aws_cdk.cloud_assembly_schema._jsii: file /Users/shantanu/.virtualenvs/
LOG:  Metadata fresh for aws_cdk.cloud_assembly_schema: file /Users/shantanu/.virtualenvs/wut/li	LOG:  Metadata fresh for aws_cdk.cloud_assembly_schema: file /Users/shantanu/.virtualenvs/wut/li
LOG:  Metadata fresh for aws_cdk.cx_api._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3	LOG:  Metadata fresh for aws_cdk.cx_api._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3
LOG:  Metadata fresh for aws_cdk.region_info._jsii: file /Users/shantanu/.virtualenvs/wut/lib/py	LOG:  Metadata fresh for aws_cdk.region_info._jsii: file /Users/shantanu/.virtualenvs/wut/lib/py
LOG:  Metadata fresh for aws_cdk.region_info: file /Users/shantanu/.virtualenvs/wut/lib/python3.	LOG:  Metadata fresh for aws_cdk.region_info: file /Users/shantanu/.virtualenvs/wut/lib/python3.
LOG:  Metadata fresh for constructs._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/s	LOG:  Metadata fresh for constructs._jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/s
LOG:  Metadata fresh for importlib.abc: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site	LOG:  Metadata fresh for importlib.abc: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site
LOG:  Metadata fresh for array: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package	LOG:  Metadata fresh for array: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package
LOG:  Metadata fresh for mmap: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for mmap: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for os: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m	LOG:  Metadata fresh for os: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m
LOG:  Metadata fresh for _ast: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for _ast: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for codecs: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag	LOG:  Metadata fresh for codecs: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag
LOG:  Metadata fresh for _importlib_modulespec: file /Users/shantanu/.virtualenvs/wut/lib/python	LOG:  Metadata fresh for _importlib_modulespec: file /Users/shantanu/.virtualenvs/wut/lib/python
LOG:  Metadata fresh for json: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for json: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for jsii._compat: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for jsii._compat: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for attr: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages	LOG:  Metadata fresh for attr: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages
LOG:  Metadata fresh for jsii._reference_map: file /Users/shantanu/.virtualenvs/wut/lib/python3.	LOG:  Metadata fresh for jsii._reference_map: file /Users/shantanu/.virtualenvs/wut/lib/python3.
LOG:  Metadata fresh for jsii._kernel: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for jsii._kernel: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for jsii.python: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p	LOG:  Metadata fresh for jsii.python: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p
LOG:  Metadata fresh for jsii._kernel.types: file /Users/shantanu/.virtualenvs/wut/lib/python3.9	LOG:  Metadata fresh for jsii._kernel.types: file /Users/shantanu/.virtualenvs/wut/lib/python3.9
LOG:  Metadata fresh for importlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac	LOG:  Metadata fresh for importlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac
LOG:  Metadata fresh for posix: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package	LOG:  Metadata fresh for posix: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package
LOG:  Metadata fresh for os.path: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa	LOG:  Metadata fresh for os.path: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa
LOG:  Metadata fresh for json.decoder: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for json.decoder: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for json.encoder: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for json.encoder: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for importlib.resources: file /Users/shantanu/.virtualenvs/wut/lib/python3.	LOG:  Metadata fresh for importlib.resources: file /Users/shantanu/.virtualenvs/wut/lib/python3.
LOG:  Metadata fresh for attr.exceptions: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si	LOG:  Metadata fresh for attr.exceptions: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si
LOG:  Metadata fresh for attr.filters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for attr.filters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for attr.converters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si	LOG:  Metadata fresh for attr.converters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si
LOG:  Metadata fresh for attr.setters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-	LOG:  Metadata fresh for attr.setters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-
LOG:  Metadata fresh for attr.validators: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si	LOG:  Metadata fresh for attr.validators: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si
LOG:  Metadata fresh for attr._version_info: file /Users/shantanu/.virtualenvs/wut/lib/python3.9	LOG:  Metadata fresh for attr._version_info: file /Users/shantanu/.virtualenvs/wut/lib/python3.9
LOG:  Metadata fresh for inspect: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa	LOG:  Metadata fresh for inspect: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa
LOG:  Metadata fresh for itertools: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac	LOG:  Metadata fresh for itertools: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac
LOG:  Metadata fresh for functools: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac	LOG:  Metadata fresh for functools: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac
LOG:  Metadata fresh for jsii.errors: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p	LOG:  Metadata fresh for jsii.errors: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p
LOG:  Metadata fresh for jsii._utils: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p	LOG:  Metadata fresh for jsii._utils: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p
LOG:  Metadata fresh for jsii._kernel.providers: file /Users/shantanu/.virtualenvs/wut/lib/pytho	LOG:  Metadata fresh for jsii._kernel.providers: file /Users/shantanu/.virtualenvs/wut/lib/pytho
LOG:  Metadata fresh for genericpath: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p	LOG:  Metadata fresh for genericpath: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-p
LOG:  Metadata fresh for pathlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa	LOG:  Metadata fresh for pathlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packa
LOG:  Metadata fresh for textwrap: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for textwrap: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for jsii._kernel.providers.base: file /Users/shantanu/.virtualenvs/wut/lib/	LOG:  Metadata fresh for jsii._kernel.providers.base: file /Users/shantanu/.virtualenvs/wut/lib/
LOG:  Metadata fresh for jsii._kernel.providers.process: file /Users/shantanu/.virtualenvs/wut/l	LOG:  Metadata fresh for jsii._kernel.providers.process: file /Users/shantanu/.virtualenvs/wut/l
LOG:  Metadata fresh for atexit: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag	LOG:  Metadata fresh for atexit: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packag
LOG:  Metadata fresh for contextlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa	LOG:  Metadata fresh for contextlib: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa
LOG:  Metadata fresh for importlib.machinery: file /Users/shantanu/.virtualenvs/wut/lib/python3.	LOG:  Metadata fresh for importlib.machinery: file /Users/shantanu/.virtualenvs/wut/lib/python3.
LOG:  Metadata fresh for platform: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for platform: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for subprocess: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa	LOG:  Metadata fresh for subprocess: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pa
LOG:  Metadata fresh for tempfile: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for tempfile: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for cattr: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package	LOG:  Metadata fresh for cattr: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-package
LOG:  Metadata fresh for dateutil.parser: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si	LOG:  Metadata fresh for dateutil.parser: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/si
LOG:  Metadata fresh for dateutil: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for dateutil: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for jsii._embedded.jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.	LOG:  Metadata fresh for jsii._embedded.jsii: file /Users/shantanu/.virtualenvs/wut/lib/python3.
LOG:  Metadata fresh for jsii._embedded: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/sit	LOG:  Metadata fresh for jsii._embedded: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/sit
LOG:  Metadata fresh for cattr.converters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/s	LOG:  Metadata fresh for cattr.converters: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/s
LOG:  Metadata fresh for cattr.gen: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac	LOG:  Metadata fresh for cattr.gen: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pac
LOG:  Metadata fresh for cattr._compat: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site	LOG:  Metadata fresh for cattr._compat: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site
LOG:  Metadata fresh for cattr.disambiguators: file /Users/shantanu/.virtualenvs/wut/lib/python3	LOG:  Metadata fresh for cattr.disambiguators: file /Users/shantanu/.virtualenvs/wut/lib/python3
LOG:  Metadata fresh for cattr.multistrategy_dispatch: file /Users/shantanu/.virtualenvs/wut/lib	LOG:  Metadata fresh for cattr.multistrategy_dispatch: file /Users/shantanu/.virtualenvs/wut/lib
LOG:  Metadata fresh for re: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m	LOG:  Metadata fresh for re: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/m
LOG:  Metadata fresh for operator: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack	LOG:  Metadata fresh for operator: file /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-pack
LOG:  Metadata fresh for cattr.function_dispatch: file /Users/shantanu/.virtualenvs/wut/lib/pyth	LOG:  Metadata fresh for cattr.function_dispatch: file /Users/shantanu/.virtualenvs/wut/lib/pyth
LOG:  Loaded graph with 83 nodes (0.059 sec)							   |	LOG:  Loaded graph with 84 nodes (0.059 sec)
LOG:  Found 54 SCCs; largest has 21 nodes							   |	LOG:  Found 55 SCCs; largest has 21 nodes
LOG:  Processing 49 queued fresh SCCs								   |	LOG:  Processing 23 queued fresh SCCs
LOG:  Processing SCC singleton (aws_cdk.cx_api._jsii) as inherently stale			   |	LOG:  Processing SCC singleton (aws_cdk) as inherently stale
												   >	LOG:  Writing aws_cdk /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk aws_c
												   >	LOG:  Cached module aws_cdk has changed interface
												   >	LOG:  Processing 26 queued fresh SCCs
												   >	LOG:  Processing SCC singleton (aws_cdk.cx_api._jsii) as inherently stale with stale deps (aws_c
LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/cx_api/_jsii/	LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/cx_api/_jsii/
LOG:  Writing aws_cdk.cx_api._jsii /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/	LOG:  Writing aws_cdk.cx_api._jsii /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/
LOG:  Cached module aws_cdk.cx_api._jsii has changed interface						LOG:  Cached module aws_cdk.cx_api._jsii has changed interface
LOG:  Processing SCC singleton (aws_cdk.cx_api) as inherently stale with stale deps (aws_cdk.cx_   |	LOG:  Processing SCC singleton (aws_cdk.cx_api) as inherently stale with stale deps (aws_cdk aws
LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/cx_api/__init	LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/cx_api/__init
LOG:  Writing aws_cdk.cx_api /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cd	LOG:  Writing aws_cdk.cx_api /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cd
LOG:  Cached module aws_cdk.cx_api has changed interface						LOG:  Cached module aws_cdk.cx_api has changed interface
LOG:  Processing SCC singleton (aws_cdk.core._jsii) as inherently stale with stale deps (aws_cdk   |	LOG:  Processing SCC singleton (aws_cdk.core._jsii) as inherently stale with stale deps (aws_cdk
LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/core/_jsii/__	LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/core/_jsii/__
LOG:  Writing aws_cdk.core._jsii /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aw	LOG:  Writing aws_cdk.core._jsii /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aw
LOG:  Cached module aws_cdk.core._jsii has changed interface						LOG:  Cached module aws_cdk.core._jsii has changed interface
LOG:  Processing SCC singleton (aws_cdk.core) as inherently stale with stale deps (aws_cdk.core.   |	LOG:  Processing SCC singleton (aws_cdk.core) as inherently stale with stale deps (aws_cdk aws_c
LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/core/__init__	LOG:  Parsing /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/core/__init__
LOG:  Writing aws_cdk.core /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/	LOG:  Writing aws_cdk.core /Users/shantanu/.virtualenvs/wut/lib/python3.9/site-packages/aws_cdk/
LOG:  Cached module aws_cdk.core has changed interface							LOG:  Cached module aws_cdk.core has changed interface
LOG:  Processing SCC singleton (repro) as inherently stale with stale deps (aws_cdk.core)	   |	LOG:  Processing SCC singleton (repro) as inherently stale with stale deps (aws_cdk aws_cdk.core
LOG:  Parsing repro.py (repro)									   |	LOG:  Writing repro repro.py repro.meta.json repro.data.json
repro.py:1: error: Skipping analyzing 'aws_cdk': found module but no type hints or library stubs   |	LOG:  Cached module repro has changed interface
repro.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports	   <
LOG:  Deleting repro repro.py repro.meta.json repro.data.json					   <
LOG:  No fresh SCCs left in queue									LOG:  No fresh SCCs left in queue
LOG:  Build finished in 2.996 seconds with 83 modules, and 2 errors				   |	LOG:  Build finished in 2.980 seconds with 84 modules, and 0 errors
Found 1 error in 1 file (checked 1 source file)							   |	Success: no issues found in 1 source file
```
</details>
Looks like this was previously reported at https://github.com/python/mypy/issues/9777
I suspect it's the same as https://github.com/python/mypy/issues/7276 as well.
I have a similar issue, any new info on this?
I reported the same bug on https://github.com/pre-commit/pre-commit/issues/1966 as it happened only when I was running mypy from inside pre-commit but this morning I managed to find an workaround for this bug.

I reconfigured the hook to force it to avoid use of progressive run. Mainly on hook that is `args: ['--no-incremental']`.

Interesting enough, I am also on MacOS and using py39 from pyenv. Maybe the messed cached issue may be specific to macos.
@ssbarnea I have the same issue on linux. Probably not macos specific.
I am very curious on what is happening as that is a real PITA. It does not happen with all projects but is still common enough.
This a pretty bad bug, non-exhaustive list of reports: #10056 #7276 #9852 #9777 #10906 #10376 #10644 #10906

I found a "fix", but unfortunately, I don't really understand the code. But maybe in posting I can snipe @msullivan into fixing this for real :-)

First note that in #10056 there's a pretty minimal repro, scriptified as:
```
rm -rf lib env
mkdir -p lib/ns_package/reg_package

echo 'from ns_package import reg_package' > script.py
echo 'something: int' > lib/ns_package/reg_package/__init__.py
touch lib/ns_package/reg_package/py.typed
cat << EOF > lib/setup.py
from setuptools import setup, find_namespace_packages
setup(
    name="ns_package.reg_package",
    packages=find_namespace_packages(),
    package_data={"": ["py.typed"]},
)
EOF

python3 -m venv env
source env/bin/activate
pip install mypy -e lib

mypy script.py
mypy script.py
mypy script.py
mypy script.py
```

I'm also not sure the best way to turn this into a test. I haven't been able to repro without the PEP 561 package.

Here's the diff that fixes it:
```
diff --git a/mypy/build.py b/mypy/build.py
index b8831b21a..21dbb3b4f 100644
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -767,8 +767,9 @@ class BuildManager:
                     # As a workaround for for some bugs in cycle handling (#4498),
                     # if all of the imports are submodules, do the import at a lower
                     # priority.
-                    pri = import_priority(imp, PRI_HIGH if not all_are_submodules else PRI_LOW)
-                    res.insert(pos, ((pri, cur_id, imp.line)))
+                    if not all_are_submodules:
+                        pri = import_priority(imp, PRI_HIGH if not all_are_submodules else PRI_LOW)
+                        res.insert(pos, ((pri, cur_id, imp.line)))
                 elif isinstance(imp, ImportAll):
                     pri = import_priority(imp, PRI_HIGH)
                     res.append((pri, correct_rel_imp(imp), imp.line))
```

The only test failure this causes is testIncrementalPackageNameOverload, which isn't the worst thing.

This code was previously touched in:
https://github.com/python/mypy/pull/4910
https://github.com/python/mypy/pull/5016
(Guido's comment on the latter has aged well)
I had a few minutes so I went back and looked at old mypy releases and based on the above repro this started when namespace packages were added all the way back in 0.630. My gut tells me the real issue is modulefinder, but I'll take a closer look this weekend.

Patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -751,7 +751,6 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
                             res.append((ancestor_pri, ".".join(ancestors), imp.line))
                 elif isinstance(imp, ImportFrom):
                     cur_id = correct_rel_imp(imp)
-                    pos = len(res)
                     all_are_submodules = True
                     # Also add any imported names that are submodules.
                     pri = import_priority(imp, PRI_MED)
@@ -768,7 +767,10 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
                     # if all of the imports are submodules, do the import at a lower
                     # priority.
                     pri = import_priority(imp, PRI_HIGH if not all_are_submodules else PRI_LOW)
-                    res.insert(pos, ((pri, cur_id, imp.line)))
+                    # The imported module goes in after the
+                    # submodules, for the same namespace related
+                    # reasons discussed in the Import case.
+                    res.append((pri, cur_id, imp.line))
                 elif isinstance(imp, ImportAll):
                     pri = import_priority(imp, PRI_HIGH)
                     res.append((pri, correct_rel_imp(imp), imp.line))
@@ -1317,7 +1319,7 @@ def validate_meta(meta: Optional[CacheMeta], id: str, path: Optional[str],
         st = manager.get_stat(path)
     except OSError:
         return None
-    if not stat.S_ISREG(st.st_mode):
+    if not (stat.S_ISREG(st.st_mode) or stat.S_ISDIR(st.st_mode)):
         manager.log('Metadata abandoned for {}: file {} does not exist'.format(id, path))
         return None
 
@@ -1360,7 +1362,11 @@ def validate_meta(meta: Optional[CacheMeta], id: str, path: Optional[str],
 
         t0 = time.time()
         try:
-            source_hash = manager.fscache.hash_digest(path)
+            # dir means it is a namespace package
+            if stat.S_ISDIR(st.st_mode):
+                source_hash = ''
+            else:
+                source_hash = manager.fscache.hash_digest(path)
         except (OSError, UnicodeDecodeError, DecodeError):
             return None
         manager.add_stats(validate_hash_time=time.time() - t0)
@@ -1835,15 +1841,15 @@ def __init__(self,
         if path:
             self.abspath = os.path.abspath(path)
         self.xpath = path or '<string>'
-        if path and source is None and self.manager.fscache.isdir(path):
-            source = ''
-        self.source = source
         if path and source is None and self.manager.cache_enabled:
             self.meta = find_cache_meta(self.id, path, manager)
             # TODO: Get mtime if not cached.
             if self.meta is not None:
                 self.interface_hash = self.meta.interface_hash
                 self.meta_source_hash = self.meta.hash
+        if path and source is None and self.manager.fscache.isdir(path):
+            source = ''
+        self.source = source
         self.add_ancestors()
         t0 = time.time()
         self.meta = validate_meta(self.meta, self.id, self.path, self.ignore_all, manager)
@@ -2038,6 +2044,9 @@ def parse_file(self) -> None:
                     else:
                         err = "mypy: can't decode file '{}': {}".format(self.path, str(decodeerr))
                     raise CompileError([err], module_with_blocker=self.id) from decodeerr
+            elif self.path and self.manager.fscache.isdir(self.path):
+                source = ''
+                self.source_hash = ''
             else:
                 assert source is not None
                 self.source_hash = compute_hash(source)


Test patch:
diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test
--- a/test-data/unit/check-incremental.test
+++ b/test-data/unit/check-incremental.test
@@ -5563,3 +5563,25 @@ class D:
 [out2]
 tmp/a.py:2: note: Revealed type is "builtins.list[builtins.int]"
 tmp/a.py:3: note: Revealed type is "builtins.list[builtins.str]"
+
+[case testIncrementalNamespacePackage1]
+# flags: --namespace-packages
+import m
+[file m.py]
+from foo.bar import x
+x + 0
+[file foo/bar.py]
+x = 0
+[rechecked]
+[stale]
+
+[case testIncrementalNamespacePackage2]
+# flags: --namespace-packages
+import m
+[file m.py]
+from foo import bar
+bar.x + 0
+[file foo/bar.py]
+x = 0
+[rechecked]
+[stale]
diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test
--- a/test-data/unit/pep561.test
+++ b/test-data/unit/pep561.test
@@ -187,9 +187,8 @@ import a
 [out2]
 a.py:1: error: Unsupported operand types for + ("int" and "str")
 
--- Test for issue #9852, among others
-[case testTypedPkgNamespaceRegFromImportTwice-xfail]
-# pkgs: typedpkg, typedpkg_ns
+[case testTypedPkgNamespaceRegFromImportTwice]
+# pkgs: typedpkg_ns
 from typedpkg_ns import ns
 -- dummy should trigger a second iteration
 [file dummy.py.2]