Fix printExpression output for slice expressions and set displays - #11598
Open
Henry Su (hsusul) wants to merge 1 commit into
Open
Fix printExpression output for slice expressions and set displays#11598Henry Su (hsusul) wants to merge 1 commit into
Henry Su (hsusul) wants to merge 1 commit into
Conversation
printExpression rendered "x[1:]" as "x[1]", "x[::2]" as "x[: 2]" and
"x[::-1]" as "x[: -1]" because the colon separating the end and step
values was emitted as a prefix of those sub-expressions rather than as a
separator, so an omitted end or step value swallowed it. It also printed
a set display without its enclosing braces, so "{1, 2}" read as a tuple.
Because printExpression drives reveal_type messages, hover tooltips for
parameter default values and completion labels, these expressions were
reported to users under a name that parses as a different expression.
Failure classification: (B) Pyright limitation. No typeshed change is
involved and no test expectations were relaxed; type precision is
unaffected, since only the textual rendering of an expression changes.
Collaborator
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
Rich Chiodo (rchiodo)
approved these changes
Aug 6, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Stella Huang (StellaHuang95)
approved these changes
Aug 6, 2026
Stella Huang (StellaHuang95)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Collaborator
|
🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR. |
Rich Chiodo (rchiodo)
approved these changes
Aug 10, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Rich Chiodo (rchiodo)
approved these changes
Aug 11, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Stella Huang (StellaHuang95)
approved these changes
Aug 13, 2026
Stella Huang (StellaHuang95)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ParseTreeUtils.printExpressionrenders slice expressions and set literals as text that means something different from the source. The most visible symptom isreveal_typeoutput, which is also used for hover text (tooltipUtilsprints parameter default values) and completion-generated signatures.This is the same class of defect as #6345 (
reveal_type(not y)printing"noty"), but here the printed text is not merely misformatted — it parses as a different expression.Reproduction
Current behavior (1.1.411)
x[1:]is reported asx[1](a slice printed as an index),x[::2]asx[:2],x[::-1]asx[:-1], andx[1::2]asx[1:2]— every one of these names a different expression than the one in the source. A set display is printed without its braces, so{1, 2}reads as a tuple.Corrected behavior
Root cause
packages/pyright-internal/src/analyzer/parseTreeUtils.tsendValueswallowed the colon that separates it fromstepValue, and an omittedendValueandstepValuedropped the trailing colon entirely.Implementation
The slice case now always emits the first colon, emits
startValuebefore it andendValueafter it when present, and emits a second colon only whenstepValueis present. This preserves the existing (correct) rendering ofx[:],x[:1],x[1:2]andx[1:2:3], and it collapsesx[::]tox[:]andx[:2:]tox[:2], which are the same slice.The incidental space that used to follow each colon is dropped, so the output matches how slices are written in source (and in PEP 8). Keeping it would have produced
x[: : -1]forx[::-1].The set case wraps the joined elements in
{}.No behavior outside
printExpressionchanges.Regression coverage
packages/pyright-internal/src/tests/parseTreeUtils.test.tsgains two tests next to the existingprintExpressiontest, covering the eight slice shapes (x[:],x[1:],x[:1],x[1:2],x[::2],x[1::2],x[::-1],x[1:2:3]) and single- and multi-element set displays.Both fail on
main:Validation
Run from a clean worktree at
dde0aae(main), Node 24.9.0:npx jest parseTreeUtils.test --forceExit(before fix)2 failed, 1 passed— the two new testsnpx jest parseTreeUtils.test --forceExit(after fix)21 passed, 21 totalnpm test(packages/pyright-internal)62 suites passed, 2551 tests passed, 200 snpm run check(syncpack + eslint + prettier)npm run build:cli:dev)git diff --checkCompatibility
printExpressionoutput is not part of the public API; it feedsreveal_typemessages, hover tooltips, completion labels and internal tracing. Existing test expectations for slice or set text: none (the only occurrence ofa[: -1]in the repo is a tokenizer input string, not an expected output).