-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_autoret.py
More file actions
72 lines (62 loc) · 2.74 KB
/
test_autoret.py
File metadata and controls
72 lines (62 loc) · 2.74 KB
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
# -*- coding: utf-8 -*-
"""Implicit return statements."""
from ...syntax import macros, test, test_raises # noqa: F401
from ...test.fixtures import session, testset
from ...syntax import macros, autoreturn # noqa: F401, F811
from ...env import env
def runtests():
# - in a function body, any expression "expr" in tail position
# (along any code path) is transformed to "return expr".
# - if/elif/else, try/except/else/finally and "with" blocks are supported.
# - a loop (for/else, while) in tail position is considered to always return None.
# - if you need a loop in tail position to have a return value,
# use an explicit return, or the constructs from unpythonic.fploop.
# - any explicit return statements are left alone, so "return" can be used normally.
with autoreturn:
with testset("basic usage"):
def f():
"I'll just return this"
test[f() == "I'll just return this"]
def f2():
return "I'll just return this" # explicit return, not transformed
test[f2() == "I'll just return this"]
with testset("if, elif, else"):
def g(x):
if x == 1:
"one"
elif x == 2:
"two"
else:
"something else"
test[g(1) == "one"]
test[g(2) == "two"]
test[g(42) == "something else"]
with testset("except, else"):
def h(x):
try:
if x == 1:
raise ValueError("h doesn't like the number 'one'")
except ValueError:
"error" # there's a tail position in each "except" clause
else: # if an "else" clause is present in a try, the tail position of the main path is there
2 * x
test[h(10) == 20]
test[h(1) == "error"]
with testset("except, body of the try"):
def h2(x):
try:
if x == 1:
raise ValueError("h doesn't like the number 'one'")
x # no else clause, the tail position of the main path is in the "try"
except ValueError:
"error" # also in this case there's a tail position in each "except" clause
test[h2(10) == 10]
test[h2(1) == "error"]
with testset("with block"):
def ctx():
with env(x="hi") as e: # just need some context manager for testing, doesn't matter which
e.x # tail position in a with block
test[ctx() == "hi"]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()