summaryrefslogtreecommitdiff
path: root/tests/run/pep557_dataclasses.py
blob: 288c71ed2426c99e384e6e7b7dfb567379253bb4 (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
45
46
47
48
49
50
51
52
53
54
# mode: run
# tag: pep557, pure3.7

import dataclasses
from typing import Sequence


@dataclasses.dataclass
class Color:
    """
    >>> list(Color.__dataclass_fields__.keys())
    ['red', 'green', 'blue', 'alpha']
    >>> Color(1, 2, 3)
    Color(red=1, green=2, blue=3, alpha=255)
    >>> Color(1, 2, 3, 4)
    Color(red=1, green=2, blue=3, alpha=4)
    >>> Color(green=1, blue=2, red=3, alpha=40)
    Color(red=3, green=1, blue=2, alpha=40)
    """
    red: int
    green: int
    blue: int
    alpha: int = 255


@dataclasses.dataclass
class NamedColor(Color):
    """
    >>> list(NamedColor.__dataclass_fields__.keys())
    ['red', 'green', 'blue', 'alpha', 'names']
    >>> NamedColor(1, 2, 3)
    NamedColor(red=1, green=2, blue=3, alpha=255, names=[])
    >>> NamedColor(1, 2, 3, 4)
    NamedColor(red=1, green=2, blue=3, alpha=4, names=[])
    >>> NamedColor(green=1, blue=2, red=3, alpha=40)
    NamedColor(red=3, green=1, blue=2, alpha=40, names=[])
    >>> NamedColor(1, 2, 3, names=["blackish", "very dark cyan"])
    NamedColor(red=1, green=2, blue=3, alpha=255, names=['blackish', 'very dark cyan'])
    """
    names: Sequence[str] = dataclasses.field(default_factory=list)


@dataclasses.dataclass(frozen=True)
class IceCream:
    """
    >>> IceCream("vanilla")
    IceCream(flavour='vanilla', num_toppings=2)
    >>> IceCream("vanilla") == IceCream("vanilla", num_toppings=3)
    False
    >>> IceCream("vanilla") == IceCream("vanilla", num_toppings=2)
    True
    """
    flavour: str
    num_toppings: int = 2