matangover / mypy-vscode

VS Code extension that provides type checking and analysis for Python code using mypy.
https://marketplace.visualstudio.com/items?itemName=matangover.mypy
Other
90 stars 16 forks source link

Use MyPy for inlay type hints #69

Open martinkozle opened 1 year ago

martinkozle commented 1 year ago

Is it possible on a technical level for this extension to overwrite the inlay type hints from Pylance (Pyright) with ones from MyPy? If so, would you consider this to be a feature worth implementing?

The reason why I would like this feature personally is because I use MyPy for typing but VS Code's Python language server, Pylance, shows hover and inlay hints from Pyright, which is a lot of the time different from MyPy. This makes it confusing when trying to keep the code MyPy valid. An example where this is an issue is:

image
image

The inlay hint shows the type revealed from Pylance, which when typing with MyPy in mind gives the wrong impression of the type.

I was considering posting this feature request directly to the Pylance repository, but since Pylance's whole point is that it is based on it's own type checker I thought that this extension implementing it would be a better fit.

matangover commented 1 year ago

Hi, thanks for your suggestion. This is maybe possible to implement using a feature that mypy added recently - dmypy inspect. However I don't think I'll have time to do that. Contributions are welcome.

matangover commented 1 year ago

It seems not possible at the moment to use dmypy inspect for inlay hints. It could be used for hovers, however.

martinkozle commented 1 year ago

We looked at this with a friend a while ago and came to the same conclusion. Inlay hints are done through the language server, which in this case is Pylance. And we can't really change what Pylance shows in the inlay hints. But I think the hover type hints would be even better and more helpful anyways. The rust-analyzer extension for Rust has them very well implemented where if you select any piece of code it gives you the type of the selected piece of code. Example image: image An example in Python would be if you partially or fully select len(lst) in the hover it would give you the type int. I think this would be quite useful of a feature for Python, but I don't know if dmypy is able to inspect and give type hints for selected bits of code.

matangover commented 1 year ago

Yes, that seems possible.

benjamin-kirkbride commented 4 months ago

Inlay hints are done through the language server, which in this case is Pylance

Does vscode support multiple language servers at once? Alternatively, does Pylance support plugins?