Debian Codegraph @

test-data/unit/check-lists.test

raw
1 -- Nested list assignment
2 -- -----------------------------
3
4 [case testNestedListAssignment]
5 from typing import List
6 a1: A
7 a2: A
8 b1: B
9 b2: B
10 c1: C
11 c2: C
12
13 if int():
14 a1, [b1, c1] = a2, [b2, c2]
15 if int():
16 a1, [a1, [b1, c1]] = a2, [a2, [b2, c2]]
17 if int():
18 a1, [a1, [a1, b1]] = a1, [a1, [a1, c1]] # E: Incompatible types in assignment (expression has type "C", variable has type "B")
19
20 class A: pass
21 class B: pass
22 class C: pass
23 [builtins fixtures/list.pyi]
24 [out]
25
26 [case testNestedListAssignmentToTuple]
27 from typing import List
28 a: A
29 b: B
30 c: C
31
32 a, b = [a, b]
33 a, b = [a] # E: Need more than 1 value to unpack (2 expected)
34 a, b = [a, b, c] # E: Too many values to unpack (2 expected, 3 provided)
35
36 class A: pass
37 class B: pass
38 class C: pass
39 [builtins fixtures/list.pyi]
40 [out]
41
42 [case testListAssignmentFromTuple]
43 from typing import List
44 a: A
45 b: B
46 c: C
47 t = a, b
48
49 if int():
50 [a, b], c = t, c
51 if int():
52 [a, c], c = t, c # E: Incompatible types in assignment (expression has type "B", variable has type "C")
53 if int():
54 [a, a, a], c = t, c # E: Need more than 2 values to unpack (3 expected)
55 if int():
56 [a], c = t, c # E: Too many values to unpack (1 expected, 2 provided)
57
58 class A: pass
59 class B: pass
60 class C: pass
61 [builtins fixtures/list.pyi]
62 [out]
63
64 [case testListAssignmentUnequalAmountToUnpack]
65 from typing import List
66 a: A
67 b: B
68 c: C
69
70 def f() -> None: # needed because test parser tries to parse [a, b] as section header
71 [a, b] = [a, b]
72 [a, b] = [a] # E: Need more than 1 value to unpack (2 expected)
73 [a, b] = [a, b, c] # E: Too many values to unpack (2 expected, 3 provided)
74
75 class A: pass
76 class B: pass
77 class C: pass
78 [builtins fixtures/list.pyi]
79 [out]
80
81 [case testListWithStarExpr]
82 (x, *a) = [1, 2, 3]
83 a = [1, *[2, 3]]
84 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
85 b = [0, *a]
86 reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]"
87 c = [*a, 0]
88 reveal_type(c) # N: Revealed type is "builtins.list[builtins.int]"
89 [builtins fixtures/list.pyi]
90
91 [case testComprehensionShadowBinder]
92 def foo(x: object) -> None:
93 if isinstance(x, str):
94 [reveal_type(x) for x in [1, 2, 3]] # N: Revealed type is "builtins.int"
95
96 [builtins fixtures/isinstancelist.pyi]
97
98 [case testUnpackAssignmentWithStarExpr]
99 a: A
100 b: list[B]
101 if int():
102 (a,) = [*b] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
103
104 class A: pass
105 class B: pass