This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ropehints.py
133 lines (101 loc) · 3.89 KB
/
ropehints.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
import os.path
import re
import rope.base.oi.soi
from rope.base.pyobjectsdef import PyModule
def update_type_for(project, source, resource):
pass
def infer_parameter_objects_with_hints(func):
def inner(pyfunction):
params_types = func(pyfunction)
try:
hintdb = pyfunction.pycore.hintdb
except AttributeError:
return params_types
param_names = pyfunction.get_param_names(False)
for i, name in enumerate(param_names):
ptype = hintdb.get_function_param_type(pyfunction, name)
if ptype != None:
params_types[i] = ptype
return params_types
return inner
rope.base.oi.soi.infer_parameter_objects = infer_parameter_objects_with_hints(
rope.base.oi.soi.infer_parameter_objects)
def infer_returned_object_with_hints(func):
def inner(pyfunction, args):
try:
hintdb = pyfunction.pycore.hintdb
except AttributeError:
return func(pyfunction, args)
rtype = hintdb.get_function_param_type(pyfunction, 'return')
if rtype is None:
rtype = func(pyfunction, args)
return rtype
return inner
rope.base.oi.soi.infer_returned_object = infer_returned_object_with_hints(
rope.base.oi.soi.infer_returned_object)
class HintDb(object):
def __init__(self, project):
self.type_cache = {}
project.pycore.hintdb = self
def get_function_param_type(self, pyfunc, name):
scope_path = self.get_scope_path(pyfunc.get_scope())
type_name = self.find_type_for(scope_path, name)
if type_name:
return self.get_type(pyfunc.pycore, type_name)
else:
return None
def get_scope_path(self, scope):
result = []
current_scope = scope
while current_scope is not None:
pyobj = current_scope.pyobject
if isinstance(pyobj, PyModule):
name = pyobj.pycore.modname(pyobj.resource)
else:
name = pyobj.get_name()
result.insert(0, name)
current_scope = current_scope.parent
return '.'.join(result)
def find_type_for(scope_path, name):
return None
def get_type(self, pycore, type_name):
try:
return self.type_cache[type_name]
except KeyError:
pass
module, sep, name = type_name.rpartition('.')
if module:
module = pycore.get_module(module)
obj = module[name].get_object()
else:
obj = pycore.get_module(name)
self.type_cache[type_name] = obj
return obj
class ReHintDb(HintDb):
def __init__(self, project):
super(ReHintDb, self).__init__(project)
self.db = []
def add_hint(self, scope, name, object_type):
self.db.append((re.compile(scope), re.compile(name), object_type))
def find_type_for(self, scope_path, name):
for scope, vname, otype in self.db:
if scope.search(scope_path) and vname.search(name):
return otype
return None
class FileHintDb(ReHintDb):
def __init__(self, project):
super(FileHintDb, self).__init__(project)
self.hints_filename = os.path.join(project.ropefolder.real_path, 'hints')
self.load_hints()
def refresh(self):
self.load_hints()
def load_hints(self):
self.db[:] = []
if os.path.exists(self.hints_filename):
with open(self.hints_filename) as f:
for l in f:
try:
scope, name, type = l.strip().split()
self.add_hint(scope, name, type)
except ValueError:
continue