File: definitions.py

package info (click to toggle)
python-beniget 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: python: 1,590; sh: 7; makefile: 6
file content (381 lines) | stat: -rw-r--r-- 11,744 bytes parent folder | download
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
from unittest import TestCase
import gast as ast
import beniget
import sys


class StrictDefUseChains(beniget.DefUseChains):
    def unbound_identifier(self, name, node):
        raise RuntimeError(
            "W: unbound identifier '{}' at {}:{}".format(
                name, node.lineno, node.col_offset
            )
        )


class TestGlobals(TestCase):
    def checkGlobals(self, code, ref):
        node = ast.parse(code)
        c = StrictDefUseChains()
        c.visit(node)
        self.assertEqual(c.dump_definitions(node), ref)

    def test_SingleFunctionDef(self):
        code = "def foo(): pass"
        self.checkGlobals(code, ["foo"])

    def test_MultipleFunctionDef(self):
        code = "def foo(): pass\ndef bar(): return"
        self.checkGlobals(code, ["bar", "foo"])

    def testFuntionRedefinion(self):
        code = "def foo(): pass\ndef foo(): return"
        self.checkGlobals(code, ["foo", "foo"])

    def testFuntionNested(self):
        code = "def foo():\n def bar(): return"
        self.checkGlobals(code, ["foo"])

    if sys.version_info.major >= 3:

        def testAsyncFunctionDef(self):
            code = "async def foo(): pass"
            self.checkGlobals(code, ["foo"])

    def testClassDef(self):
        code = "class C:pass"
        self.checkGlobals(code, ["C"])

    def testDelClassDef(self):
        code = "class C:pass\ndel C"
        self.checkGlobals(code, ["C"])

    def testDelClassDefReDef(self):
        code = "class C:pass\ndel C\nclass C:pass"
        self.checkGlobals(code, ["C", "C"])

    def testNestedClassDef(self):
        code = "class C:\n class D: pass"
        self.checkGlobals(code, ["C"])

    def testMultipleClassDef(self):
        code = "class C: pass\nclass D: pass"
        self.checkGlobals(code, ["C", "D"])

    def testClassRedefinition(self):
        code = "class C: pass\nclass C: pass"
        self.checkGlobals(code, ["C", "C"])

    def testClassMethodDef(self):
        code = "class C:\n def some(self):pass"
        self.checkGlobals(code, ["C"])

    def testGlobalDef(self):
        code = "x = 1"
        self.checkGlobals(code, ["x"])

    if sys.version_info.major >= 3:

        def testGlobalAnnotatedDef(self):
            code = "x : 1"
            self.checkGlobals(code, ["x"])

    def testMultipleGlobalDef(self):
        code = "x = 1; x = 2"
        self.checkGlobals(code, ["x", "x"])

    def testGlobalDestructuring(self):
        code = "x, y = 1, 2"
        self.checkGlobals(code, ["x", "y"])

    def testGlobalAugAssign(self):
        code = "x = 1; x += 2"
        self.checkGlobals(code, ["x"])

    def testGlobalFor(self):
        code = "for x in (1,2): pass"
        self.checkGlobals(code, ["x"])

    def testGlobalForDestructuring(self):
        code = "for x, y in [(1,2)]: pass"
        self.checkGlobals(code, ["x", "y"])

    def testGlobalNestedFor(self):
        code = "for x in (1,2):\n for y in (2, 1): pass"
        self.checkGlobals(code, ["x", "y"])

    def testGlobalInFor(self):
        code = "for x in (1,2): y = x"
        self.checkGlobals(code, ["x", "y"])

    if sys.version_info >= (3, 7):

        def testGlobalAsyncFor(self):
            code = "async for x in (1,2): pass"
            self.checkGlobals(code, ["x"])

    def testGlobalInWhile(self):
        code = "while True: x = 1"
        self.checkGlobals(code, ["x"])

    def testGlobalInIfTrueBranch(self):
        code = "if 1: a = 1"
        self.checkGlobals(code, ["a"])

    def testGlobalInIfFalseBranch(self):
        code = "if 1: pass\nelse: a = 1"
        self.checkGlobals(code, ["a"])

    def testGlobalInIfBothBranch(self):
        code = "if 1: a = 1\nelse: a = 2"
        self.checkGlobals(code, ["a", "a"])

    def testGlobalInIfBothBranchDifferent(self):
        code = "if 1: a = 1\nelse: b = 2"
        self.checkGlobals(code, ["a", "b"])

    def testGlobalWith(self):
        code = "from some import foo\nwith foo() as x: pass"
        self.checkGlobals(code, ["foo", "x"])

    if sys.version_info >= (3, 7):

        def testGlobalAsyncWith(self):
            code = "from some import foo\nasync with foo() as x: pass"
            self.checkGlobals(code, ["foo", "x"])

    def testGlobalTry(self):
        code = "try: x = 1\nexcept Exception: pass"
        self.checkGlobals(code, ["x"])

    def testGlobalTryExcept(self):
        code = "from some import foo\ntry: foo()\nexcept Exception as e: pass"
        self.checkGlobals(code, ["e", "foo"])

    def testGlobalTryExceptFinally(self):
        code = "try: w = 1\nexcept Exception as x: y = 1\nfinally: z = 1"
        self.checkGlobals(code, ["w", "x", "y", "z"])

    def testGlobalThroughKeyword(self):
        code = "def foo(): global x"
        self.checkGlobals(code, ["foo", "x"])

    def testGlobalThroughKeywords(self):
        code = "def foo(): global x, y"
        self.checkGlobals(code, ["foo", "x", "y"])

    def testGlobalThroughMultipleKeyword(self):
        code = "def foo(): global x\ndef bar(): global x"
        self.checkGlobals(code, ["bar", "foo", "x"])

    def testGlobalBeforeKeyword(self):
        code = "x = 1\ndef foo(): global x"
        self.checkGlobals(code, ["foo", "x"])

    def testGlobalsBeforeKeyword(self):
        code = "x = 1\ndef foo(): global x, y"
        self.checkGlobals(code, ["foo", "x", "y"])

    if sys.version_info.major >= 3:

        def testGlobalAfterKeyword(self):
            code = "def foo(): global x\nx : 1"
            self.checkGlobals(code, ["foo", "x"])

        def testGlobalsAfterKeyword(self):
            code = "def foo(): global x, y\ny : 1"
            self.checkGlobals(code, ["foo", "x", "y"])

    def testGlobalImport(self):
        code = "import foo"
        self.checkGlobals(code, ["foo"])

    def testGlobalImports(self):
        code = "import foo, bar"
        self.checkGlobals(code, ["bar", "foo"])

    def testGlobalImportSubModule(self):
        code = "import foo.bar"
        self.checkGlobals(code, ["foo"])

    def testGlobalImportSubModuleAs(self):
        code = "import foo.bar as foobar"
        self.checkGlobals(code, ["foobar"])

    def testGlobalImportAs(self):
        code = "import foo as bar"
        self.checkGlobals(code, ["bar"])

    def testGlobalImportsAs(self):
        code = "import foo as bar, foobar"
        self.checkGlobals(code, ["bar", "foobar"])

    def testGlobalImportFrom(self):
        code = "from foo import bar"
        self.checkGlobals(code, ["bar"])

    def testGlobalImportFromAs(self):
        code = "from foo import bar as BAR"
        self.checkGlobals(code, ["BAR"])

    def testGlobalImportFromStar(self):
        code = "from foo import *"
        self.checkGlobals(code, ["*"])

    def testGlobalImportFromStarRedefine(self):
        code = "from foo import *\nx+=1"
        self.checkGlobals(code, ["*", "x"])

    def testGlobalImportsFrom(self):
        code = "from foo import bar, man"
        self.checkGlobals(code, ["bar", "man"])

    def testGlobalImportsFromAs(self):
        code = "from foo import bar, man as maid"
        self.checkGlobals(code, ["bar", "maid"])

    def testGlobalListComp(self):
        code = "from some import y; [1 for x in y]"
        if sys.version_info.major == 2:
            self.checkGlobals(code, ["x", "y"])
        else:
            self.checkGlobals(code, ["y"])

    def testGlobalSetComp(self):
        code = "from some import y; {1 for x in y}"
        if sys.version_info.major == 2:
            self.checkGlobals(code, ["x", "y"])
        else:
            self.checkGlobals(code, ["y"])

    def testGlobalDictComp(self):
        code = "from some import y; {1:1 for x in y}"
        if sys.version_info.major == 2:
            self.checkGlobals(code, ["x", "y"])
        else:
            self.checkGlobals(code, ["y"])

    def testGlobalGeneratorExpr(self):
        code = "from some import y; (1 for x in y)"
        if sys.version_info.major == 2:
            self.checkGlobals(code, ["x", "y"])
        else:
            self.checkGlobals(code, ["y"])

    def testGlobalLambda(self):
        code = "lambda x: x"
        self.checkGlobals(code, [])


