Skip to content

[pyrefly] Expose inferred type hints as quick fixes#4243

Open
shubhamshettyy wants to merge 1 commit into
facebook:mainfrom
shubhamshettyy:insert-type-annotation-code-action
Open

[pyrefly] Expose inferred type hints as quick fixes#4243
shubhamshettyy wants to merge 1 commit into
facebook:mainfrom
shubhamshettyy:insert-type-annotation-code-action

Conversation

@shubhamshettyy

Copy link
Copy Markdown

Reuse insertable inlay hint edits in the quick-fix menu so users can materialize inferred annotations through code actions instead of double-clicking the rendered hint. Keep the initial behavior scoped to existing type hints while preserving their configuration and safety filters.

Summary

  • Reuse Transaction::inlay_hints from the existing quick-fix handler.
  • Offer “Insert inferred type annotation” for existing variable, class-attribute, and function-return type hints.
  • Reuse the exact label text and insertion position already used by inlay-hint edits.
  • Exclude non-insertable hints, such as annotations on unpacked variables.
  • Exclude call-argument name hints such as value=, since they are not type annotations.
  • Respect the existing inlay-hint configuration for variable and function-return types.
  • Support both regular Python files and notebook cells.

The implementation obtains the configured inlay hints during textDocument/codeAction, filters for safely insertable type hints whose insertion position is covered by the requested range, and converts the matching hint into a CodeActionKind::QUICKFIX containing a zero-width TextEdit.

Fixes #4231

Test Plan

  • Added LSP integration coverage for:

    • Function-return annotations.
    • Variable annotations.
    • Class-attribute annotations.
    • Notebook-cell edits.
    • Non-insertable unpacked variables.
    • Call-argument name hints.
    • Already annotated variables.
    • Code-action kind filtering.
    • Disabled variable and return inlay-hint configuration.
  • Ran:

    cargo test insert_inferred_type_annotation

  • Ran the required formatting and lint checks:

    python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema

Reuse insertable inlay hint edits in the quick-fix menu so users can materialize inferred annotations without targeting the rendered hint. Keep the initial behavior scoped to existing type hints and preserve their configuration and safety filters.
@meta-cla

meta-cla Bot commented Jul 22, 2026

Copy link
Copy Markdown

Hi @shubhamshettyy!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@shubhamshettyy

Copy link
Copy Markdown
Author

Potential Follow-up

To keep this initial change small and appropriate for a good-first issue, it identifies type hints from their existing : and -> label prefixes and matches code-action requests at the hint’s insertion position. A more robust implementation could extend InlayHintData with a semantic kind, such as Type or ParameterName, and a source range describing the declaration associated with the hint. This would avoid depending on display strings and would allow the quick fix to appear when the cursor is anywhere on the variable, attribute, or function declaration rather than only at the insertion point. That shared metadata would live in lsp/wasm/inlay_hints.rs, which contains platform-independent LSP logic used by the native server as well as WebAssembly consumers. I can include this extension in the current PR if maintainers prefer the stronger behavior.

@meta-cla

meta-cla Bot commented Jul 22, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quickfix insert parameter types

2 participants