This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
runtests.py
executable file
·144 lines (117 loc) · 3.39 KB
/
runtests.py
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
#!/usr/bin/env python
import argparse
import ast
import importlib
import os
import subprocess
import sys
import pytest
STUBS_ROOT = os.path.dirname(os.path.abspath(__file__))
# Technically "public" functions (they don't start with an underscore)
# that we don't want to include.
BLACKLIST = {
"numpy": {
# Stdlib modules in the namespace by accident
"absolute_import",
"division",
"print_function",
"warnings",
# Accidentally public, deprecated, or shouldn't be used
"Tester",
"add_docstring",
"add_newdoc",
"add_newdoc_ufunc",
"core",
"fastCopyAndTranspose",
"get_array_wrap",
"int_asbuffer",
"oldnumeric",
"safe_eval",
"set_numeric_ops",
"test",
# Builtins
"bool",
"complex",
"float",
"int",
"long",
"object",
"str",
"unicode",
# Should use numpy_financial instead
"fv",
"ipmt",
"irr",
"mirr",
"nper",
"npv",
"pmt",
"ppmt",
"pv",
"rate",
# More standard names should be preferred
"alltrue", # all
"sometrue", # any
}
}
class FindAttributes(ast.NodeVisitor):
"""Find top-level attributes/functions/classes in the stubs.
Do this by walking the stubs ast. See e.g.
https://greentreesnakes.readthedocs.io/en/latest/index.html
for more information on working with Python's ast.
"""
def __init__(self):
self.attributes = set()
def visit_FunctionDef(self, node):
if node.name == "__getattr__":
# Not really a module member.
return
self.attributes.add(node.name)
# Do not call self.generic_visit; we are only interested in
# top-level functions.
return
def visit_ClassDef(self, node):
if not node.name.startswith("_"):
self.attributes.add(node.name)
return
def visit_AnnAssign(self, node):
self.attributes.add(node.target.id)
def find_missing(module_name):
module_path = os.path.join(
STUBS_ROOT,
module_name.replace("numpy", "numpy-stubs").replace(".", os.sep),
"__init__.pyi",
)
module = importlib.import_module(module_name)
module_attributes = {
attribute for attribute in dir(module) if not attribute.startswith("_")
}
if os.path.isfile(module_path):
with open(module_path) as f:
tree = ast.parse(f.read())
ast_visitor = FindAttributes()
ast_visitor.visit(tree)
stubs_attributes = ast_visitor.attributes
else:
# No stubs for this module yet.
stubs_attributes = set()
blacklist = BLACKLIST.get(module_name, set())
missing = module_attributes - stubs_attributes - blacklist
print("\n".join(sorted(missing)))
def run_pytest(argv):
subprocess.run(
[sys.executable, "-m", "pip", "install", STUBS_ROOT],
capture_output=True,
check=True,
)
return pytest.main([STUBS_ROOT] + argv)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--find-missing")
args, remaining_argv = parser.parse_known_args()
if args.find_missing is not None:
find_missing(args.find_missing)
sys.exit(0)
sys.exit(run_pytest(remaining_argv))
if __name__ == "__main__":
main()