-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
typing_example.py
420 lines (287 loc) · 7.69 KB
/
typing_example.py
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# SPDX-License-Identifier: MIT
import re
from typing import Any, Dict, List, Tuple, Union
import attr
import attrs
# Typing via "type" Argument ---
@attr.s
class C:
a = attr.ib(type=int)
c = C(1)
C(a=1)
@attr.s
class D:
x = attr.ib(type=List[int])
@attr.s
class E:
y = attr.ib(type="List[int]")
@attr.s
class F:
z = attr.ib(type=Any)
# Typing via Annotations ---
@attr.s
class CC:
a: int = attr.ib()
cc = CC(1)
CC(a=1)
@attr.s
class DD:
x: List[int] = attr.ib()
@attr.s
class EE:
y: "List[int]" = attr.ib()
@attr.s
class FF:
z: Any = attr.ib()
@attrs.define
class FFF:
z: int
FFF(1)
# Inheritance --
@attr.s
class GG(DD):
y: str = attr.ib()
GG(x=[1], y="foo")
@attr.s
class HH(DD, EE):
z: float = attr.ib()
HH(x=[1], y=[], z=1.1)
# same class
c == cc
# Exceptions
@attr.s(auto_exc=True)
class Error(Exception):
x: int = attr.ib()
try:
raise Error(1)
except Error as e:
e.x
e.args
str(e)
@attrs.define
class Error2(Exception):
x: int
try:
raise Error2(1)
except Error as e:
e.x
e.args
str(e)
# Converters
# XXX: Currently converters can only be functions so none of this works
# although the stubs should be correct.
# @attr.s
# class ConvCOptional:
# x: Optional[int] = attr.ib(converter=attr.converters.optional(int))
# ConvCOptional(1)
# ConvCOptional(None)
# @attr.s
# class ConvCDefaultIfNone:
# x: int = attr.ib(converter=attr.converters.default_if_none(42))
# ConvCDefaultIfNone(1)
# ConvCDefaultIfNone(None)
# @attr.s
# class ConvCToBool:
# x: int = attr.ib(converter=attr.converters.to_bool)
# ConvCToBool(1)
# ConvCToBool(True)
# ConvCToBool("on")
# ConvCToBool("yes")
# ConvCToBool(0)
# ConvCToBool(False)
# ConvCToBool("n")
# Validators
@attr.s
class Validated:
a = attr.ib(
type=List[C],
validator=attr.validators.deep_iterable(
attr.validators.instance_of(C), attr.validators.instance_of(list)
),
)
a = attr.ib(
type=Tuple[C],
validator=attr.validators.deep_iterable(
attr.validators.instance_of(C), attr.validators.instance_of(tuple)
),
)
b = attr.ib(
type=List[C],
validator=attr.validators.deep_iterable(
attr.validators.instance_of(C)
),
)
c = attr.ib(
type=Dict[C, D],
validator=attr.validators.deep_mapping(
attr.validators.instance_of(C),
attr.validators.instance_of(D),
attr.validators.instance_of(dict),
),
)
d = attr.ib(
type=Dict[C, D],
validator=attr.validators.deep_mapping(
attr.validators.instance_of(C), attr.validators.instance_of(D)
),
)
e: str = attr.ib(validator=attr.validators.matches_re(re.compile(r"foo")))
f: str = attr.ib(
validator=attr.validators.matches_re(r"foo", flags=42, func=re.search)
)
# Test different forms of instance_of
g: int = attr.ib(validator=attr.validators.instance_of(int))
h: int = attr.ib(validator=attr.validators.instance_of((int,)))
j: Union[int, str] = attr.ib(
validator=attr.validators.instance_of((int, str))
)
k: Union[int, str, C] = attr.ib(
validator=attrs.validators.instance_of((int, C, str))
)
@attr.define
class Validated2:
num: int = attr.field(validator=attr.validators.ge(0))
@attrs.define
class Validated3:
num: int = attr.field(validator=attr.validators.ge(0))
with attr.validators.disabled():
Validated2(num=-1)
with attrs.validators.disabled():
Validated3(num=-1)
try:
attr.validators.set_disabled(True)
Validated2(num=-1)
finally:
attr.validators.set_disabled(False)
# Custom repr()
@attr.s
class WithCustomRepr:
a: int = attr.ib(repr=True)
b: str = attr.ib(repr=False)
c: str = attr.ib(repr=lambda value: "c is for cookie")
d: bool = attr.ib(repr=str)
@attrs.define
class WithCustomRepr2:
a: int = attrs.field(repr=True)
b: str = attrs.field(repr=False)
c: str = attrs.field(repr=lambda value: "c is for cookie")
d: bool = attrs.field(repr=str)
# Check some of our own types
@attr.s(eq=True, order=False)
class OrderFlags:
a: int = attr.ib(eq=False, order=False)
b: int = attr.ib(eq=True, order=True)
# on_setattr hooks
@attr.s(on_setattr=attr.setters.validate)
class ValidatedSetter:
a: int
b: str = attr.ib(on_setattr=attr.setters.NO_OP)
c: bool = attr.ib(on_setattr=attr.setters.frozen)
d: int = attr.ib(on_setattr=[attr.setters.convert, attr.setters.validate])
e: bool = attr.ib(
on_setattr=attr.setters.pipe(
attr.setters.convert, attr.setters.validate
)
)
@attrs.define(on_setattr=attr.setters.validate)
class ValidatedSetter2:
a: int
b: str = attrs.field(on_setattr=attrs.setters.NO_OP)
c: bool = attrs.field(on_setattr=attrs.setters.frozen)
d: int = attrs.field(
on_setattr=[attrs.setters.convert, attrs.setters.validate]
)
e: bool = attrs.field(
on_setattr=attrs.setters.pipe(
attrs.setters.convert, attrs.setters.validate
)
)
# field_transformer
def ft_hook(cls: type, attribs: List[attr.Attribute]) -> List[attr.Attribute]:
return attribs
# field_transformer
def ft_hook2(
cls: type, attribs: List[attrs.Attribute]
) -> List[attrs.Attribute]:
return attribs
@attr.s(field_transformer=ft_hook)
class TransformedAttrs:
x: int
@attrs.define(field_transformer=ft_hook2)
class TransformedAttrs2:
x: int
# Auto-detect
@attr.s(auto_detect=True)
class AutoDetect:
x: int
def __init__(self, x: int):
self.x = x
# Provisional APIs
@attr.define(order=True)
class NGClass:
x: int = attr.field(default=42)
ngc = NGClass(1)
@attr.mutable(slots=False)
class NGClass2:
x: int
ngc2 = NGClass2(1)
@attr.frozen(str=True)
class NGFrozen:
x: int
ngf = NGFrozen(1)
attr.fields(NGFrozen).x.evolve(eq=False)
a = attr.fields(NGFrozen).x
a.evolve(repr=False)
attrs.fields(NGFrozen).x.evolve(eq=False)
a = attrs.fields(NGFrozen).x
a.evolve(repr=False)
@attr.s(collect_by_mro=True)
class MRO:
pass
@attr.s
class FactoryTest:
a: List[int] = attr.ib(default=attr.Factory(list))
b: List[Any] = attr.ib(default=attr.Factory(list, False))
c: List[int] = attr.ib(default=attr.Factory((lambda s: s.a), True))
@attrs.define
class FactoryTest2:
a: List[int] = attrs.field(default=attrs.Factory(list))
b: List[Any] = attrs.field(default=attrs.Factory(list, False))
c: List[int] = attrs.field(default=attrs.Factory((lambda s: s.a), True))
attrs.asdict(FactoryTest2())
attr.asdict(FactoryTest(), tuple_keys=True)
# Check match_args stub
@attr.s(match_args=False)
class MatchArgs:
a: int = attr.ib()
b: int = attr.ib()
attr.asdict(FactoryTest())
attr.asdict(FactoryTest(), retain_collection_types=False)
# Check match_args stub
@attrs.define(match_args=False)
class MatchArgs2:
a: int
b: int
# NG versions of asdict/astuple
attrs.asdict(MatchArgs2(1, 2))
attrs.astuple(MatchArgs2(1, 2))
def importing_from_attr() -> None:
"""
Use a function to keep the ns clean.
"""
from attr.converters import optional
from attr.exceptions import FrozenError
from attr.filters import include
from attr.setters import frozen
from attr.validators import and_
assert optional and FrozenError and include and frozen and and_
def importing_from_attrs() -> None:
"""
Use a function to keep the ns clean.
"""
from attrs.converters import optional
from attrs.exceptions import FrozenError
from attrs.filters import include
from attrs.setters import frozen
from attrs.validators import and_
assert optional and FrozenError and include and frozen and and_