forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-python38.test
More file actions
50 lines (39 loc) · 1.02 KB
/
run-python38.test
File metadata and controls
50 lines (39 loc) · 1.02 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
[case testWalrus1]
from typing import Optional
def foo(x: int) -> Optional[int]:
if x < 0:
return None
return x
def test(x: int) -> str:
if (n := foo(x)) is not None:
return str(x)
else:
return "<fail>"
[file driver.py]
from native import test
assert test(10) == "10"
assert test(-1) == "<fail>"
[case testWalrus2]
from typing import Optional, Tuple, List
class Node:
def __init__(self, val: int, next: Optional['Node']) -> None:
self.val = val
self.next = next
def pairs(nobe: Optional[Node]) -> List[Tuple[int, int]]:
if nobe is None:
return []
l = []
while next := nobe.next:
l.append((nobe.val, next.val))
nobe = next
return l
def make(l: List[int]) -> Optional[Node]:
cur: Optional[Node] = None
for x in reversed(l):
cur = Node(x, cur)
return cur
[file driver.py]
from native import Node, make, pairs
assert pairs(make([1,2,3])) == [(1,2), (2,3)]
assert pairs(make([1])) == []
assert pairs(make([])) == []