forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pep646_syntax.py
158 lines (135 loc) · 3.58 KB
/
test_pep646_syntax.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
doctests = """
Setup
>>> class A:
... def __class_getitem__(cls, x):
... return ParameterisedA(x)
>>> class ParameterisedA:
... def __init__(self, params):
... self._params = params
... def __repr__(self):
... return f"A[{self._params}]"
... def __iter__(self):
... for p in self._params:
... yield p
>>> class B:
... def __iter__(self):
... yield StarredB()
... def __repr__(self):
... return "B"
>>> class StarredB:
... def __repr__(self):
... return "StarredB"
>>> b = B()
Slices that are supposed to work, starring our custom B class
>>> A[*b]
A[(StarredB,)]
>>> A[*b, *b]
A[(StarredB, StarredB)]
>>> A[b, *b]
A[(B, StarredB)]
>>> A[*b, b]
A[(StarredB, B)]
>>> A[b, b, *b]
A[(B, B, StarredB)]
>>> A[*b, b, b]
A[(StarredB, B, B)]
>>> A[b, *b, b]
A[(B, StarredB, B)]
>>> A[b, b, *b, b]
A[(B, B, StarredB, B)]
>>> A[b, *b, b, b]
A[(B, StarredB, B, B)]
>>> A[A[b, *b, b]]
A[A[(B, StarredB, B)]]
>>> A[*A[b, *b, b]]
A[(B, StarredB, B)]
>>> A[b, ...]
A[(B, Ellipsis)]
>>> A[*A[b, ...]]
A[(B, Ellipsis)]
Slices that are supposed to work, starring a list
>>> l = [1, 2, 3]
>>> A[*l]
A[(1, 2, 3)]
>>> A[*l, 4]
A[(1, 2, 3, 4)]
>>> A[0, *l]
A[(0, 1, 2, 3)]
>>> A[1:2, *l]
A[(slice(1, 2, None), 1, 2, 3)]
>>> repr(A[1:2, *l]) == repr(A[1:2, 1, 2, 3])
True
Slices that are supposed to work, starring a tuple
>>> t = (1, 2, 3)
>>> A[*t]
A[(1, 2, 3)]
>>> A[*t, 4]
A[(1, 2, 3, 4)]
>>> A[0, *t]
A[(0, 1, 2, 3)]
>>> A[1:2, *t]
A[(slice(1, 2, None), 1, 2, 3)]
>>> repr(A[1:2, *t]) == repr(A[1:2, 1, 2, 3])
True
Starring an expression (rather than a name) in a slice
>>> def returns_list():
... return [1, 2, 3]
>>> A[returns_list()]
A[[1, 2, 3]]
>>> A[returns_list(), 4]
A[([1, 2, 3], 4)]
>>> A[*returns_list()]
A[(1, 2, 3)]
>>> A[*returns_list(), 4]
A[(1, 2, 3, 4)]
>>> A[0, *returns_list()]
A[(0, 1, 2, 3)]
>>> A[*returns_list(), *returns_list()]
A[(1, 2, 3, 1, 2, 3)]
Slices that should fail
>>> A[:*b]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
>>> A[*b:]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
>>> A[*b:*b]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
>>> A[**b]
Traceback (most recent call last):
...
SyntaxError: invalid syntax
*args annotated as starred expression
>>> def f1(*args: *b): pass
>>> f1.__annotations__
{'args': StarredB}
>>> def f2(*args: *b, arg1): pass
>>> f2.__annotations__
{'args': StarredB}
>>> def f3(*args: *b, arg1: int): pass
>>> f3.__annotations__
{'args': StarredB, 'arg1': <class 'int'>}
>>> def f4(*args: *b, arg1: int = 2): pass
>>> f4.__annotations__
{'args': StarredB, 'arg1': <class 'int'>}
Other uses of starred expressions as annotations should fail
>>> def f6(x: *b): pass
Traceback (most recent call last):
...
SyntaxError: invalid syntax
>>> x: *b
Traceback (most recent call last):
...
SyntaxError: invalid syntax
"""
__test__ = {'doctests' : doctests}
def test_main(verbose=False):
from test import support
from test import test_pep646_syntax
support.run_doctest(test_pep646_syntax, verbose)
if __name__ == "__main__":
test_main(verbose=True)