File: test_py3_syntax.py

package info (click to toggle)
python-ast-decompiler 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: python: 1,738; makefile: 3
file content (337 lines) | stat: -rw-r--r-- 5,031 bytes parent folder | download
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from .tests import check


def test_MatMult() -> None:
    check("a @ b")
    check("(a * b) @ c")
    check("a * (b @ c)")
    check("a + (b @ c)")


def test_AsyncFunctionDef() -> None:
    check(
        """
async def f(a, b):
    pass
"""
    )


def test_async_gen() -> None:
    check(
        """
async def f():
    yield
"""
    )


def test_async_comprehensions() -> None:
    check(
        """
async def f(lst):
    return [await x for x in lst]
"""
    )
    check(
        """
async def f(lst):
    a = [x async for x in lst]
    b = {x async for x in lst}
    c = {x: x async for x in lst}
    d = (x async for x in lst)
"""
    )


def test_function_annotations() -> None:
    check(
        """
def f(a: int, b: str) -> float:
    pass
"""
    )


def test_class_keywords() -> None:
    check(
        """
class Foo(a=3):
    pass
"""
    )
    check(
        """
class WithMeta(metaclass=type):
    pass
"""
    )


def test_annotations() -> None:
    check("def f(a: int, b: int = 0, *args: int, c: int, **kwargs: int): pass")


def test_AsyncFor() -> None:
    check(
        """
async def f(y):
    async for x in y:
        pass
"""
    )


def test_py3_with() -> None:
    check(
        """
with a as b:
    pass
"""
    )
    check(
        """
with a as b, c as d:
    pass
"""
    )
    check(
        """
with a as b:
    with c as d:
        pass
"""
    )


def test_async_with() -> None:
    check(
        """
async def f(a):
    async with a as b:
        pass
"""
    )


def test_raise_with_cause() -> None:
    check(
        """
raise e from ee
"""
    )


def test_Nonlocal() -> None:
    check(
        """
def f(x):
    nonlocal y
"""
    )
    check(
        """
def f(x):
    nonlocal y, z
"""
    )


def test_Await() -> None:
    check(
        """
async def f(x):
    await x
"""
    )
    check(
        """
async def f(x):
    1 + await x
"""
    )
    check(
        """
async def f(x):
    x = await x
"""
    )
    check(
        """
async def f(x):
    x += await x
"""
    )
    check(
        """
async def f(x):
    return 3, (await x)
"""
    )


def test_YieldFrom() -> None:
    check("yield from x")
    check("1 + (yield from x)")
    check("x = yield from x")
    check("x += yield from x")
    check("return 3, (yield from x)")


def test_FormattedValue() -> None:
    check('f"a"')
    check('f"{b}"')
    check('f"{b}a"')
    check('f"{b!r}"')
    check('f"{b:a}"')
    check('f"{b!r:a}"')
    check("f\"{'b'!r:'a'}\"")
    check('f"{a}b{c!a}s"')
    check('f"{a.b}c{d()}"')
    check("f'{1/3:.1f}'")
    check(r"f'{a}\''")
    check("f'{1/3:{5}.1}'")
    check("f'{ {a, b, c} }'")
    check("f'{ {a: b} }'")
    check("f'{ {a for a in b} }'")
    check("f'{ {a: b for a, b in c} }'")
    check(r"f'{a}\n'")
    check(r"f'{a}\t'")
    check("f'{a}é'")
    check('f"{{"')
    check('f"}}"')
    check('f"{{{a}"')


def test_Bytes() -> None:
    check('b"a"')


def test_NameConstant() -> None:
    check("True")


def test_Starred() -> None:
    check("a, *b = 3")
    check("[a, *b]")


def test_kwonlyargs() -> None:
    check("def f(a, *, b): pass")
    check("def f(a, *args, b): pass")
    check("def f(a, *, b=3): pass")
    check("def f(a, *args, b=3): pass")
    check("def f(a, *args, b=3, **kwargs): pass")


def test_annassign() -> None:
    check("a: int")
    check("a: int = 3")
    check("(a): int")
    check(
        """
class A:
    b: int
"""
    )
    check(
        """
def f():
    a: int
"""
    )


def test_future_annotations() -> None:
    # This doesn't really affect ast_decompiler because the __future__
    # import doesn't change the AST.
    check(
        """
from __future__ import annotations

def f(x: int) -> str:
    pass

y: float
"""
    )


def test_async_await_in_fstring() -> None:
    check("f'{await x}'")
    check("f'{[x async for x in y]}'")


def test_too_many_args() -> None:
    args = ", ".join(f"x{i}" for i in range(300))
    check(
        """
def f({}):
    pass

f({})
""".format(
            args, args
        )
    )


def test_finally_continue() -> None:
    check(
        """
def f():
    for x in y:
        try:
            whatever
        finally:
            continue
"""
    )


def test_unpacking() -> None:
    check(
        """
def parse(family):
    lastname, *members = family.split()
    return (lastname.upper(), *members)
"""
    )


def test_unparenthesized_unpacking() -> None:
    check(
        """
def parse(family):
    lastname, *members = family.split()
    return lastname.upper(), *members
"""
    )


def test_assignment_expression() -> None:
    # Some of these can be used unparenthesized in 3.10+ but we don't bother.
    check(
        """
if (x := 3):
    pass
{(y := 4)}
{(z := 5) for a in b}
lst[(alpha := 3)]
lst[(beta := 4):(gamma := 5)]
"""
    )


def test_positional_only() -> None:
    check(
        """
def f(x, /):
    pass
"""
    )


def test_fstring_debug_specifier() -> None:
    check("f'{user=} {member_since=}'")
    check("f'{user=!s}  {delta.days=:,d}'")