-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Assigning to a slice of a list fails to retain literal values #9564
Comments
The observed behavior is a consequence of pyright's inference behavior for tuples. It retains literal values for "bare" tuple expressions but does not for tuple expressions that are nested within another container like a list expression. Normally, bidirectional type inference would help here, but pyright's bidirectional inference logic doesn't currently handle overloaded Adding bidirectional type inference support for overloaded In the meantime, you can work around this limitation by using a temporary variable with an explicit type declaration. temp: list[tuple[Tag, str]] = [("B", "foo")]
l[0:0] = temp |
Thanks for the quick response, and the workaround! |
from typing import Literal, cast Define the literal typeTag = Literal["A", "B"] Define the list with the correct type annotationl: list[tuple[Tag, str]] = [ Assign to an element (this works fine)l[0] = ("B", "foo") Workaround 1: Explicitly annotate the new items being insertednew_items: list[tuple[Tag, str]] = [("B", "foo")] Workaround 2: Use cast to explicitly set the type for the assigned valuesl[0:0] = [cast(tuple[Tag, str], ("A", "baz"))] Verify that the list retains the expected typeprint(l) # Output: [('A', 'baz'), ('B', 'foo'), ('A', 'foo'), ('B', 'bar'), ('A', 'basz')] |
…nts that are assigning to an index expression that is subscripted by a slice. This addresses #9564.
This is addressed in pyright 1.1.392. |
Describe the bug
Assigning to a slice of a list fails to retain literal values
Code or Screenshots
Code sample in pyright playground
VS Code extension or command-line
Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?
I'm running it as a VS Code extension (the code package on Arch Linux). The version is v1.1.390
The text was updated successfully, but these errors were encountered: