Skip to content
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

Use MyPy for inlay type hints #69

Open
martinkozle opened this issue Feb 14, 2023 · 7 comments
Open

Use MyPy for inlay type hints #69

martinkozle opened this issue Feb 14, 2023 · 7 comments

Comments

@martinkozle
Copy link

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
Copy link
Owner

matangover commented Feb 14, 2023

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
Copy link
Owner

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

@martinkozle
Copy link
Author

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
Copy link
Owner

Yes, that seems possible.

@benjamin-kirkbride
Copy link

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?

@LaurensBosscher
Copy link

LaurensBosscher commented Aug 26, 2024

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.

@matangover
Copy link
Owner

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.

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

No branches or pull requests

4 participants