Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 5,447 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os

import pytest

import _ast3

# Lowest and highest supported Python 3 minor version (inclusive)
MIN_VER = 4
MAX_VER = 7
NEXT_VER = MAX_VER + 1


basics = """\
def foo():
    # type: () -> int
    pass

def bar():  # type: () -> None
    pass
"""
def test_basics():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(basics, "<basics>", "exec", version)
        assert tree.body[0].type_comment == "() -> int"
        assert tree.body[1].type_comment == "() -> None"


vardecl = """\
a = 0  # type: int
a  # type: int
"""
def test_vardecl():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(vardecl, "<vardecl>", "exec", version)
        assert tree.body[0].type_comment == "int"
        # Curious fact: an expression can have a type comment
        # but it is lost in the AST.


forstmt = """\
for a in []:  # type: int
    pass
"""
def test_forstmt():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(forstmt, "<forstmt>", "exec", version)
        assert tree.body[0].type_comment == "int"


withstmt = """\
with context():  # type: int
    pass
"""
def test_withstmt():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(withstmt, "<withstmt>", "exec", version)
        assert tree.body[0].type_comment == "int"


# A test function named 'fabvk' would have two positional args, a and b,
# plus a var-arg *v, plus a kw-arg **k.  It is verified in test_longargs()
# that it has exactly these arguments, no more, no fewer.
longargs = """\
def fa(
    a = 1,  # type: A
):
    pass

def fa(
    a = 1  # type: A
):
    pass

def fab(
    a,  # type: A
    b,  # type: B
):
    pass

def fab(
    a,  # type: A
    b  # type: B
):
    pass

def fv(
    *v,  # type: V
):
    pass

def fv(
    *v  # type: V
):
    pass

def fk(
    **k,  # type: K
):
    pass

def fk(
    **k  # type: K
):
    pass

def fvk(
    *v,  # type: V
    **k,  # type: K
):
    pass

def fvk(
    *v,  # type: V
    **k  # type: K
):
    pass

def fav(
    a,  # type: A
    *v,  # type: V
):
    pass

def fav(
    a,  # type: A
    *v  # type: V
):
    pass

def fak(
    a,  # type: A
    **k,  # type: K
):
    pass

def fak(
    a,  # type: A
    **k  # type: K
):
    pass

def favk(
    a,  # type: A
    *v,  # type: V
    **k,  # type: K
):
    pass

def favk(
    a,  # type: A
    *v,  # type: V
    **k  # type: K
):
    pass

"""
def test_longargs():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(longargs, "<longargs>", "exec", version)
        for t in tree.body:
            # The expected args are encoded in the function name
            todo = set(t.name[1:])
            assert len(t.args.args) == len(todo) - bool(t.args.vararg) - bool(t.args.kwarg)
            assert t.name.startswith('f')
            for c in t.name[1:]:
                todo.remove(c)
                if c == 'v':
                    arg = t.args.vararg
                elif c == 'k':
                    arg = t.args.kwarg
                else:
                    assert 0 <= ord(c) - ord('a') < len(t.args.args)
                    arg = t.args.args[ord(c) - ord('a')]
                assert arg.arg == c  # That's the argument name
                assert arg.type_comment == arg.arg.upper()
            assert not todo


ignores = """\
def foo():
    pass  # type: ignore

def bar():
    x = 1  # type: ignore
"""
def test_ignores():
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(ignores, "<ignores>", "exec", version)
        assert [ti.lineno for ti in tree.type_ignores] == [2, 5]


# TODO: type comment on new line (currently fails)
asyncfunc = """\
async def foo():  # type: () -> int
    return await bar()
"""
def test_asyncfunc():
    for version in range(3, 5):
        with pytest.raises(SyntaxError):
            _ast3._parse(asyncfunc, "<asyncfunc>", "exec", version)
    for version in range(5, NEXT_VER):
        tree = _ast3._parse(asyncfunc, "<asyncfunc>", "exec", version)
        assert tree.body[0].type_comment == "() -> int"


asyncvar = """\
async = 12
await = 13
"""
def test_asyncvar():
    for version in range(3, 7):
        tree = _ast3._parse(asyncvar, "<asyncvar>", "exec", version)
    for version in range(7, NEXT_VER):
        with pytest.raises(SyntaxError):
            _ast3._parse(asyncvar, "<asyncvar>", "exec", version)


asynccomp = """\
async def foo(xs):
    [x async for x in xs]
"""
def test_asynccomp():
    for version in range(3, 6):
        with pytest.raises(SyntaxError):
            tree = _ast3._parse(asynccomp, "<asynccomp>", "exec", version)
    for version in range(6, NEXT_VER):
            _ast3._parse(asynccomp, "<asynccomp>", "exec", version)


matmul = """\
a = b @ c
"""
def test_matmul():
    for version in range(3, 5):
        with pytest.raises(SyntaxError):
            tree = _ast3._parse(matmul, "<matmul>", "exec", version)
    for version in range(5, NEXT_VER):
        tree = _ast3._parse(matmul, "<matmul>", "exec", version)


strkind = """\
plain = 'abc'
raw = r'abc'
"""
def test_strkind():
    # Test that Str() objects have a kind argument/attribute.
    node = _ast3.Str("foo", "r")
    assert node.s == "foo"
    assert node.kind == "r"
    for version in range(MIN_VER, NEXT_VER):
        tree = _ast3._parse(strkind, "<strkind>", "exec", version)
        assert tree.body[0].value.kind == ""
        assert tree.body[1].value.kind == "r"