-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
test_annotations.py
676 lines (521 loc) · 17.5 KB
/
test_annotations.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# SPDX-License-Identifier: MIT
"""
Tests for PEP-526 type annotations.
Python 3.6+ only.
"""
import sys
import types
import typing
import pytest
import attr
from attr._make import _is_class_var
from attr.exceptions import UnannotatedAttributeError
def assert_init_annotations(cls, **annotations):
"""
Assert cls.__init__ has the correct annotations.
"""
__tracebackhide__ = True
annotations["return"] = type(None)
assert annotations == typing.get_type_hints(cls.__init__)
class TestAnnotations:
"""
Tests for types derived from variable annotations (PEP-526).
"""
def test_basic_annotations(self):
"""
Sets the `Attribute.type` attr from basic type annotations.
"""
@attr.resolve_types
@attr.s
class C:
x: int = attr.ib()
y = attr.ib(type=str)
z = attr.ib()
assert int is attr.fields(C).x.type
assert str is attr.fields(C).y.type
assert None is attr.fields(C).z.type
assert_init_annotations(C, x=int, y=str)
def test_catches_basic_type_conflict(self):
"""
Raises ValueError if type is specified both ways.
"""
with pytest.raises(ValueError) as e:
@attr.s
class C:
x: int = attr.ib(type=int)
assert (
"Type annotation and type argument cannot both be present",
) == e.value.args
def test_typing_annotations(self):
"""
Sets the `Attribute.type` attr from typing annotations.
"""
@attr.resolve_types
@attr.s
class C:
x: typing.List[int] = attr.ib()
y = attr.ib(type=typing.Optional[str])
assert typing.List[int] is attr.fields(C).x.type
assert typing.Optional[str] is attr.fields(C).y.type
assert_init_annotations(C, x=typing.List[int], y=typing.Optional[str])
def test_only_attrs_annotations_collected(self):
"""
Annotations that aren't set to an attr.ib are ignored.
"""
@attr.resolve_types
@attr.s
class C:
x: typing.List[int] = attr.ib()
y: int
assert 1 == len(attr.fields(C))
assert_init_annotations(C, x=typing.List[int])
@pytest.mark.skipif(
sys.version_info[:2] < (3, 11),
reason="Incompatible behavior on older Pythons",
)
@pytest.mark.parametrize("slots", [True, False])
def test_auto_attribs(self, slots):
"""
If *auto_attribs* is True, bare annotations are collected too.
Defaults work and class variables are ignored.
"""
@attr.s(auto_attribs=True, slots=slots)
class C:
cls_var: typing.ClassVar[int] = 23
a: int
x: typing.List[int] = attr.Factory(list)
y: int = 2
z: int = attr.ib(default=3)
foo: typing.Any = None
i = C(42)
assert "C(a=42, x=[], y=2, z=3, foo=None)" == repr(i)
attr_names = {a.name for a in C.__attrs_attrs__}
assert "a" in attr_names # just double check that the set works
assert "cls_var" not in attr_names
attr.resolve_types(C)
assert int == attr.fields(C).a.type
assert attr.Factory(list) == attr.fields(C).x.default
assert typing.List[int] == attr.fields(C).x.type
assert int == attr.fields(C).y.type
assert 2 == attr.fields(C).y.default
assert int == attr.fields(C).z.type
assert typing.Any == attr.fields(C).foo.type
# Class body is clean.
if slots is False:
with pytest.raises(AttributeError):
C.y
assert 2 == i.y
else:
assert isinstance(C.y, types.MemberDescriptorType)
i.y = 23
assert 23 == i.y
assert_init_annotations(
C,
a=int,
x=typing.List[int],
y=int,
z=int,
foo=typing.Any,
)
@pytest.mark.parametrize("slots", [True, False])
def test_auto_attribs_unannotated(self, slots):
"""
Unannotated `attr.ib`s raise an error.
"""
with pytest.raises(UnannotatedAttributeError) as e:
@attr.s(slots=slots, auto_attribs=True)
class C:
v = attr.ib()
x: int
y = attr.ib()
z: str
assert (
"The following `attr.ib`s lack a type annotation: v, y.",
) == e.value.args
@pytest.mark.parametrize("slots", [True, False])
def test_auto_attribs_subclassing(self, slots):
"""
Attributes from base classes are inherited, it doesn't matter if the
subclass has annotations or not.
Ref #291
"""
@attr.resolve_types
@attr.s(slots=slots, auto_attribs=True)
class A:
a: int = 1
@attr.resolve_types
@attr.s(slots=slots, auto_attribs=True)
class B(A):
b: int = 2
@attr.resolve_types
@attr.s(slots=slots, auto_attribs=True)
class C(A):
pass
assert "B(a=1, b=2)" == repr(B())
assert "C(a=1)" == repr(C())
assert_init_annotations(A, a=int)
assert_init_annotations(B, a=int, b=int)
assert_init_annotations(C, a=int)
def test_converter_annotations(self):
"""
An unannotated attribute with an annotated converter gets its
annotation from the converter.
"""
def int2str(x: int) -> str:
return str(x)
@attr.s
class A:
a = attr.ib(converter=int2str)
assert_init_annotations(A, a=int)
def int2str_(x: int, y: str = ""):
return str(x)
@attr.s
class A:
a = attr.ib(converter=int2str_)
assert_init_annotations(A, a=int)
def test_converter_attrib_annotations(self):
"""
If a converter is provided, an explicit type annotation has no
effect on an attribute's type annotation.
"""
def int2str(x: int) -> str:
return str(x)
@attr.s
class A:
a: str = attr.ib(converter=int2str)
b = attr.ib(converter=int2str, type=str)
assert_init_annotations(A, a=int, b=int)
def test_non_introspectable_converter(self):
"""
A non-introspectable converter doesn't cause a crash.
"""
@attr.s
class A:
a = attr.ib(converter=print)
def test_nullary_converter(self):
"""
A coverter with no arguments doesn't cause a crash.
"""
def noop():
pass
@attr.s
class A:
a = attr.ib(converter=noop)
assert A.__init__.__annotations__ == {"return": None}
def test_pipe(self):
"""
pipe() uses the input annotation of its first argument and the
output annotation of its last argument.
"""
def int2str(x: int) -> str:
return str(x)
def strlen(y: str) -> int:
return len(y)
def identity(z):
return z
assert attr.converters.pipe(int2str).__annotations__ == {
"val": int,
"return": str,
}
assert attr.converters.pipe(int2str, strlen).__annotations__ == {
"val": int,
"return": int,
}
assert attr.converters.pipe(identity, strlen).__annotations__ == {
"return": int
}
assert attr.converters.pipe(int2str, identity).__annotations__ == {
"val": int
}
def int2str_(x: int, y: int = 0) -> str:
return str(x)
assert attr.converters.pipe(int2str_).__annotations__ == {
"val": int,
"return": str,
}
def test_pipe_empty(self):
"""
pipe() with no converters is annotated like the identity.
"""
p = attr.converters.pipe()
assert "val" in p.__annotations__
t = p.__annotations__["val"]
assert isinstance(t, typing.TypeVar)
assert p.__annotations__ == {"val": t, "return": t}
def test_pipe_non_introspectable(self):
"""
pipe() doesn't crash when passed a non-introspectable converter.
"""
assert attr.converters.pipe(print).__annotations__ == {}
def test_pipe_nullary(self):
"""
pipe() doesn't crash when passed a nullary converter.
"""
def noop():
pass
assert attr.converters.pipe(noop).__annotations__ == {}
def test_optional(self):
"""
optional() uses the annotations of the converter it wraps.
"""
def int2str(x: int) -> str:
return str(x)
def int_identity(x: int):
return x
def strify(x) -> str:
return str(x)
def identity(x):
return x
assert attr.converters.optional(int2str).__annotations__ == {
"val": typing.Optional[int],
"return": typing.Optional[str],
}
assert attr.converters.optional(int_identity).__annotations__ == {
"val": typing.Optional[int]
}
assert attr.converters.optional(strify).__annotations__ == {
"return": typing.Optional[str]
}
assert attr.converters.optional(identity).__annotations__ == {}
def int2str_(x: int, y: int = 0) -> str:
return str(x)
assert attr.converters.optional(int2str_).__annotations__ == {
"val": typing.Optional[int],
"return": typing.Optional[str],
}
def test_optional_non_introspectable(self):
"""
optional() doesn't crash when passed a non-introspectable
converter.
"""
assert attr.converters.optional(print).__annotations__ == {}
def test_optional_nullary(self):
"""
optional() doesn't crash when passed a nullary converter.
"""
def noop():
pass
assert attr.converters.optional(noop).__annotations__ == {}
@pytest.mark.skipif(
sys.version_info[:2] < (3, 11),
reason="Incompatible behavior on older Pythons",
)
@pytest.mark.parametrize("slots", [True, False])
def test_annotations_strings(self, slots):
"""
String annotations are passed into __init__ as is.
It fails on 3.6 due to a bug in Python.
"""
import typing as t
from typing import ClassVar
@attr.s(auto_attribs=True, slots=slots)
class C:
cls_var1: "typing.ClassVar[int]" = 23
cls_var2: "ClassVar[int]" = 23
cls_var3: "t.ClassVar[int]" = 23
a: "int"
x: "typing.List[int]" = attr.Factory(list)
y: "int" = 2
z: "int" = attr.ib(default=3)
foo: "typing.Any" = None
attr.resolve_types(C, locals(), globals())
assert_init_annotations(
C,
a=int,
x=typing.List[int],
y=int,
z=int,
foo=typing.Any,
)
@pytest.mark.parametrize("slots", [True, False])
def test_typing_extensions_classvar(self, slots):
"""
If ClassVar is coming from typing_extensions, it is recognized too.
"""
@attr.s(auto_attribs=True, slots=slots)
class C:
cls_var: "typing_extensions.ClassVar" = 23 # noqa
assert_init_annotations(C)
def test_keyword_only_auto_attribs(self):
"""
`kw_only` propagates to attributes defined via `auto_attribs`.
"""
@attr.s(auto_attribs=True, kw_only=True)
class C:
x: int
y: int
with pytest.raises(TypeError):
C(0, 1)
with pytest.raises(TypeError):
C(x=0)
c = C(x=0, y=1)
assert c.x == 0
assert c.y == 1
def test_base_class_variable(self):
"""
Base class' class variables can be overridden with an attribute
without resorting to using an explicit `attr.ib()`.
"""
class Base:
x: int = 42
@attr.s(auto_attribs=True)
class C(Base):
x: int
assert 1 == C(1).x
def test_removes_none_too(self):
"""
Regression test for #523: make sure defaults that are set to None are
removed too.
"""
@attr.s(auto_attribs=True)
class C:
x: int = 42
y: typing.Any = None
with pytest.raises(AttributeError):
C.x
with pytest.raises(AttributeError):
C.y
def test_non_comparable_defaults(self):
"""
Regression test for #585: objects that are not directly comparable
(for example numpy arrays) would cause a crash when used as
default values of an attrs auto-attrib class.
"""
class NonComparable:
def __eq__(self, other):
raise ValueError
@attr.s(auto_attribs=True)
class C:
x: typing.Any = NonComparable()
def test_basic_resolve(self):
"""
Resolve the `Attribute.type` attr from basic type annotations.
Unannotated types are ignored.
"""
@attr.s
class C:
x: "int" = attr.ib()
y = attr.ib(type=str)
z = attr.ib()
attr.resolve_types(C)
assert int is attr.fields(C).x.type
assert str is attr.fields(C).y.type
assert None is attr.fields(C).z.type
@pytest.mark.parametrize("slots", [True, False])
def test_resolve_types_auto_attrib(self, slots):
"""
Types can be resolved even when strings are involved.
"""
@attr.s(slots=slots, auto_attribs=True)
class A:
a: typing.List[int]
b: typing.List["int"]
c: "typing.List[int]"
# Note: I don't have to pass globals and locals here because
# int is a builtin and will be available in any scope.
attr.resolve_types(A)
assert typing.List[int] == attr.fields(A).a.type
assert typing.List[int] == attr.fields(A).b.type
assert typing.List[int] == attr.fields(A).c.type
@pytest.mark.parametrize("slots", [True, False])
def test_resolve_types_decorator(self, slots):
"""
Types can be resolved using it as a decorator.
"""
@attr.resolve_types
@attr.s(slots=slots, auto_attribs=True)
class A:
a: typing.List[int]
b: typing.List["int"]
c: "typing.List[int]"
assert typing.List[int] == attr.fields(A).a.type
assert typing.List[int] == attr.fields(A).b.type
assert typing.List[int] == attr.fields(A).c.type
@pytest.mark.parametrize("slots", [True, False])
def test_self_reference(self, slots):
"""
References to self class using quotes can be resolved.
"""
@attr.s(slots=slots, auto_attribs=True)
class A:
a: "A"
b: typing.Optional["A"] # noqa: will resolve below
attr.resolve_types(A, globals(), locals())
assert A == attr.fields(A).a.type
assert typing.Optional[A] == attr.fields(A).b.type
@pytest.mark.parametrize("slots", [True, False])
def test_forward_reference(self, slots):
"""
Forward references can be resolved.
"""
@attr.s(slots=slots, auto_attribs=True)
class A:
a: typing.List["B"] # noqa: will resolve below
@attr.s(slots=slots, auto_attribs=True)
class B:
a: A
attr.resolve_types(A, globals(), locals())
attr.resolve_types(B, globals(), locals())
assert typing.List[B] == attr.fields(A).a.type
assert A == attr.fields(B).a.type
assert typing.List[B] == attr.fields(A).a.type
assert A == attr.fields(B).a.type
def test_init_type_hints(self):
"""
Forward references in __init__ can be automatically resolved.
"""
@attr.s
class C:
x = attr.ib(type="typing.List[int]")
assert_init_annotations(C, x=typing.List[int])
def test_init_type_hints_fake_module(self):
"""
If you somehow set the __module__ to something that doesn't exist
you'll lose __init__ resolution.
"""
class C:
x = attr.ib(type="typing.List[int]")
C.__module__ = "totally fake"
C = attr.s(C)
with pytest.raises(NameError):
typing.get_type_hints(C.__init__)
def test_inheritance(self):
"""
Subclasses can be resolved after the parent is resolved.
"""
@attr.define()
class A:
n: "int"
@attr.define()
class B(A):
pass
attr.resolve_types(A)
attr.resolve_types(B)
assert int == attr.fields(A).n.type
assert int == attr.fields(B).n.type
def test_resolve_twice(self):
"""
You can call resolve_types as many times as you like.
This test is here mostly for coverage.
"""
@attr.define()
class A:
n: "int"
attr.resolve_types(A)
assert int == attr.fields(A).n.type
attr.resolve_types(A)
assert int == attr.fields(A).n.type
@pytest.mark.parametrize(
"annot",
[
typing.ClassVar,
"typing.ClassVar",
"'typing.ClassVar[dict]'",
"t.ClassVar[int]",
],
)
def test_is_class_var(annot):
"""
ClassVars are detected, even if they're a string or quoted.
"""
assert _is_class_var(annot)