python-lsp / pylsp-mypy

Mypy plugin for the Python LSP Server.
MIT License
118 stars 35 forks source link

default config causes mypy cache invalidation due to `--follow-imports silent` #81

Open allisonkarlitskaya opened 4 months ago

allisonkarlitskaya commented 4 months ago

The default way that mypy gets invoked is with the arguments --show-error-end --no-error-summary --incremental --follow-imports silent. Most of those are OK, but --follow-imports silent will invalidate the cache if the same value isn't also present in the [tool.mypy] section of pyproject.toml.

The default value for this option is normal.

The cache invalidation causes commandline invocations of mypy to take much longer after having opened the editor, and causes the editor to take longer after having run mypy from the commandline.

It's possible to reproduce this problem doing something like:

rm -r .mypy_cache/
time mypy .  # ~12s (cold cache)
time mypy .  # ~2s (warm cache)
nvim somefile.py  # ...and quit after a few seconds
time mypy .  # ~12s (cold cache again, due to invalidation)

There's two workarounds for this, either (somewhat inelegantly) hack the value back to its default:

[tool.pylsp-mypy]
overrides = [true, '--follow-imports', 'normal']

or just use 'silent' mode for all mypy invocations:

[tool.mypy]
follow_imports = 'silent'

A third option is to give mypy a separate --cache-dir when run from pylsp, but this loses the benefit of sharing the cache between the two invocation modes (which is otherwise substantial).

allisonkarlitskaya commented 4 months ago

I think the solution to this issue is that pylsp-mypy should just stop giving --follow-imports silent: you actually lose some errors this way, even within a single file.

allisonkarlitskaya commented 4 months ago

This would effectively be a revert to https://github.com/tomv564/pyls-mypy/pull/12. cc @eliwe.

Richardk2n commented 4 months ago

Can you give an example for an error that would be lost?

allisonkarlitskaya commented 4 months ago

Can you give an example for an error that would be lost?

After trying to reproduce this, I think I was wrong.

The error in question was several instances of var-annotated, and I saw it reported after removing follow_imports = 'silent' from my config, but the real difference was that I was using dmypy instead of mypy. I'm not sure why dmypy reports an error where mypy didn't, but I've been able to reproduce it that way now.

The reason I got confused is because of the reason I modified the config: follow_imports = 'silent' is incompatible with dmypy.

So, remove one reason for eliminating --follow-imports silent (supposed extra messages) but add another: dmypy compatibility. And indeed, pylsp-mypy won't pass this argument when using dmypy (but then, of course, you lose support for live updates...).