-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathdataclasses_transform_class.py
More file actions
122 lines (81 loc) · 2.59 KB
/
Copy pathdataclasses_transform_class.py
File metadata and controls
122 lines (81 loc) · 2.59 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Tests the dataclass_transform mechanism when it is applied to a base class.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/dataclasses.html#the-dataclass-transform-decorator
from typing import Any, Generic, TypeVar, dataclass_transform
T = TypeVar("T")
class ModelField:
def __init__(self, *, init: bool = True, default: Any | None = None) -> None:
...
def model_field(
*, init: bool = True, default: Any | None = None, alias: str | None = None
) -> Any:
raise NotImplementedError
@dataclass_transform(
kw_only_default=True,
field_specifiers=(ModelField, model_field),
)
class ModelBase:
not_a_field: str
def __init_subclass__(
cls,
*,
frozen: bool = False,
kw_only: bool = True,
order: bool = True,
) -> None:
...
def __init__(self, not_a_field: str) -> None:
self.not_a_field = not_a_field
class Customer1(ModelBase, frozen=True):
id: int = model_field()
name: str = model_field()
name2: str = model_field(alias="other_name", default="None")
# This should generate an error because a non-frozen dataclass cannot
# derive from a frozen one.
class Customer1Subclass(Customer1): # E
salary: float = model_field()
class Customer2(ModelBase, order=True):
id: int
name: str = model_field(default="None")
c1_1 = Customer1(id=3, name="Sue", other_name="Susan")
# This should generate an error because the class is frozen.
c1_1.id = 4 # E
# This should generate an error because the class is kw_only.
c1_2 = Customer1(3, "Sue") # E
c1_3 = Customer1(id=3, name="John")
# This should generate an error because comparison methods are
# not synthesized.
v1 = c1_1 < c1_2 # E
c2_1 = Customer2(id=0, name="John")
c2_2 = Customer2(id=1)
v2 = c2_1 < c2_2
# This should generate an error because Customer2 supports
# keyword-only parameters for its constructor.
c2_3 = Customer2(0, "John") # E
@dataclass_transform(
kw_only_default=True,
field_specifiers=(ModelField, model_field),
)
class GenericModelBase(Generic[T]):
not_a_field: T
def __init_subclass__(
cls,
*,
frozen: bool = False,
kw_only: bool = True,
order: bool = True,
) -> None:
...
class GenericCustomer(GenericModelBase[int]):
id: int = model_field()
gc_1 = GenericCustomer(id=3)
@dataclass_transform(frozen_default=True)
class ModelBaseFrozen:
not_a_field: str
class Customer3(ModelBaseFrozen):
id: int
name: str
c3_1 = Customer3(id=2, name="hi")
# This should generate an error because Customer3 is frozen.
c3_1.id = 4 # E