Skip to content

Fix crash in dmypy when packages are removed during dmypy run #19229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

koen-boost
Copy link

At our company we use mypy extensively on quite a large codebase. More often than not, after switching branches dmypy will crash, because it still expects an import of a package although the package is gone in the new branch.

I did not manage to write a unit test for this, but verified it on our codebase and it solved the issue.

Copy link
Contributor

github-actions bot commented Jun 4, 2025

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

return [BuildSource(graph[dep].path, dep, followed=True) for dep in state.dependencies]
except KeyError:
# Dependency not found in graph, it was probably removed while dmypy was running.
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this will result in inconsistent state eventually. Would be really helpful to see a reproducer, but in general something like this (modules changed while (d)mypy is running) is really difficult to support. I can envision a solution that restarts the whole processing upon encountering such condition (and that should be moderately easy to support), we need to be really careful with such patching.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to find a more general fix, but just so it's a 100% clear, is dmypy supposed to support the following use case?

  • I run dmypy run ., and see the results.
  • I do various changes in my codebase (possibly switching branches), which results in different packages being removed or added
  • I run dmypy run . again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely yes, and we have a bunch of tests attesting that. I don't use dmypy personally, but I've made a couple of fixes related to such module structure changes in 1.16. Does your problem reproduce on latest mypy version?

I must have misinterpreted your PR description as "file is deleted while dmypy is running", which is indeed likely unsupported.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested with 1.16.0 and sadly the issue is still there.

Thanks for the feedback, I'll see whether I can make a reproducible test and potentially a better fix.

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.

2 participants