Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/tuples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,11 @@ export function getSlicedTupleType(
const startValue = getTupleSliceParam(evaluator, sliceNode.d.startValue, 0, tupleTypeArgs);
const endValue = getTupleSliceParam(evaluator, sliceNode.d.endValue, tupleTypeArgs.length, tupleTypeArgs);

if (startValue === undefined || endValue === undefined || endValue < startValue) {
if (startValue === undefined || endValue === undefined) {
return undefined;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

t[True:False] and t[2:1:None] still infer an unbounded tuple instead of tuple[()]. Normalize literal booleans as integer bounds, treat an explicit None step like an omitted step, and add regression assertions for both cases.

[verified]


const slicedTypeArgs = tupleTypeArgs.slice(startValue, endValue);
const slicedTypeArgs = startValue < endValue ? tupleTypeArgs.slice(startValue, endValue) : [];
return ClassType.cloneAsInstance(specializeTupleClass(tupleType, slicedTypeArgs));
}

Expand Down
15 changes: 15 additions & 0 deletions packages/pyright-internal/src/tests/samples/tuple20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sample test for tuple slicing with start index >= stop index.

t: tuple[int, str, bool] = (1, "a", True)

# Slicing where start >= stop produces an empty tuple (tuple[()])
# in Python runtime.

a = t[2:1]
reveal_type(a, expected_text="tuple[()]")

b = t[-1:-2]
reveal_type(b, expected_text="tuple[()]")

c = t[5:1]
reveal_type(c, expected_text="tuple[()]")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

tuple13.py already owns tuple slice-expression coverage, including empty and reversed slices. Move these cases there and remove the additional sample registration so the behavior remains specified in one place.

[verified]

6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator8.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ test('Tuple19', () => {
TestUtils.validateResults(analysisResults, 1);
});

test('Tuple20', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['tuple20.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('NamedTuple1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['namedTuple1.py']);

Expand Down
Loading