/
test_next_gen.py
150 lines (109 loc) · 3.05 KB
/
test_next_gen.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
"""
Python 3-only integration tests for provisional next generation APIs.
"""
import pytest
import attr
@attr.define
class C:
x: str
y: int
class TestNextGen:
def test_simple(self):
"""
Instantiation works.
"""
C("1", 2)
def test_no_slots(self):
"""
slots can be deactivated.
"""
@attr.define(slots=False)
class NoSlots:
x: int
ns = NoSlots(1)
assert {"x": 1} == getattr(ns, "__dict__")
def test_validates(self):
"""
Validators at __init__ and __setattr__ work.
"""
@attr.define
class Validated:
x: int = attr.field(validator=attr.validators.instance_of(int))
v = Validated(1)
with pytest.raises(TypeError):
Validated(None)
with pytest.raises(TypeError):
v.x = "1"
def test_no_order(self):
"""
Order is off by default but can be added.
"""
with pytest.raises(TypeError):
C("1", 2) < C("2", 3)
@attr.define(order=True)
class Ordered:
x: int
assert Ordered(1) < Ordered(2)
def test_override_auto_attribs_true(self):
"""
Don't guess if auto_attrib is set explicitly.
Having an unannotated attr.ib/attr.field fails.
"""
with pytest.raises(attr.exceptions.UnannotatedAttributeError):
@attr.define(auto_attribs=True)
class ThisFails:
x = attr.field()
y: int
def test_override_auto_attribs_false(self):
"""
Don't guess if auto_attrib is set explicitly.
Annotated fields that don't carry an attr.ib are ignored.
"""
@attr.define(auto_attribs=False)
class NoFields:
x: int
y: int
assert NoFields() == NoFields()
def test_auto_attribs_detect(self):
"""
define correctly detects if a class lacks type annotations.
"""
@attr.define
class OldSchool:
x = attr.field()
assert OldSchool(1) == OldSchool(1)
def test_exception(self):
"""
Exceptions are detected and correctly handled.
"""
@attr.define
class E(Exception):
msg: str
other: int
with pytest.raises(E) as ei:
raise E("yolo", 42)
e = ei.value
assert ("yolo", 42) == e.args
assert "yolo" == e.msg
assert 42 == e.other
def test_frozen(self):
"""
attr.frozen freezes classes.
"""
@attr.frozen
class F:
x: str
f = F(1)
with pytest.raises(attr.exceptions.FrozenInstanceError):
f.x = 2
def test_auto_detect_eq(self):
"""
auto_detect=True works for eq.
Regression test for #670.
"""
@attr.define
class C:
def __eq__(self, o):
raise ValueError()
with pytest.raises(ValueError):
C() == C()