Return a string describing the signature of a callable object, or ''. For Python-coded functions and methods, the first line is introspected. Delete 'self' parameter for classes (.__init__) and bound methods. The next lines are the first lines of the doc string up to the first empty
(ob)
| 152 | _invalid_method = "invalid method signature" |
| 153 | |
| 154 | def get_argspec(ob): |
| 155 | '''Return a string describing the signature of a callable object, or ''. |
| 156 | |
| 157 | For Python-coded functions and methods, the first line is introspected. |
| 158 | Delete 'self' parameter for classes (.__init__) and bound methods. |
| 159 | The next lines are the first lines of the doc string up to the first |
| 160 | empty line or _MAX_LINES. For builtins, this typically includes |
| 161 | the arguments in addition to the return value. |
| 162 | ''' |
| 163 | # Determine function object fob to inspect. |
| 164 | try: |
| 165 | ob_call = ob.__call__ |
| 166 | except BaseException: # Buggy user object could raise anything. |
| 167 | return '' # No popup for non-callables. |
| 168 | # For Get_argspecTest.test_buggy_getattr_class, CallA() & CallB(). |
| 169 | fob = ob_call if isinstance(ob_call, types.MethodType) else ob |
| 170 | |
| 171 | # Initialize argspec and wrap it to get lines. |
| 172 | try: |
| 173 | argspec = str(inspect.signature(fob)) |
| 174 | except Exception as err: |
| 175 | msg = str(err) |
| 176 | if msg.startswith(_invalid_method): |
| 177 | return _invalid_method |
| 178 | else: |
| 179 | argspec = '' |
| 180 | |
| 181 | if isinstance(fob, type) and argspec == '()': |
| 182 | # If fob has no argument, use default callable argspec. |
| 183 | argspec = _default_callable_argspec |
| 184 | |
| 185 | lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT) |
| 186 | if len(argspec) > _MAX_COLS else [argspec] if argspec else []) |
| 187 | |
| 188 | # Augment lines from docstring, if any, and join to get argspec. |
| 189 | doc = inspect.getdoc(ob) |
| 190 | if doc: |
| 191 | for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]: |
| 192 | line = line.strip() |
| 193 | if not line: |
| 194 | break |
| 195 | if len(line) > _MAX_COLS: |
| 196 | line = line[: _MAX_COLS - 3] + '...' |
| 197 | lines.append(line) |
| 198 | argspec = '\n'.join(lines) |
| 199 | |
| 200 | return argspec or _default_callable_argspec |
| 201 | |
| 202 | |
| 203 | if __name__ == '__main__': |
no test coverage detected
searching dependent graphs…