Use MyPy for inlay type hints
martinkozle opened this issue · 7 comments
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:
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.
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.
It seems not possible at the moment to use dmypy inspect
for inlay hints. It could be used for hovers, however.
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:
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.
Yes, that seems possible.
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?
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?
Pylance seems to be closed source and doesn't support any plugins, something like basedpyright might be an option if you want to integrate this functionality into an existing language server.
VSCode should be able to run multiple language servers at once, e.g Ruff recently implemented a language server.
An extension can provide inlay hints with or without a language server. vscode also supports multiple inlay hints extensions for the same language. In short, there is no limitation on the vscode side. The harder part is pulling this information out of mypy.