| 1 |
#!/usr/bin/env python |
| 2 |
"""Find the functions in a module missing type annotations. |
| 3 |
|
| 4 |
To use it run |
| 5 |
|
| 6 |
./functions_missing_types.py <module> |
| 7 |
|
| 8 |
and it will print out a list of functions in the module that don't |
| 9 |
have types. |
| 10 |
|
| 11 |
""" |
| 12 |
import argparse |
| 13 |
import ast |
| 14 |
import importlib |
| 15 |
import os |
| 16 |
|
| 17 |
NUMPY_ROOT = os.path.dirname(os.path.join( |
| 18 |
os.path.abspath(__file__), "..", |
| 19 |
)) |
| 20 |
|
| 21 |
# Technically "public" functions (they don't start with an underscore) |
| 22 |
# that we don't want to include. |
| 23 |
EXCLUDE_LIST = { |
| 24 |
"numpy": { |
| 25 |
# Stdlib modules in the namespace by accident |
| 26 |
"absolute_import", |
| 27 |
"division", |
| 28 |
"print_function", |
| 29 |
"warnings", |
| 30 |
"sys", |
| 31 |
"os", |
| 32 |
"math", |
| 33 |
# Accidentally public, deprecated, or shouldn't be used |
| 34 |
"Tester", |
| 35 |
"_core", |
| 36 |
"get_array_wrap", |
| 37 |
"int_asbuffer", |
| 38 |
"numarray", |
| 39 |
"oldnumeric", |
| 40 |
"safe_eval", |
| 41 |
"test", |
| 42 |
"typeDict", |
| 43 |
# Builtins |
| 44 |
"bool", |
| 45 |
"complex", |
| 46 |
"float", |
| 47 |
"int", |
| 48 |
"long", |
| 49 |
"object", |
| 50 |
"str", |
| 51 |
"unicode", |
| 52 |
# More standard names should be preferred |
| 53 |
"alltrue", # all |
| 54 |
"sometrue", # any |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
class FindAttributes(ast.NodeVisitor): |
| 60 |
"""Find top-level attributes/functions/classes in stubs files. |
| 61 |
|
| 62 |
Do this by walking the stubs ast. See e.g. |
| 63 |
|
| 64 |
https://greentreesnakes.readthedocs.io/en/latest/index.html |
| 65 |
|
| 66 |
for more information on working with Python's ast. |
| 67 |
|
| 68 |
""" |
| 69 |
|
| 70 |
def __init__(self): |
| 71 |
self.attributes = set() |
| 72 |
|
| 73 |
def visit_FunctionDef(self, node): |
| 74 |
if node.name == "__getattr__": |
| 75 |
# Not really a module member. |
| 76 |
return |
| 77 |
self.attributes.add(node.name) |
| 78 |
# Do not call self.generic_visit; we are only interested in |
| 79 |
# top-level functions. |
| 80 |
return |
| 81 |
|
| 82 |
def visit_ClassDef(self, node): |
| 83 |
if not node.name.startswith("_"): |
| 84 |
self.attributes.add(node.name) |
| 85 |
|
| 86 |
def visit_AnnAssign(self, node): |
| 87 |
self.attributes.add(node.target.id) |
| 88 |
|
| 89 |
|
| 90 |
def find_missing(module_name): |
| 91 |
module_path = os.path.join( |
| 92 |
NUMPY_ROOT, |
| 93 |
module_name.replace(".", os.sep), |
| 94 |
"__init__.pyi", |
| 95 |
) |
| 96 |
|
| 97 |
module = importlib.import_module(module_name) |
| 98 |
module_attributes = { |
| 99 |
attribute for attribute in dir(module) if not attribute.startswith("_") |
| 100 |
} |
| 101 |
|
| 102 |
if os.path.isfile(module_path): |
| 103 |
with open(module_path) as f: |
| 104 |
tree = ast.parse(f.read()) |
| 105 |
ast_visitor = FindAttributes() |
| 106 |
ast_visitor.visit(tree) |
| 107 |
stubs_attributes = ast_visitor.attributes |
| 108 |
else: |
| 109 |
# No stubs for this module yet. |
| 110 |
stubs_attributes = set() |
| 111 |
|
| 112 |
exclude_list = EXCLUDE_LIST.get(module_name, set()) |
| 113 |
|
| 114 |
missing = module_attributes - stubs_attributes - exclude_list |
| 115 |
print("\n".join(sorted(missing))) |
| 116 |
|
| 117 |
|
| 118 |
def main(): |
| 119 |
parser = argparse.ArgumentParser() |
| 120 |
parser.add_argument("module") |
| 121 |
args = parser.parse_args() |
| 122 |
|
| 123 |
find_missing(args.module) |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__": |
| 127 |
main() |