forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-dataclass-transform.test
More file actions
335 lines (262 loc) · 9.78 KB
/
check-dataclass-transform.test
File metadata and controls
335 lines (262 loc) · 9.78 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
[case testDataclassTransformReusesDataclassLogic]
# flags: --python-version 3.11
from typing import dataclass_transform, Type
@dataclass_transform()
def my_dataclass(cls: Type) -> Type:
return cls
@my_dataclass
class Person:
name: str
age: int
def summary(self):
return "%s is %d years old." % (self.name, self.age)
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person"
Person('John', 32)
Person('Jonh', 21, None) # E: Too many arguments for "Person"
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformIsFoundInTypingExtensions]
# flags: --python-version 3.7
from typing import Type
from typing_extensions import dataclass_transform
@dataclass_transform()
def my_dataclass(cls: Type) -> Type:
return cls
@my_dataclass
class Person:
name: str
age: int
def summary(self):
return "%s is %d years old." % (self.name, self.age)
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person"
Person('John', 32)
Person('Jonh', 21, None) # E: Too many arguments for "Person"
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformParametersAreApplied]
# flags: --python-version 3.11
from typing import dataclass_transform, Callable, Type
@dataclass_transform()
def my_dataclass(*, eq: bool, order: bool) -> Callable[[Type], Type]:
def transform(cls: Type) -> Type:
return cls
return transform
@my_dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True
class Person:
name: str
age: int
reveal_type(Person) # N: Revealed type is "def (name: builtins.str, age: builtins.int) -> __main__.Person"
Person('John', 32)
Person('John', 21, None) # E: Too many arguments for "Person"
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformParametersMustBeBoolLiterals]
# flags: --python-version 3.11
from typing import dataclass_transform, Callable, Type
@dataclass_transform()
def my_dataclass(*, eq: bool = True, order: bool = False) -> Callable[[Type], Type]:
def transform(cls: Type) -> Type:
return cls
return transform
@dataclass_transform()
class BaseClass:
def __init_subclass__(cls, *, eq: bool): ...
@dataclass_transform()
class Metaclass(type): ...
BOOL_CONSTANT = True
@my_dataclass(eq=BOOL_CONSTANT) # E: "eq" argument must be a True or False literal
class A: ...
@my_dataclass(order=not False) # E: "order" argument must be a True or False literal
class B: ...
class C(BaseClass, eq=BOOL_CONSTANT): ... # E: "eq" argument must be a True or False literal
class D(metaclass=Metaclass, order=not False): ... # E: "order" argument must be a True or False literal
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformDefaultParamsMustBeLiterals]
# flags: --python-version 3.11
from typing import dataclass_transform, Type, Final
BOOLEAN_CONSTANT = True
FINAL_BOOLEAN: Final = True
@dataclass_transform(eq_default=BOOLEAN_CONSTANT) # E: "eq_default" argument must be a True or False literal
def foo(cls: Type) -> Type:
return cls
@dataclass_transform(eq_default=(not True)) # E: "eq_default" argument must be a True or False literal
def bar(cls: Type) -> Type:
return cls
@dataclass_transform(eq_default=FINAL_BOOLEAN) # E: "eq_default" argument must be a True or False literal
def baz(cls: Type) -> Type:
return cls
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformUnrecognizedParamsAreErrors]
# flags: --python-version 3.11
from typing import dataclass_transform, Type
BOOLEAN_CONSTANT = True
@dataclass_transform(nonexistant=True) # E: Unrecognized dataclass_transform parameter "nonexistant"
def foo(cls: Type) -> Type:
return cls
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformDefaultParams]
# flags: --python-version 3.11
from typing import dataclass_transform, Type, Callable
@dataclass_transform(eq_default=False)
def no_eq(*, order: bool = False) -> Callable[[Type], Type]:
return lambda cls: cls
@no_eq()
class Foo: ...
@no_eq(order=True) # E: "eq" must be True if "order" is True
class Bar: ...
@dataclass_transform(kw_only_default=True)
def always_use_kw(cls: Type) -> Type:
return cls
@always_use_kw
class Baz:
x: int
Baz(x=5)
Baz(5) # E: Too many positional arguments for "Baz"
@dataclass_transform(order_default=True)
def ordered(*, eq: bool = True) -> Callable[[Type], Type]:
return lambda cls: cls
@ordered()
class A:
x: int
A(1) > A(2)
@dataclass_transform(frozen_default=True)
def frozen(cls: Type) -> Type:
return cls
@frozen
class B:
x: int
b = B(x=1)
b.x = 2 # E: Property "x" defined in "B" is read-only
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformDefaultsCanBeOverridden]
# flags: --python-version 3.11
from typing import dataclass_transform, Callable, Type
@dataclass_transform(kw_only_default=True)
def my_dataclass(*, kw_only: bool = True) -> Callable[[Type], Type]:
return lambda cls: cls
@my_dataclass()
class KwOnly:
x: int
@my_dataclass(kw_only=False)
class KwOptional:
x: int
KwOnly(5) # E: Too many positional arguments for "KwOnly"
KwOptional(5)
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformFieldSpecifiersDefaultsToEmpty]
# flags: --python-version 3.11
from dataclasses import field, dataclass
from typing import dataclass_transform, Type
@dataclass_transform()
def my_dataclass(cls: Type) -> Type:
return cls
@my_dataclass
class Foo:
foo: int = field(kw_only=True)
# Does not cause a type error because `dataclasses.field` is not a recognized field specifier by
# default
Foo(5)
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformOverloadsDecoratorOnOverload]
# flags: --python-version 3.11
from typing import dataclass_transform, overload, Any, Callable, Type, Literal
@overload
def my_dataclass(*, foo: str) -> Callable[[Type], Type]: ...
@overload
@dataclass_transform(frozen_default=True)
def my_dataclass(*, foo: int) -> Callable[[Type], Type]: ...
def my_dataclass(*, foo: Any) -> Callable[[Type], Type]:
return lambda cls: cls
@my_dataclass(foo="hello")
class A:
a: int
@my_dataclass(foo=5)
class B:
b: int
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A"
reveal_type(B) # N: Revealed type is "def (b: builtins.int) -> __main__.B"
A(1, "hello") # E: Too many arguments for "A"
a = A(1)
a.a = 2 # E: Property "a" defined in "A" is read-only
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformOverloadsDecoratorOnImpl]
# flags: --python-version 3.11
from typing import dataclass_transform, overload, Any, Callable, Type, Literal
@overload
def my_dataclass(*, foo: str) -> Callable[[Type], Type]: ...
@overload
def my_dataclass(*, foo: int) -> Callable[[Type], Type]: ...
@dataclass_transform(frozen_default=True)
def my_dataclass(*, foo: Any) -> Callable[[Type], Type]:
return lambda cls: cls
@my_dataclass(foo="hello")
class A:
a: int
@my_dataclass(foo=5)
class B:
b: int
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A"
reveal_type(B) # N: Revealed type is "def (b: builtins.int) -> __main__.B"
A(1, "hello") # E: Too many arguments for "A"
a = A(1)
a.a = 2 # E: Property "a" defined in "A" is read-only
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformViaBaseClass]
# flags: --python-version 3.11
from typing import dataclass_transform
@dataclass_transform(frozen_default=True)
class Dataclass:
def __init_subclass__(cls, *, kw_only: bool = False): ...
class Person(Dataclass, kw_only=True):
name: str
age: int
reveal_type(Person) # N: Revealed type is "def (*, name: builtins.str, age: builtins.int) -> __main__.Person"
Person('Jonh', 21) # E: Too many positional arguments for "Person"
person = Person(name='John', age=32)
person.name = "John Smith" # E: Property "name" defined in "Person" is read-only
class Contact(Person):
email: str
reveal_type(Contact) # N: Revealed type is "def (email: builtins.str, *, name: builtins.str, age: builtins.int) -> __main__.Contact"
Contact('john@john.com', name='John', age=32)
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformViaMetaclass]
# flags: --python-version 3.11
from typing import dataclass_transform
@dataclass_transform(frozen_default=True)
class Dataclass(type): ...
class Person(metaclass=Dataclass, kw_only=True):
name: str
age: int
reveal_type(Person) # N: Revealed type is "def (*, name: builtins.str, age: builtins.int) -> __main__.Person"
Person('Jonh', 21) # E: Too many positional arguments for "Person"
person = Person(name='John', age=32)
person.name = "John Smith" # E: Property "name" defined in "Person" is read-only
class Contact(Person):
email: str
reveal_type(Contact) # N: Revealed type is "def (email: builtins.str, *, name: builtins.str, age: builtins.int) -> __main__.Contact"
Contact('john@john.com', name='John', age=32)
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]
[case testDataclassTransformViaSubclassOfMetaclass]
# flags: --python-version 3.11
from typing import dataclass_transform
@dataclass_transform(frozen_default=True)
class BaseMeta(type): ...
class SubMeta(BaseMeta): ...
# MyPy does *not* recognize this as a dataclass because the metaclass is not directly decorated with
# dataclass_transform
class Foo(metaclass=SubMeta):
foo: int
reveal_type(Foo) # N: Revealed type is "def () -> __main__.Foo"
Foo(1) # E: Too many arguments for "Foo"
[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]