Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/lint.py
1396 views
1
# vim:fileencoding=utf-8
2
# License: BSD
3
# Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
4
5
linter = require('../tools/lint.js')
6
7
def lint(code):
8
return linter.lint_code(code, {'filename':'<test>', 'report':def():
9
pass
10
})
11
12
def err_test(code, ident, line, name, other_line):
13
msgs = lint(code)
14
assrt.ok(len(msgs) > 0, 'failed to find error in: ' + code)
15
assrt.equal(len(msgs), 1, 'found ' + (len(msgs) - 1) + ' extra errors in: ' + code)
16
assrt.equal(msgs[0].ident, ident, 'incorrect ident: ' + msgs[0].ident + ' for: ' + code)
17
if line != undefined:
18
assrt.equal(msgs[0].start_line, line, 'incorrect line: ' + msgs[0].start_line + ' for: ' + code)
19
if name != undefined:
20
assrt.equal(msgs[0].name, name, 'incorrect name: ' + msgs[0].name + ' for: ' + code)
21
if other_line != undefined:
22
assrt.equal(msgs[0].other_line, other_line, 'incorrect other_line: ' + msgs[0].other_line + ' for: ' + code)
23
24
def ok_test(code):
25
msgs = lint(code)
26
assrt.equal(msgs.length, 0, 'Got unexpected msg: ' + (msgs[0] or {}).message + ' for code: ' + code)
27
28
# Imports
29
err_test('import foo', 'unused-import', 1, 'foo')
30
err_test('import foo.boo', 'unused-import', 1, 'foo')
31
err_test('import foo as boo', 'unused-import', 1, 'boo')
32
err_test('from x import foo', 'unused-import', 1, 'foo')
33
err_test('from x import foo as boo', 'unused-import', 1, 'boo')
34
ok_test('import foo\nfoo')
35
36
# Function arguments
37
ok_test('def f(a):\n return a')
38
ok_test('def f(a=1):\n return a')
39
ok_test('def f(*a):\n return a')
40
ok_test('def f(**a):\n return a')
41
ok_test('def f(a):\n return 1')
42
43
# Extended slices
44
ok_test('a = []; a[::2], a[1:-1]')
45
46
# Top level unused ignored
47
ok_test('a=1\ndef f():\n pass')
48
49
# Destructuring assignment
50
ok_test('def f():\n a, b = 1, 2; a, b')
51
ok_test('def f():\n a = 1; b, c = a, 2; return b, c')
52
ok_test('for x, (y, z) in [ [1, [2, 3]] ]:\n x + y + z')
53
err_test('def f():\n a, b = 1, 1; return a', 'unused-local', 2, 'b')
54
55
# Compound assignment
56
err_test('a += 1', 'undef', 1, 'a')
57
ok_test('def f():\n a = 1; a += 1')
58
59
# Unused bindings
60
err_test('def f():\n a=1', 'unused-local', 2, 'a')
61
err_test('def f():\n def a():\n pass', 'unused-local', 2, 'a')
62
# But vars starting with _ are OK.
63
ok_test("def f():\n _=5")
64
ok_test("def f():\n _foo=5")
65
66
# Undefined references
67
err_test('f()', 'undef', 1, 'f')
68
err_test('a', 'undef', 1, 'a')
69
err_test('def f():\n a=1; return a\na', 'undef', 3, 'a')
70
71
# Comprehensions
72
ok_test('[x for x in [1,2]]')
73
ok_test('[1 for x in [1,2] if x]')
74
ok_test('[1 for x in [1,2]]')
75
ok_test('def f():\n l=[1,2];[x for x in l]')
76
ok_test('def f():\n l=[1,2];{x:True for x in l}')
77
err_test('def f():\n [x for x in [1,2]]; return x', 'undef', 2, 'x')
78
79
# Loops
80
ok_test('def f():\n for x in "":\n pass\n return x')
81
ok_test('def f():\n for x in "":\n x += 1\n')
82
ok_test('def f():\n for x, y in "":\n pass\n return x, y')
83
ok_test('for r"%js var i = 0; i < 1; i++":\n print(i)')
84
err_test('def f():\n a = 1\n for a in "":\n a', 'loop-shadowed', 3, 'a', 2)
85
86
# Semi-colons
87
err_test('a=1;;a', 'extra-semicolon', 1, ';')
88
err_test('a=1;', 'eol-semicolon', 1, ';')
89
90
# Builtins
91
for k in 'String Symbol this self window Map arguments print len range dir undefined'.split(' '):
92
ok_test(k)
93
94
# noqa
95
ok_test('f() # noqa')
96
ok_test('f() # noqa:undef')
97
err_test('f() # noqa:xxx', 'undef')
98
99
# Named func in branch
100
err_test('if 1:\n def f():\n pass', 'func-in-branch', 2, 'f')
101
err_test('if 1:\n pass\nelse:\n def f():\n pass', 'func-in-branch', 4, 'f')
102
err_test('try:\n def f():\n pass\nexcept:\n pass', 'func-in-branch', 2, 'f')
103
ok_test('if 1:\n a = def():\n pass')
104
105
# Syntax errors
106
err_test('def f(:\n pass', 'syntax-err', 1)
107
108
# Functions
109
ok_test('def f():\n pass\nf()')
110
111
# Non-locals
112
err_test('def a():\n x = 1\n def b():\n nonlocal x\n b()', 'unused-local', 2, 'x')
113
ok_test('def a():\n x = 1\n def b():\n nonlocal x\n x\n b()')
114
ok_test('def a():\n x = 1\n def b():\n nonlocal x\n x\n x = 2\n b()')
115
ok_test('def a():\n x = 1\n def ():\n nonlocal x\n x = 1\n x')
116
ok_test('nonlocal a\na()')
117
118
# Define after use
119
err_test('def g():\n f()\n f()\n def f():\n pass', 'def-after-use', 3, 'f')
120
121
# Decorators
122
ok_test('from x import y\n@y\ndef f():\n pass')
123
124
# try/except
125
ok_test('try:\n 1\nexcept Error as e:\n e')
126
127
# Classes
128
ok_test('''
129
class A:
130
131
h = j = 1
132
x = h + j
133
if True:
134
k = 1
135
else:
136
k = 2
137
138
def __init__(self, a):
139
self.a = a
140
141
def unused(self):
142
pass
143
other = unused
144
145
class B(A):
146
147
def __init__(self):
148
A.__init__(self, 'b')
149
''')
150
err_test('class A:\n def a(self):\n a()', 'undef', 3, 'a')
151
err_test('class A:\n def a(self):\n pass\n def a(self):\n pass', 'dup-method', 4, 'a')
152
153
# Object literals
154
err_test('{"a":1, "a":2}', 'dup-key', 1, 'a')
155
156
# keyword arg values
157
ok_test('def f():\n a=1\n f(b=a)')
158
ok_test('def f():\n a={}\n f(**a)')
159
160
# with statement
161
ok_test('''
162
def f():
163
a = 1
164
with a as b:
165
b()
166
''')
167
err_test('with a:\n pass', 'undef', '1', 'a')
168
169