python / python/mypy

dmypy 'KeyError' on recheck

Open
#20,279 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash topic-daemon
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

Running dmypy multiple times results in a KeyError. The error is always on files we exclude in our mypy.ini.

Traceback

[nick@Mac] ~/workspace/mypy-playground [bug-dmypy-key-error]
$ uv run dmypy start -- --show-traceback
Daemon started
[nick@Mac] ~/workspace/mypy-playground [bug-dmypy-key-error]
$ uv run dmypy check src/
src/mypy_playground/example.py:3: error: Name "x" already defined on line 1  [no-redef]
Found 1 error in 1 file (checked 2 source files)
[nick@Mac] ~/workspace/mypy-playground [bug-dmypy-key-error]
$ uv run dmypy recheck
src/mypy_playground/example.py:3: error: Incompatible import of "x" (imported name has type "int", local name has type "str")  [assignment]
Found 1 error in 1 file (checked 61 source files)
[nick@Mac] ~/workspace/mypy-playground [bug-dmypy-key-error]
$ uv run dmypy recheck
Daemon crashed!
Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 237, in serve
  File "mypy/dmypy_server.py", line 286, in run_command
  File "mypy/dmypy_server.py", line 408, in cmd_recheck
  File "mypy/dmypy_server.py", line 629, in fine_grained_increment_follow_imports
  File "mypy/dmypy_server.py", line 768, in find_reachable_changed_modules
KeyError: 'mypy_playground.tests'

To Reproduce
Pushed example source code to https://github.com/njgrisafi/mypy-playground/ on branch bug-dmypy-key-error

See the README https://github.com/njgrisafi/mypy-playground/tree/bug-dmypy-key-error

Your Environment

  • Mypy version used: 1.18.2 & 1.16.0
  • Mypy command-line flags: dmypy start, dmypy check src/ and dmypy recheck
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
check_untyped_defs = True
ignore_missing_imports = True
no_implicit_optional = True
allow_untyped_globals = True
local_partial_types = True
enable_error_code = ignore-without-code

follow_imports = normal
warn_unused_configs = True
warn_redundant_casts = True
no_warn_no_return = True
disallow_untyped_defs = False
disable_error_code =
  has-type,
  annotation-unchecked,
  misc

mypy_path = src
files = src/mypy_playground/

exclude = (?x)(
    \./.*  # dot-prefixed files
    | tests
  ) # (intentional whitespace, otherwise it errors)


[mypy-tests.*,*.tests.*,*.unit_tests.*,*.unit_test.*,*.integration_tests.*,conftest_integration]
ignore_errors = True
follow_imports = skip
  • Python version used: 3.12.11
  • Operating system and version: macOS Tahoe 26.1

I suspect it can be related to https://github.com/python/mypy/pull/18906

relates to https://github.com/python/mypy/issues/18755

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction on the bug-dmypy-key-error branch, then read mypy/dmypy_server.py around cmd_recheck, fine_grained_increment_follow_imports, and find_reachable_changed_modules. Re-run the repeated dmypy recheck sequence with the shown mypy.ini exclusion. Done means the daemon no longer crashes on the excluded tests and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.