Skip to content

KeyError: 'deprecated' because of bad cache #18473

Open

Description

@injust

Crash Report

I ran mypy in my venv and the cache somehow got into a bad state, leading to crashes until the cache was removed. I don't know what triggered this. If it matters, the mypy that caused the bad cache was installed with faster-cache.

Traceback

Traceback (most recent call last):
  File "[...]/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File "[...]/.venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "mypy/main.py", line 109, in main
  File "mypy/main.py", line 193, in run_build
  File "mypy/build.py", line 194, in build
  File "mypy/build.py", line 269, in _build
  File "mypy/build.py", line 2940, in dispatch
  File "mypy/build.py", line 3331, in process_graph
  File "mypy/build.py", line 3409, in process_fresh_modules
  File "mypy/build.py", line 2096, in load_tree
  File "mypy/nodes.py", line 402, in deserialize
  File "mypy/nodes.py", line 4058, in deserialize
  File "mypy/nodes.py", line 3999, in deserialize
  File "mypy/nodes.py", line 263, in deserialize
  File "mypy/nodes.py", line 3420, in deserialize
  File "mypy/nodes.py", line 4058, in deserialize
  File "mypy/nodes.py", line 3999, in deserialize
  File "mypy/nodes.py", line 263, in deserialize
  File "mypy/nodes.py", line 952, in deserialize
  File "mypy/nodes.py", line 879, in deserialize
KeyError: 'deprecated'

To Reproduce

repro.zip

unzip repro.zip
cd repro/
uv venv
source .venv/bin/activate.fish
uv pip install -r requirements.txt
mypy foo.py

Your Environment

  • Mypy version used: 1.14.1 (compiled: yes)
  • Mypy configuration options from mypy.ini (and other config files): strict = true
  • Python version used: 3.13.1
  • Operating system and version: macOS Sequoia 15.2

Activity

  1. ncoghlan commented on Jan 27, 2025

    @ncoghlan

    Workaround if anyone else runs into this: pass --no-incremental to a mypy run and the full run will fix up the corrupted cache.

  2. harith-mindway commented on Feb 24, 2025

    @harith-mindway

    Facing the same issue here.
    @ncoghlan thanks for the suggestion, though I get an error when passing --no-incremental - probably specific to my config:
    error: --install-types not supported with incremental mode disabled

    I am using mypy through pre-commit, if anyone else has a similar config, would appreciate any workarounds! Thanks

  3. injust commented on Feb 24, 2025

    @injust
    Author

    I am using mypy through pre-commit, if anyone else has a similar config, would appreciate any workarounds! Thanks

    I also happen to be using mypy through pre-commit 🤔

  4. tyralla commented on Jun 14, 2025

    @tyralla
    Collaborator

    Thanks for providing the repro, @injust. I experimented with it but cannot reproduce the crash. The only difference that I am aware of is that I am working on Windows.

    The deprecated decorator has been supported since Mypy 1.14. For each function definition (FuncDef), an attribute called "deprecated" (that handles the deprecation message or is None/null) should be available in the cache. However, this is not the case for any of the function definitions stored in tokenize.data.json. I randomly picked another file in the cache ( enum.data.json), and it contains information on "deprecated" for the function definitions I checked.

    The data_mtime information of all cached files:

    {'1736325097': ['_frozen_importlib', '_frozen_importlib_external', '_io'],
     '1736939877': ['tokenize'],
     '1736939880': ['token'],
     '1736940420': ['abc',
                    'ast',
                    'builtins',
                    'codecs',
                    'contextlib',
                    'dataclasses',
                    'enum',
                    'genericpath',
                    'io',
                    'pathlib',
                    'posixpath',
                    're',
                    'resource',
                    'sre_compile',
                    'sre_constants',
                    'sre_parse',
                    'subprocess',
                    'types',
                    'typing',
                    'typing_extensions',
                    'warnings',
                    '_ast',
                    '_codecs',
                    '_collections_abc',
                    '_warnings']}
    

    I cannot imagine a way how the serialisation of a function definition could be incomplete in such a way because the related method returns either a complete dictionary or nothing:

    mypy/mypy/nodes.py

    Lines 860 to 876 in 5081c59

    return {
    ".class": "FuncDef",
    "name": self._name,
    "fullname": self._fullname,
    "arg_names": self.arg_names,
    "arg_kinds": [int(x.value) for x in self.arg_kinds],
    "type": None if self.type is None else self.type.serialize(),
    "flags": get_flags(self, FUNCDEF_FLAGS),
    "abstract_status": self.abstract_status,
    # TODO: Do we need expanded, original_def?
    "dataclass_transform_spec": (
    None
    if self.dataclass_transform_spec is None
    else self.dataclass_transform_spec.serialize()
    ),
    "deprecated": self.deprecated,
    }

    It's hard to tell what went wrong for me. My only idea is that tokenize.data.json could have been created by an older Mypy version and was not updated when a newer one was applied. Not very likely, I guess.

    Are there any hints on how the cash became corrupted?

    (@hauntsaninja: Do you think something like this could happen when switching between versions with fast-cache enabled and disabled?)

  5. injust commented on Jun 14, 2025

    @injust
    Author

    I (used to) run mypy through the command-line (installed in my venv) and also through pre-commit (as a commit hook or pre-commit run -a), but I'm not sure running through pre-commit uses/modifies the same .mypy_cache/ directory.

    Perhaps using both and/or their versions being out of sync could have caused this problem?

  6. tyralla commented on Jun 14, 2025

    @tyralla
    Collaborator

    Maybe. I don't know.

    Does the crash still occur with Mypy 1.15 and 1.16?

  7. injust commented on Jun 14, 2025

    @injust
    Author

    Does the crash still occur with Mypy 1.15 and 1.16?

    No, at least I haven't run into this crash since I reported it.

  8. tyralla commented on Jun 14, 2025

    @tyralla
    Collaborator

    @ncoghlan @harith-mindway @tomasvotava @victorbadenas

    Can you confirm the problem went away for (unknown) reasons? I think we could close this issue, then.

  9. tomasvotava commented on Jun 14, 2025

    @tomasvotava

    Well the problem still appears from time to time, but deleting .mypy_cache always helps.

  10. tyralla commented on Jun 14, 2025

    @tyralla
    Collaborator

    And it is always because of KeyError: 'deprecated'? (And eventually only happens when analysing tokenize or other specific modules?)

  11. victorbadenas commented on Jun 25, 2025

    @victorbadenas