File: check-lists.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (105 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- Nested list assignment
-- -----------------------------

[case testNestedListAssignment]
from typing import List
a1: A
a2: A
b1: B
b2: B
c1: C
c2: C

if int():
    a1, [b1, c1] = a2, [b2, c2]
if int():
    a1, [a1, [b1, c1]] = a2, [a2, [b2, c2]]
if int():
    a1, [a1, [a1, b1]] = a1, [a1, [a1, c1]]  # E: Incompatible types in assignment (expression has type "C", variable has type "B")

class A: pass
class B: pass
class C: pass
[builtins fixtures/list.pyi]
[out]

[case testNestedListAssignmentToTuple]
from typing import List
a: A
b: B
c: C

a, b = [a, b]
a, b = [a]  # E: Need more than 1 value to unpack (2 expected)
a, b = [a, b, c]  # E: Too many values to unpack (2 expected, 3 provided)

class A: pass
class B: pass
class C: pass
[builtins fixtures/list.pyi]
[out]

[case testListAssignmentFromTuple]
from typing import List
a: A
b: B
c: C
t = a, b

if int():
    [a, b], c = t, c
if int():
    [a, c], c = t, c  # E: Incompatible types in assignment (expression has type "B", variable has type "C")
if int():
    [a, a, a], c = t, c  # E: Need more than 2 values to unpack (3 expected)
if int():
    [a], c = t, c  # E: Too many values to unpack (1 expected, 2 provided)

class A: pass
class B: pass
class C: pass
[builtins fixtures/list.pyi]
[out]

[case testListAssignmentUnequalAmountToUnpack]
from typing import List
a: A
b: B
c: C

def f() -> None: # needed because test parser tries to parse [a, b] as section header
    [a, b] = [a, b]
    [a, b] = [a]  # E: Need more than 1 value to unpack (2 expected)
    [a, b] = [a, b, c]  # E: Too many values to unpack (2 expected, 3 provided)

class A: pass
class B: pass
class C: pass
[builtins fixtures/list.pyi]
[out]

[case testListWithStarExpr]
(x, *a) = [1, 2, 3]
a = [1, *[2, 3]]
reveal_type(a)  # N: Revealed type is "builtins.list[builtins.int]"
b = [0, *a]
reveal_type(b)  # N: Revealed type is "builtins.list[builtins.int]"
c = [*a, 0]
reveal_type(c)  # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]

[case testComprehensionShadowBinder]
def foo(x: object) -> None:
    if isinstance(x, str):
        [reveal_type(x) for x in [1, 2, 3]]  # N: Revealed type is "builtins.int"

[builtins fixtures/isinstancelist.pyi]

[case testUnpackAssignmentWithStarExpr]
a: A
b: list[B]
if int():
    (a,) = [*b]  # E: Incompatible types in assignment (expression has type "B", variable has type "A")

class A: pass
class B: pass