Skip to content

Literal autocomplete should take care of existing quote marks #1086

@fangyi-zhou

Description

@fangyi-zhou

Describe the Bug

from typing import Literal

def foo(fruit: Literal["apple", "pear"]):
    ...

foo(''

Python: 3.12

When the cursor is in between the quotes, the auto-complete populates the literals correctly. Then if I select one of the options, the post-completion state becomes

foo(''apple''

which didn't take into account the existing quote marks and is incorrect.

On a side note, the current auto-complete doesn't handle strings with quotes correctly --- which is an orthogonal issue with lower priority.

Relevant code pointer:

fn add_literal_completions_from_type(param_type: &Type, completions: &mut Vec<CompletionItem>) {

Presumably this call wraps the string into another layer of single quotes

label: lit.to_string(),

Lit::Str(x) => write!(f, "'{x}'"),

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AZCAxiVUUADropmGGDphcuABRhKAV2GJBw0eIDaEkKmLFYRgDR0jxGKkpGAugEpEUuh7qFvUqYpUA5AEgFiDqDNBwJOSIIADEdACqEVDCpArq6ADGEbjocL5Ycgq8NKgMAPro6jTYosr42qwMznQAtAB8dHAMlG7onnSUMAzqlANgRgByNXV9dMD4AL5GUiBLoag5EABuMABi0DAUaFh4RGTrQA&version=3.12

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions