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

Function _grab_overloads

tools/generate_proxy_methods.py:133–175  ·  view source on GitHub ↗

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 """grab @overload entries for a function, assuming black-formatted
135 code ;) so that we can do a simple regex
136
137 """
138
139 # functions that use @util.deprecated and whatnot will have a string
140 # generated fn. we can look at __wrapped__ but these functions don't
141 # have any overloads in any case right now so skip
142 if fn.__code__.co_filename == "<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(r"^\w", line)
156 if outside_block_match:
157 current_ov[:] = []
158 break
159
160 fn_match = re.match(r"^ (?: )?(?: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(r"^ (?: )?@overload$", line)
166 if ov_match:
167 output.append("".join(reversed(current_ov)))
168 current_ov[:] = []
169
170 if re.match(r"^ 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