MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _grab_overloads

Function _grab_overloads

tools/generate_proxy_methods.py:133–175  ·  tools/generate_proxy_methods.py::_grab_overloads

grab @overload entries for a function, assuming black-formatted code ;) so that we can do a simple regex

(fn)

Source from the content-addressed store, hash-verified

131
132
133def _grab_overloads(fn):
134 class="st">"""grab @overload entries for a function, assuming black-formatted
135 code ;) so that we can do a simple regex
136
137 class="st">"""
138
139 class="cm"># functions that use @util.deprecated and whatnot will have a string
140 class="cm"># generated fn. we can look at __wrapped__ but these functions don't
141 class="cm"># have any overloads in any case right now so skip
142 if fn.__code__.co_filename == class="st">"<string>":
143 return []
144
145 with open(fn.__code__.co_filename) as f:
146 lines = [l for i, l in zip(range(fn.__code__.co_firstlineno), f)]
147
148 lines.reverse()
149
150 output = []
151
152 current_ov = []
153 for line in lines[1:]:
154 current_ov.append(line)
155 outside_block_match = re.match(rclass="st">"^\w", line)
156 if outside_block_match:
157 current_ov[:] = []
158 break
159
160 fn_match = re.match(rclass="st">"^ (?: )?(?:async )?def (.*)\(", line)
161 if fn_match and fn_match.group(1) != fn.__name__:
162 current_ov[:] = []
163 break
164
165 ov_match = re.match(rclass="st">"^ (?: )?@overload$", line)
166 if ov_match:
167 output.append(class="st">"".join(reversed(current_ov)))
168 current_ov[:] = []
169
170 if re.match(rclass="st">"^ if (?:typing\.)?TYPE_CHECKING:", line):
171 output.append(line)
172 current_ov[:] = []
173
174 output.reverse()
175 return output
176
177
178def process_class(

Callers 1

instrumentFunction · 0.85

Calls 5

openFunction · 0.85
reverseMethod · 0.45
appendMethod · 0.45
matchMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected