8000 Fixed bug that led to inconsistent behaviors when attempting to assig… · sourcegraph/scip-python@ee851cf · GitHub
[go: up one dir, main page]

Skip to content

Commit ee851cf

Browse files
committed
Fixed bug that led to inconsistent behaviors when attempting to assign callable with a tuple parameter where the tuple was of indeterminate length. Previously, this generated an error if the source type parameter was a tuple with zero elements. This addresses microsoft/pyright#4876.
1 parent 09c79ed commit ee851cf

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

packages/pyright-internal/src/analyzer/typeEvaluator.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22310,19 +22310,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
2231022310
}
2231122311
}
2231222312
} else {
22313-
if (srcUnboundedIndex >= 0) {
22314-
// PEP 646 allows an indeterminate tuple type to be assigned to
22315-
// a determinate tuple type if it's associated with a TypeVarTuple.
22316-
if (!destType.isUnpacked) {
22317-
diag?.addMessage(
22318-
Localizer.DiagnosticAddendum.tupleSizeMismatchIndeterminate().format({
22319-
expected: destTypeArgs.length,
22320-
})
22321-
);
22322-
22323-
return false;
22324-
}
22325-
} else {
22313+
if (srcUnboundedIndex < 0) {
2232622314
diag?.addMessage(
2232722315
Localizer.DiagnosticAddendum.tupleSizeMismatch().format({
2232822316
expected: destTypeArgs.length,

packages/pyright-internal/src/localization/localize.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,10 +1257,6 @@ export namespace Localizer {
12571257
new ParameterizedString<{ expected: number; received: number }>(
12581258
getRawString('DiagnosticAddendum.tupleSizeMismatch')
12591259
);
1260-
export const tupleSizeMismatchIndeterminate = () =>
1261-
new ParameterizedString<{ expected: number }>(
1262-
getRawString('DiagnosticAddendum.tupleSizeMismatchIndeterminate')
1263-
);
12641260
export const typeAssignmentMismatch = () =>
12651261
new ParameterizedString<{ sourceType: string; destType: string }>(
12661262
getRawString('DiagnosticAddendum.typeAssignmentMismatch')

packages/pyright-internal/src/localization/package.nls.en-us.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@
645645
"tupleEntryTypeMismatch": "Tuple entry {entry} is incorrect type",
646646
"tupleAssignmentMismatch": "Type \"{type}\" is incompatible with target tuple",
647647
"tupleSizeMismatch": "Element size mismatch; expected {expected} but received {received}",
648-
"tupleSizeMismatchIndeterminate": "Tuple size mismatch; expected {expected} but received indeterminate number",
649648
"typeAssignmentMismatch": "Type \"{sourceType}\" cannot be assigned to type \"{destType}\"",
650649
"typeBound": " 8000 Type \"{sourceType}\" is incompatible with bound type \"{destType}\" for type variable \"{name}\"",
651650
"typeConstrainedTypeVar": "Type \"{type}\" is incompatible with constrained type variable \"{name}\"",

packages/pyright-internal/src/tests/samples/tuples1.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# This sample file tests various aspects of type analysis for tuples.
22

3-
from typing import List, Tuple, Union
43
import os
4+
from typing import Callable
55
from typing_extensions import TypeVarTuple, Unpack
66

77
Ts = TypeVarTuple("Ts")
88

9-
def func1() -> Tuple[int, int, int]:
9+
10+
def func1() -> tuple[int, int, int]:
1011
a = 1, 2, 3
1112

1213
# This should generate an error because
@@ -27,15 +28,15 @@ def func1() -> Tuple[int, int, int]:
2728
return a
2829

2930

30-
def func2() -> Tuple[int, int, str]:
31+
def func2() -> tuple[int, int, str]:
3132
a = 1, 2, 3
3233

3334
# This should generate an error because the
3435
# item types don't match.
3536
return a
3637

3738

38-
def func3() -> Tuple[str, ...]:
39+
def func3() -> tuple[str, ...]:
3940
a = "1", 2, 3
4041

4142
# This should generate an error because the
@@ -44,7 +45,7 @@ def func3() -> Tuple[str, ...]:
4445
return a
4546

4647

47-
def func4() -> Tuple[str, ...]:
48+
def func4() -> tuple[str, ...]:
4849
a = (1,)
4950

5051
# This should generate an error because the first
@@ -57,7 +58,7 @@ def func6():
5758
a.index("1")
5859

5960

60-
def func7(a: Tuple) -> Tuple[()]:
61+
def func7(a: tuple) -> tuple[()]:
6162
return ()
6263

6364

@@ -73,7 +74,7 @@ def func8() -> str:
7374
return dirname
7475

7576

76-
def func9(param1: Tuple[int, ...]):
77+
def func9(param1: tuple[int, ...]):
7778
pass
7879

7980

@@ -127,20 +128,20 @@ def func11() -> float:
127128
def func12():
128129
data = ["a", "b"]
129130
data1 = (*map(str.split, data),)
130-
data2: Tuple[List[str], ...] = (*map(str.split, data),)
131+
data2: tuple[list[str], ...] = (*map(str.split, data),)
131132
data3 = (*map(str.split, data),)
132-
data4: Tuple[List[str], ...] = (*map(str.split, data),)
133+
data4: tuple[list[str], ...] = (*map(str.split, data),)
133134

134135

135136
# Tests for index-out-of-range error.
136137
def func13(
137-
a: Tuple[int, str],
138-
b: Tuple[()],
139-
c: Tuple[int, ...],
140-
d: Union[Tuple[int], Tuple[str, str], Tuple[int, ...]],
141-
e: Tuple[int, Unpack[Tuple[str, ...]], float],
142-
f: Tuple[int, Unpack[Ts], float],
143-
g: Tuple[Unpack[Ts]]
138+
a: tuple[int, str],
139+
b: tuple[()],
140+
c: tuple[int, ...],
141+
d: tuple[int] | tuple[str, str] | tuple[int, ...],
142+
e: tuple[int, Unpack[tuple[str, ...]], float],
143+
f: tuple[int, Unpack[Ts], E499 float],
144+
g: tuple[Unpack[Ts]],
144145
):
145146
v1 = a[0]
146147
reveal_type(v1, expected_text="int")
@@ -189,8 +190,25 @@ def func13(
189190
reveal_type(v15, expected_text="int | Union[*Ts@func13] | float")
190191

191192

192-
# Test for construction using the tuple constructor
193193
def func14():
194194
list1 = [1, 2, 3]
195195
v1 = tuple(list1)
196196
reveal_type(v1, expected_text="tuple[int, ...]")
197+
198+
199+
def func15(var: tuple[()]) -> str:
200+
raise NotImplementedError
201+
202+
203+
def func16(var: tuple[int, int]) -> str:
204+
raise NotImplementedError
205+
206+
207+
def func17(var: tuple[int, int, int]) -> str:
208+
raise NotImplementedError
209+
210+
211+
f: Callable[[tuple[int, ...]], str]
212+
f = func15
213+
f = func16
214+
f = func17

0 commit comments

Comments
 (0)
0