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
|
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2020 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
# Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
# Copyright (c) 2017 Ćukasz Rogalski <rogalski.91@gmail.com>
# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
import contextlib
import unittest
import pytest
import sys
import astroid
from astroid import extract_node
from astroid.test_utils import require_version
from astroid import InferenceError
from astroid import nodes
from astroid import util
from astroid.node_classes import AssignName, Const, Name, Starred
@contextlib.contextmanager
def _add_transform(manager, node, transform, predicate=None):
manager.register_transform(node, transform, predicate)
try:
yield
finally:
manager.unregister_transform(node, transform, predicate)
class ProtocolTests(unittest.TestCase):
def assertConstNodesEqual(self, nodes_list_expected, nodes_list_got):
self.assertEqual(len(nodes_list_expected), len(nodes_list_got))
for node in nodes_list_got:
self.assertIsInstance(node, Const)
for node, expected_value in zip(nodes_list_got, nodes_list_expected):
self.assertEqual(expected_value, node.value)
def assertNameNodesEqual(self, nodes_list_expected, nodes_list_got):
self.assertEqual(len(nodes_list_expected), len(nodes_list_got))
for node in nodes_list_got:
self.assertIsInstance(node, Name)
for node, expected_name in zip(nodes_list_got, nodes_list_expected):
self.assertEqual(expected_name, node.name)
def test_assigned_stmts_simple_for(self):
assign_stmts = extract_node(
"""
for a in (1, 2, 3): #@
pass
for b in range(3): #@
pass
"""
)
for1_assnode = next(assign_stmts[0].nodes_of_class(AssignName))
assigned = list(for1_assnode.assigned_stmts())
self.assertConstNodesEqual([1, 2, 3], assigned)
for2_assnode = next(assign_stmts[1].nodes_of_class(AssignName))
self.assertRaises(InferenceError, list, for2_assnode.assigned_stmts())
@require_version(minver="3.0")
def test_assigned_stmts_starred_for(self):
assign_stmts = extract_node(
"""
for *a, b in ((1, 2, 3), (4, 5, 6, 7)): #@
pass
"""
)
for1_starred = next(assign_stmts.nodes_of_class(Starred))
assigned = next(for1_starred.assigned_stmts())
assert isinstance(assigned, astroid.List)
assert assigned.as_string() == "[1, 2]"
def _get_starred_stmts(self, code):
assign_stmt = extract_node("{} #@".format(code))
starred = next(assign_stmt.nodes_of_class(Starred))
return next(starred.assigned_stmts())
def _helper_starred_expected_const(self, code, expected):
stmts = self._get_starred_stmts(code)
self.assertIsInstance(stmts, nodes.List)
stmts = stmts.elts
self.assertConstNodesEqual(expected, stmts)
def _helper_starred_expected(self, code, expected):
stmts = self._get_starred_stmts(code)
self.assertEqual(expected, stmts)
def _helper_starred_inference_error(self, code):
assign_stmt = extract_node("{} #@".format(code))
starred = next(assign_stmt.nodes_of_class(Starred))
self.assertRaises(InferenceError, list, starred.assigned_stmts())
@require_version(minver="3.0")
def test_assigned_stmts_starred_assnames(self):
self._helper_starred_expected_const("a, *b = (1, 2, 3, 4) #@", [2, 3, 4])
self._helper_starred_expected_const("*a, b = (1, 2, 3) #@", [1, 2])
self._helper_starred_expected_const("a, *b, c = (1, 2, 3, 4, 5) #@", [2, 3, 4])
self._helper_starred_expected_const("a, *b = (1, 2) #@", [2])
self._helper_starred_expected_const("*b, a = (1, 2) #@", [1])
self._helper_starred_expected_const("[*b] = (1, 2) #@", [1, 2])
@require_version(minver="3.0")
def test_assigned_stmts_starred_yes(self):
# Not something iterable and known
self._helper_starred_expected("a, *b = range(3) #@", util.Uninferable)
# Not something inferrable
self._helper_starred_expected("a, *b = balou() #@", util.Uninferable)
# In function, unknown.
self._helper_starred_expected(
"""
def test(arg):
head, *tail = arg #@""",
util.Uninferable,
)
# These cases aren't worth supporting.
self._helper_starred_expected(
"a, (*b, c), d = (1, (2, 3, 4), 5) #@", util.Uninferable
)
@require_version(minver="3.0")
def test_assign_stmts_starred_fails(self):
# Too many starred
self._helper_starred_inference_error("a, *b, *c = (1, 2, 3) #@")
# This could be solved properly, but it complicates needlessly the
# code for assigned_stmts, without offering real benefit.
self._helper_starred_inference_error(
"(*a, b), (c, *d) = (1, 2, 3), (4, 5, 6) #@"
)
def test_assigned_stmts_assignments(self):
assign_stmts = extract_node(
"""
c = a #@
d, e = b, c #@
"""
)
simple_assnode = next(assign_stmts[0].nodes_of_class(AssignName))
assigned = list(simple_assnode.assigned_stmts())
self.assertNameNodesEqual(["a"], assigned)
assnames = assign_stmts[1].nodes_of_class(AssignName)
simple_mul_assnode_1 = next(assnames)
assigned = list(simple_mul_assnode_1.assigned_stmts())
self.assertNameNodesEqual(["b"], assigned)
simple_mul_assnode_2 = next(assnames)
assigned = list(simple_mul_assnode_2.assigned_stmts())
self.assertNameNodesEqual(["c"], assigned)
@require_version(minver="3.6")
def test_assigned_stmts_annassignments(self):
annassign_stmts = extract_node(
"""
a: str = "abc" #@
b: str #@
"""
)
simple_annassign_node = next(annassign_stmts[0].nodes_of_class(AssignName))
assigned = list(simple_annassign_node.assigned_stmts())
self.assertEqual(1, len(assigned))
self.assertIsInstance(assigned[0], Const)
self.assertEqual(assigned[0].value, "abc")
empty_annassign_node = next(annassign_stmts[1].nodes_of_class(AssignName))
assigned = list(empty_annassign_node.assigned_stmts())
self.assertEqual(1, len(assigned))
self.assertIs(assigned[0], util.Uninferable)
def test_sequence_assigned_stmts_not_accepting_empty_node(self):
def transform(node):
node.root().locals["__all__"] = [node.value]
manager = astroid.MANAGER
with _add_transform(manager, astroid.Assign, transform):
module = astroid.parse(
"""
__all__ = ['a']
"""
)
module.wildcard_import_names()
def test_not_passing_uninferable_in_seq_inference(self):
class Visitor:
def visit(self, node):
for child in node.get_children():
child.accept(self)
visit_module = visit
visit_assign = visit
visit_binop = visit
visit_list = visit
visit_const = visit
visit_name = visit
def visit_assignname(self, node):
for _ in node.infer():
pass
parsed = extract_node(
"""
a = []
x = [a*2, a]*2*2
"""
)
parsed.accept(Visitor())
@pytest.mark.skipif(
sys.version_info[:2] < (3, 8), reason="needs assignment expressions"
)
def test_named_expr_inference():
code = """
if (a := 2) == 2:
a #@
# Test a function call
def test():
return 24
if (a := test()):
a #@
# Normal assignments in sequences
{ (a:= 4) } #@
[ (a:= 5) ] #@
# Something more complicated
def test(value=(p := 24)): return p
[ y:= test()] #@
# Priority assignment
(x := 1, 2)
x #@
"""
ast_nodes = extract_node(code)
node = next(ast_nodes[0].infer())
assert isinstance(node, nodes.Const)
assert node.value == 2
node = next(ast_nodes[1].infer())
assert isinstance(node, nodes.Const)
assert node.value == 24
node = next(ast_nodes[2].infer())
assert isinstance(node, nodes.Set)
assert isinstance(node.elts[0], nodes.Const)
assert node.elts[0].value == 4
node = next(ast_nodes[3].infer())
assert isinstance(node, nodes.List)
assert isinstance(node.elts[0], nodes.Const)
assert node.elts[0].value == 5
node = next(ast_nodes[4].infer())
assert isinstance(node, nodes.List)
assert isinstance(node.elts[0], nodes.Const)
assert node.elts[0].value == 24
node = next(ast_nodes[5].infer())
assert isinstance(node, nodes.Const)
assert node.value == 1
if __name__ == "__main__":
unittest.main()
|