python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.29k stars 2.8k forks source link

KeyError: 'type_guard' #11048

Closed doctaphred closed 2 years ago

doctaphred commented 3 years ago

Crash Report

I'm getting a strange traceback with missing source lines, semi-reproducibly, ending with KeyError: 'type_guard'.

Traceback

Traceback (most recent call last):
  File "/Users/frederick/Library/Caches/pypoetry/virtualenvs/totpaas-37QkGiAn-py3.9/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/frederick/Library/Caches/pypoetry/virtualenvs/totpaas-37QkGiAn-py3.9/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 87, in main
  File "mypy/main.py", line 165, in run_build
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 254, in _build
  File "mypy/build.py", line 2697, in dispatch
  File "mypy/build.py", line 3014, in process_graph
  File "mypy/build.py", line 3089, in process_fresh_modules
  File "mypy/build.py", line 1978, in load_tree
  File "mypy/nodes.py", line 324, in deserialize
  File "mypy/nodes.py", line 3158, in deserialize
  File "mypy/nodes.py", line 3101, in deserialize
  File "mypy/nodes.py", line 233, in deserialize
  File "mypy/nodes.py", line 2642, in deserialize
  File "mypy/nodes.py", line 3158, in deserialize
  File "mypy/nodes.py", line 3101, in deserialize
  File "mypy/nodes.py", line 233, in deserialize
  File "mypy/nodes.py", line 715, in deserialize
  File "mypy/types.py", line 118, in deserialize_type
    if bases:
  File "mypy/types.py", line 1298, in deserialize
  File "mypy/types.py", line 118, in deserialize_type
    if bases:
  File "mypy/types.py", line 1817, in deserialize
  File "mypy/types.py", line 118, in deserialize_type
    if bases:
  File "mypy/types.py", line 1311, in deserialize
KeyError: 'type_guard'

Not sure why most source lines are omitted. (--show-traceback doesn't seem to affect this either way.)

To Reproduce

Started using mypy with this project (don't ask). Ran mypy totpaas; got this output:

totpaas/main.py:6: error: Skipping analyzing "mintotp": found module but no type hints or library stubs
totpaas/main.py:6: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 2 source files)

Added # type: ignore to the offending line; re-ran mypy totpaas; got the above traceback.

Re-ran mypy totpaas a third time; got this output:

Success: no issues found in 2 source files

I can semi-reliably reproduce this crash by trashing the .mypy_cache directory, reverting the code, and repeating the process above; but sometimes the crash doesn't happen. Attempts to isolate the issue in a minimal test file have so far been unsuccessful.

Sometimes I can repro the crash by simply running mypy totpaas without either removing the .mypy_cache directory or adding the # type: ignore as described above; and sometimes even following those steps fully does not reliably repro it.

Invoking mypy via python -m pdb -m mypy totpaas reliably prevents the crash.

While encountering this issue, I've also noticed extremely long wait times for mypy and other terminal commands, so something else seems to be going on. Additionally, when opening a new iTerm tab (which I've configured to spawn a shell in the same working directory) from within the project directory, sometimes it does so normally; but other times, the new shell opens in ~/.Trash/totpaas 5.08.20 PM. (I'd previously made a few additional clones of the totpaas repo in a different directory, and had subsequently moved them to the trash; no idea how they're getting confused for each other here, or which component could possibly be confusing them—iTerm? bash? the filesystem? macOS? some cache somewhere?)

So, this might not be a mypy issue at all; but if not, hopefully it'll at least provide some Google food for future debuggers of the same underlying error.

Your Environment

hauntsaninja commented 3 years ago

Can you reproduce on latest master? There have been several improvements in adjacent code since 0.910.

A5rocks commented 2 years ago

I can't reproduce: I made a shell script that would clear cache and follow steps listed here and ran it ~15x -- nothing.

Nonetheless, perhaps this could use a .get instead of indexing.

https://github.com/python/mypy/blob/f497645611b89a42cca7c5b7c81dbc66777cf6f9/mypy/types.py#L1301-L1302

Here's the shell script, for completeness :P (I was on commit 4705576cb0875284de59e92b97044cb08e4c245c of the totpaas repository):

rm -r ./.mypy_cache

mypy totpaas

sed -i 's/from mintotp import hotp/from mintotp import hotp  # type: ignore/' 'totpaas/main.py'

mypy totpaas

git checkout -- totpaas/main.py
hauntsaninja commented 2 years ago

Thanks for checking, A5rocks!!