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

Feature: "Add Explicit Type Annotation" code-action #4970

Open
laundmo opened this issue Oct 17, 2023 · 7 comments
Open

Feature: "Add Explicit Type Annotation" code-action #4970

laundmo opened this issue Oct 17, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@laundmo
Copy link

laundmo commented Oct 17, 2023

Sometimes, especially when writing libraries, explicitly annotating types which pylance can usually just infer is wanted.

I propose making a code-action show up whenever the cursor is at a place where a type annotation could be inserted (behind a variable name, between a functions closing parentheses and colon) which inserts the type pylance already knows as a explicit annotation.

| is the cursor

def function()|:
    return "test"

becomes

def function() -> str:
    return "test"
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Oct 17, 2023
@laundmo

This comment was marked as off-topic.

@debonte debonte added enhancement New feature or request and removed needs repro Issue has not been reproduced yet labels Oct 18, 2023
@burcam
Copy link

burcam commented Nov 10, 2023

If I understand what you're looking for, enabling the setting python > analysis > inlay_hints is close. Example:
Screenshot from 2023-11-10 13-33-42

@laundmo
Copy link
Author

laundmo commented Nov 10, 2023

inlay hints is close, yes, but its a result of the type checking, not a input.

writing the type to the file makes sure that if it ever changes, the typechecker would complain. inferring types is great, but the issue is that a different function entirely might change the return type of this one, which i want to catch.

of course, i can just write the type myself, but having a action do it for me is very nice.

or maybe this issue is a reflection of me getting spoilt by the Rust LSP, Rust-Analyzer, which has a LOT of these sorts of conveniences.

@debonte
Copy link
Contributor

debonte commented Nov 10, 2023

inlay hints is close, yes, but its a result of the type checking, not a input.

@laundmo, as the tooltip in the screenshot indicates, you can double-click an inlay hint to write its annotation into the file. Would that be sufficient, or do you feel a code action would be better?

@laundmo
Copy link
Author

laundmo commented Nov 10, 2023

oh, i didn't even notice the tooltip. i MUCH prefer a code action because it dowesn't need the mouse at all.

@bschnurr bschnurr removed their assignment Jan 4, 2024
@alexjbest
Copy link

+1 for having this as a code action in addition to a clickable

@TheVayt
Copy link

TheVayt commented Oct 21, 2024

Looked everywhere to find a keyboard shortcut to insert an inlay hint in vscode without using mouse, couldn't find it. Adding a code action to insert it would work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants