summaryrefslogtreecommitdiffstats
path: root/lang/python/python-attrs/test.sh
blob: 3b640120b3cd1da0984b085461199eab996d8800 (plain)
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
#!/bin/sh

[ "$1" = python3-attrs ] || exit 0

python3 - <<'EOF'
import attr
import attrs

# Define a class with attrs
@attr.s
class Point:
    x = attr.ib()
    y = attr.ib(default=0)

p = Point(1, 2)
assert p.x == 1
assert p.y == 2

p2 = Point(3)
assert p2.y == 0

# Equality
assert Point(1, 2) == Point(1, 2)
assert Point(1, 2) != Point(1, 3)

# attrs.define (modern API)
@attrs.define
class Circle:
    radius: float
    color: str = "red"

c = Circle(5.0)
assert c.radius == 5.0
assert c.color == "red"

c2 = Circle(radius=3.0, color="blue")
assert c2.color == "blue"

# asdict
d = attr.asdict(p)
assert d == {"x": 1, "y": 2}

print("python-attrs OK")
EOF