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
|
import pytest
import astroid
from astroid import bases, nodes
from astroid.const import PY37_PLUS
from astroid.exceptions import InferenceError
from astroid.util import Uninferable
if not PY37_PLUS:
pytest.skip("Dataclasses were added in 3.7", allow_module_level=True)
def test_inference_attribute_no_default():
"""Test inference of dataclass attribute with no default.
Note that the argument to the constructor is ignored by the inference.
"""
klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass
@dataclass
class A:
name: str
A.name #@
A('hi').name #@
"""
)
with pytest.raises(InferenceError):
klass.inferred()
inferred = instance.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], bases.Instance)
assert inferred[0].name == "str"
def test_inference_non_field_default():
"""Test inference of dataclass attribute with a non-field default."""
klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass
@dataclass
class A:
name: str = 'hi'
A.name #@
A().name #@
"""
)
inferred = klass.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
inferred = instance.inferred()
assert len(inferred) == 2
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
assert isinstance(inferred[1], bases.Instance)
assert inferred[1].name == "str"
def test_inference_field_default():
"""Test inference of dataclass attribute with a field call default
(default keyword argument given)."""
klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass, field
@dataclass
class A:
name: str = field(default='hi')
A.name #@
A().name #@
"""
)
inferred = klass.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
inferred = instance.inferred()
assert len(inferred) == 2
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
assert isinstance(inferred[1], bases.Instance)
assert inferred[1].name == "str"
def test_inference_field_default_factory():
"""Test inference of dataclass attribute with a field call default
(default_factory keyword argument given)."""
klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass, field
@dataclass
class A:
name: list = field(default_factory=list)
A.name #@
A().name #@
"""
)
inferred = klass.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.List)
assert inferred[0].elts == []
inferred = instance.inferred()
assert len(inferred) == 2
assert isinstance(inferred[0], nodes.List)
assert inferred[0].elts == []
assert isinstance(inferred[1], bases.Instance)
assert inferred[1].name == "list"
def test_inference_method():
"""Test inference of dataclass attribute within a method,
with a default_factory field.
Based on https://github.com/PyCQA/pylint/issues/2600
"""
node = astroid.extract_node(
"""
from typing import Dict
from dataclasses import dataclass, field
@dataclass
class TestClass:
foo: str
bar: str
baz_dict: Dict[str, str] = field(default_factory=dict)
def some_func(self) -> None:
f = self.baz_dict.items #@
for key, value in f():
print(key)
print(value)
"""
)
inferred = next(node.value.infer())
assert isinstance(inferred, bases.BoundMethod)
def test_inference_no_annotation():
"""Test that class variables without type annotations are not
turned into instance attributes.
"""
class_def, klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass
@dataclass
class A:
name = 'hi'
A #@
A.name #@
A().name #@
"""
)
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
# Both the class and instance can still access the attribute
for node in (klass, instance):
inferred = node.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
def test_inference_class_var():
"""Test that class variables with a ClassVar type annotations are not
turned into instance attributes.
"""
class_def, klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class A:
name: ClassVar[str] = 'hi'
A #@
A.name #@
A().name #@
"""
)
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
# Both the class and instance can still access the attribute
for node in (klass, instance):
inferred = node.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
def test_inference_init_var():
"""Test that class variables with InitVar type annotations are not
turned into instance attributes.
"""
class_def, klass, instance = astroid.extract_node(
"""
from dataclasses import dataclass, InitVar
@dataclass
class A:
name: InitVar[str] = 'hi'
A #@
A.name #@
A().name #@
"""
)
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
# Both the class and instance can still access the attribute
for node in (klass, instance):
inferred = node.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.Const)
assert inferred[0].value == "hi"
def test_inference_generic_collection_attribute():
"""Test that an attribute with a generic collection type from the
typing module is inferred correctly.
"""
attr_nodes = astroid.extract_node(
"""
from dataclasses import dataclass, field
import typing
@dataclass
class A:
dict_prop: typing.Dict[str, str]
frozenset_prop: typing.FrozenSet[str]
list_prop: typing.List[str]
set_prop: typing.Set[str]
tuple_prop: typing.Tuple[int, str]
a = A({}, frozenset(), [], set(), (1, 'hi'))
a.dict_prop #@
a.frozenset_prop #@
a.list_prop #@
a.set_prop #@
a.tuple_prop #@
"""
)
names = (
"Dict",
"FrozenSet",
"List",
"Set",
"Tuple",
)
for node, name in zip(attr_nodes, names):
inferred = next(node.infer())
assert isinstance(inferred, bases.Instance)
assert inferred.name == name
def test_inference_callable_attribute():
"""Test that an attribute with a Callable annotation is inferred as Uninferable.
See issue#1129.
"""
instance = astroid.extract_node(
"""
from dataclasses import dataclass
from typing import Any, Callable
@dataclass
class A:
enabled: Callable[[Any], bool]
A(lambda x: x == 42).enabled #@
"""
)
inferred = next(instance.infer())
assert inferred is Uninferable
|