Skip to content

Commit

Permalink
Fixed a bug that results in a false positive error when assigning a t…
Browse files Browse the repository at this point in the history
…uple of indeterminate length to a zero-length tuple target. This addresses #9866. (#9867)
  • Loading branch information
erictraut authored Feb 10, 2025
1 parent 131f56d commit ed53f3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3873,6 +3873,11 @@ export function createTypeEvaluator(
sourceEntryTypes.splice(unboundedIndex, 0, typeToReplicate);
}
}

if (sourceEntryTypes.length > targetTypes.length) {
// Remove elements to make the count match the target count.
sourceEntryTypes.splice(unboundedIndex, 1);
}
}

// If there's an unpack operator in the target and we have too many source elements,
Expand Down
10 changes: 10 additions & 0 deletions packages/pyright-internal/src/tests/samples/tuple6.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ def func1(p1: tuple[str, ...]):
a, b = p1

c, d, *f = p1


def func2(p1: tuple[str, ...], p2: tuple[str, *tuple[str, ...]]):
() = p1
(_,) = p1
(_, _) = p1

# This should generate an error.
() = p2
(_,) = p2
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator8.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ test('Tuple5', () => {
test('Tuple6', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['tuple6.py']);

TestUtils.validateResults(analysisResults, 9);
TestUtils.validateResults(analysisResults, 10);
});

test('Tuple7', () => {
Expand Down

0 comments on commit ed53f3e

Please sign in to comment.