Repository navigation
Expand file tree
/
Copy pathtest_api_inspect.py
More file actions
241 lines (199 loc) · 5.08 KB
/
Copy pathtest_api_inspect.py
File metadata and controls
241 lines (199 loc) · 5.08 KB
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
import ast
import pytest
from inspect_api.inspect import inspect_file_ast, inspect_file_contents
def test_example_functions():
source = '''
import numpy
import numpy as np
from numpy import random
from numpy import random as rnd
numpy.array([1, 2])
np.sum(1, 2, axis=0)
random.random(np.array([1, 2, 3]).shape)
rnd.random()
a = numpy.array([1, 2, 3])
## will not catch anything this sadly...
a.sum()
'''
result = inspect_file_ast(ast.parse(source))
expected_result = {
'def_class': {
'count': 0,
'n_attr': {},
'n_func': {},
'dunder': {},
'inherit': {}
},
'def_function': {'count': 0, 'n_args': {}},
'attribute': {},
'function': {
'numpy': {
('numpy', 'sum'): {
'count': 1,
'd_count': 0,
'n_args': {3: 1},
'kwargs': {'axis': 1}},
('numpy', 'random', 'random'): {
'count': 2,
'd_count': 0,
'n_args': {0: 1, 1: 1},
'kwargs': {}},
('numpy', 'array'): {
'count': 3,
'd_count': 0,
'n_args': {1: 3},
'kwargs': {}}
}
}
}
import pprint
pprint.pprint(dict(result['function']))
assert result == expected_result
def test_example_attribute():
source = '''
import numpy
import numpy as np
from numpy import random
from numpy import random as rnd
np.asdf
numpy.array(np.asdf)
random.random
rnd.random
'''
result = inspect_file_ast(ast.parse(source))
expected_result = {
'def_class': {
'count': 0,
'n_attr': {},
'n_func': {},
'dunder': {},
'inherit': {}
},
'def_function': {'count': 0, 'n_args': {}},
'function': {
'numpy': {
('numpy', 'array'): {
'count': 1,
'd_count': 0,
'n_args': {1: 1},
'kwargs': {}
}
}
},
'attribute': {
'numpy': {
('numpy', 'asdf'): {'count': 2, 'd_count': 0},
('numpy', 'random', 'random'): {'count': 2, 'd_count': 0},
}
}
}
assert result == expected_result
def test_example_decorator():
source = '''
import contextlib
from contextlib import contextmanager
from contextlib import contextmanager as cxtm
@contextmanager
def foo():
pass
@contextlib.contextmanager
def bar():
pass
cxtm
contextlib.contextmanager()
@cxtm(a=2)
def biz():
pass
'''
result = inspect_file_ast(ast.parse(source))
expected_result = {
'def_class': {
'count': 0,
'n_attr': {},
'n_func': {},
'dunder': {},
'inherit': {}
},
'def_function': {
'count': 3,
'n_args': {0: 3},
},
'function': {
'contextlib': {
('contextlib', 'contextmanager'): {
'count': 2,
'd_count': 1,
'kwargs': {'a': 1},
'n_args': {0: 1, 1: 1}
}
}
},
'attribute': {
'contextlib': {
('contextlib', 'contextmanager'): {'count': 3, 'd_count': 2}
}
}
}
import pprint
pprint.pprint(result)
assert result == expected_result
def test_example_def_class():
source = '''
import numpy as np
import numpy
def qwerqwer(a, b):
pass
class Foo(np.array, asdf):
qwer = 1
def __init__(self, a, b):
pass
def __getattr__(self):
pass
def foo():
pass
class Bar(numpy.matrix):
def asdf(self):
pass
'''
expected_result = {
'function': {},
'attribute': {
'numpy': {
('numpy', 'array'): {'count': 1, 'd_count': 0},
('numpy', 'matrix'): {'count': 1, 'd_count': 0},
}
},
'def_function': {
'count': 5,
'n_args': {3: 1, 2: 1, 1: 2, 0: 1}
},
'def_class': {
'count': 2,
'n_func': {3: 1, 1: 1},
'n_attr': {1: 1, 0: 1},
'dunder': {'__init__': 1, '__getattr__': 1},
'inherit': {('numpy', 'matrix'): 1, ('numpy', 'array'): 1},
}
}
result = inspect_file_ast(ast.parse(source))
import pprint
pprint.pprint(result)
assert result == expected_result
def test_example_contents_python():
source = b'''import numpy as np
a = np.array()
# hello world
a.sum()
'''
result = inspect_file_contents('example.py', source)
expected_result = {
'contents': {
'num_newlines': 6,
'num_whitespace_lines': 2,
'num_comment_lines': 1,
'min_line_length': 7,
'max_line_length': 18,
'avg_line_length': 9.0,
}
}
assert result == expected_result