File: run-python38.test

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (89 lines) | stat: -rw-r--r-- 2,019 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
-- Test cases for Python 3.8 features

[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([])) == []

[case testFStrings]
from datetime import datetime

def test_fstring_equal_sign() -> None:
    today = datetime(year=2017, month=1, day=27)
    assert f"{today=:%B %d, %Y}" == 'today=January 27, 2017' # using date format specifier and debugging

    foo = "bar"
    assert f"{ foo = }" == " foo = 'bar'" # preserves whitespace

    line = "The mill's closed"
    assert f"{line = }" == 'line = "The mill\'s closed"'
    assert f"{line = :20}" == "line = The mill's closed   "
    assert f"{line = !r:20}" == 'line = "The mill\'s closed" '

[case testMethodOverrideDefaultPosOnly1]
class Foo:
    def f(self, x: int=20, /, *, z: int=10) -> None:
        pass

class Bar(Foo):
    def f(self, *args: int, **kwargs: int) -> None:
        print("stuff", args, kwargs)

def test_pos_only() -> None:
    z: Foo = Bar()
    z.f(1, z=50)
    z.f()
    z.f(1)
    z.f(z=50)

[out]
stuff (1,) {'z': 50}
stuff () {}
stuff (1,) {}
stuff () {'z': 50}