Skip to content

fix(errors): avoid duplicate diagnostics for duplicate paths - #21895

Open
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-duplicate-package-error-21889
Open

fix(errors): avoid duplicate diagnostics for duplicate paths#21895
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-duplicate-package-error-21889

Conversation

@daleselaji-dev

Copy link
Copy Markdown

Problem

When packages and mypy_path discover the same physical Python file under two module names, an ordinary type error in that file can trigger an internal flushed_files assertion instead of a diagnostic.

Root Cause

The same physical file is processed and flushed more than once, while error flush bookkeeping compares path spellings literally. The duplicate module states can therefore report after the first flush, and the second flush can emit the same diagnostic again.

Solution

Normalize flushed paths to absolute, case-insensitive paths and make error collection and message flushing idempotent for a physical file that has already been emitted.

Changes

  • Prevent duplicate error collection when a file is revisited under an equivalent path spelling.
  • Prevent a second message flush for the same physical file.
  • Add a command-line regression covering duplicate package roots plus an ordinary type error.

Testing

  • PYTHONUTF8=1 python -m pytest -n0 mypy/test/testcmdline.py -k DuplicatePackageRoots — passed (1 selected)
  • PYTHONUTF8=1 python -m pytest -n0 mypy/test/testcmdline.py — passed (146/146)
  • python -m mypy --config-file mypy_self_check.ini -p mypy — passed (196 source files)
  • python -m black --check mypy/errors.py — passed
  • python -m ruff check mypy/errors.py — passed
  • python -m compileall -q mypy/errors.py — passed
  • git diff --check — passed
  • pre-commit — not verified: first-time actionlint environment setup returned BadZipFile before hooks completed

Compatibility/Risk

This only affects diagnostics for a physical file that is revisited during one build. It preserves the first diagnostic and avoids an internal crash or duplicate output; normal files continue to use the existing error collection flow. No public API or dependency changed.

Notes for Reviewer

The regression fails on the parent commit with AssertionError in Errors._add_error_info. The final test reports the expected arg-type diagnostic once. The test uses the same packages + overlapping mypy_path shape described in the issue and does not require external packages.

Linked Issue

Fixes #21889

@daleselaji-dev
daleselaji-dev marked this pull request as ready for review August 27, 2026 03:21
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when packages and mypy_path discover one file under two module names

1 participant