Debian Codegraph @

mypyc/test-data/run-python38.test

raw
1 -- Test cases for Python 3.8 features
2
3 [case testWalrus1]
4 from typing import Optional
5
6 def foo(x: int) -> Optional[int]:
7 if x < 0:
8 return None
9 return x
10
11 def test(x: int) -> str:
12 if (n := foo(x)) is not None:
13 return str(x)
14 else:
15 return "<fail>"
16
17 [file driver.py]
18 from native import test
19
20 assert test(10) == "10"
21 assert test(-1) == "<fail>"
22
23
24 [case testWalrus2]
25 from typing import Optional, Tuple, List
26
27 class Node:
28 def __init__(self, val: int, next: Optional['Node']) -> None:
29 self.val = val
30 self.next = next
31
32 def pairs(nobe: Optional[Node]) -> List[Tuple[int, int]]:
33 if nobe is None:
34 return []
35 l = []
36 while next := nobe.next:
37 l.append((nobe.val, next.val))
38 nobe = next
39 return l
40
41 def make(l: List[int]) -> Optional[Node]:
42 cur: Optional[Node] = None
43 for x in reversed(l):
44 cur = Node(x, cur)
45 return cur
46
47 [file driver.py]
48 from native import Node, make, pairs
49
50 assert pairs(make([1,2,3])) == [(1,2), (2,3)]
51 assert pairs(make([1])) == []
52 assert pairs(make([])) == []
53
54 [case testFStrings]
55 from datetime import datetime
56
57 def test_fstring_equal_sign() -> None:
58 today = datetime(year=2017, month=1, day=27)
59 assert f"{today=:%B %d, %Y}" == 'today=January 27, 2017' # using date format specifier and debugging
60
61 foo = "bar"
62 assert f"{ foo = }" == " foo = 'bar'" # preserves whitespace
63
64 line = "The mill's closed"
65 assert f"{line = }" == 'line = "The mill\'s closed"'
66 assert f"{line = :20}" == "line = The mill's closed "
67 assert f"{line = !r:20}" == 'line = "The mill\'s closed" '
68
69 [case testMethodOverrideDefaultPosOnly1]
70 class Foo:
71 def f(self, x: int=20, /, *, z: int=10) -> None:
72 pass
73
74 class Bar(Foo):
75 def f(self, *args: int, **kwargs: int) -> None:
76 print("stuff", args, kwargs)
77
78 def test_pos_only() -> None:
79 z: Foo = Bar()
80 z.f(1, z=50)
81 z.f()
82 z.f(1)
83 z.f(z=50)
84
85 [out]
86 stuff (1,) {'z': 50}
87 stuff () {}
88 stuff (1,) {}
89 stuff () {'z': 50}