class TestClasses(TestCase):
    def checkClasses(self, code, ref):
        node = ast.parse(code)
        c = StrictDefUseChains()
        c.visit(node)
        classes = [n for n in node.body if isinstance(n, ast.ClassDef)]
        assert len(classes) == 1, "only one top-level function per test case"
        cls = classes[0]
        self.assertEqual(c.dump_definitions(cls), ref)

    def test_class_method_assign(self):
        code = "class C:\n def foo(self):pass\n bar = foo"
        self.checkClasses(code, ["bar", "foo"])


class TestLocals(TestCase):
    def checkLocals(self, code, ref):
        node = ast.parse(code)
        c = StrictDefUseChains()
        c.visit(node)
        functions = [n for n in node.body if isinstance(n, ast.FunctionDef)]
        assert len(functions) == 1, "only one top-level function per test case"
        f = functions[0]
        self.assertEqual(c.dump_definitions(f), ref)

    def testLocalFunctionDef(self):
        code = "def foo(): pass"
        self.checkLocals(code, [])

    def testLocalFunctionDefOneArg(self):
        code = "def foo(a): pass"
        self.checkLocals(code, ["a"])

    def testLocalFunctionDefOneArgDefault(self):
        code = "def foo(a=1): pass"
        self.checkLocals(code, ["a"])

    def testLocalFunctionDefArgsDefault(self):
        code = "def foo(a, b=1): pass"
        self.checkLocals(code, ["a", "b"])

    def testLocalFunctionDefStarArgs(self):
        code = "def foo(a, *b): pass"
        self.checkLocals(code, ["a", "b"])

    def testLocalFunctionDefKwArgs(self):
        code = "def foo(a, **b): pass"
        self.checkLocals(code, ["a", "b"])

    if sys.version_info.major >= 3:

        def testLocalFunctionDefKwOnly(self):
            code = "def foo(a, *, b=1): pass"
            self.checkLocals(code, ["a", "b"])

    if sys.version_info.major == 2:

        def testLocalFunctionDefDestructureArg(self):
            code = "def foo((a, b)): pass"
            self.checkLocals(code, ["a", "b"])

    def test_LocalAssign(self):
        code = "def foo(): a = 1"
        self.checkLocals(code, ["a"])

    def test_LocalAssignRedef(self):
        code = "def foo(a): a = 1"
        self.checkLocals(code, ["a", "a"])

    def test_LocalNestedFun(self):
        code = "def foo(a):\n def bar(): return a\n return bar"
        self.checkLocals(code, ["a", "bar"])

    if sys.version_info.major >= 3:

        def test_LocalNonLocalBefore(self):
            code = "def foo(a):\n def bar():\n  nonlocal a; a = 1\n bar(); return a"
            self.checkLocals(code, ["a", "bar"])

        def test_LocalNonLocalAfter(self):
            code = (
                "def foo():\n def bar():\n  nonlocal a; a = 1\n a = 2; bar(); return a"
            )
            self.checkLocals(code, ["a", "bar"])

    def test_LocalGlobal(self):
        code = "def foo(): global a; a = 1"
        self.checkLocals(code, [])

    def test_ListCompInLoop(self):
        code = "def foo(i):\n for j in i:\n  [k for k in j]"
        if sys.version_info.major == 2:
            self.checkLocals(code, ["i", "j", "k"])
        else:
            self.checkLocals(code, ["i", "j"])

    def test_AugAssignInLoop(self):
        code = """
def foo(X, f):
    for i in range(2):
        if i == 0: A = f * X[:, i]
        else: A += f * X[:, i]
    return A"""
        self.checkLocals(code, ["A", "X", "f", "i"])

    def test_IfInWhile(self):
        code = """
def foo(a):
    while(a):
        if a == 1: print(b)
        else: b = a"""
        self.checkLocals(code, ["a", "b"])