|
24 | 24 | from docutils import nodes |
25 | 25 | from docutils.statemachine import ViewList |
26 | 26 |
|
| 27 | +signature = Signature = None |
| 28 | + |
27 | 29 | if sphinx.__version__ >= '1.7': |
28 | | - from sphinx.ext.autodoc import Signature, get_documenters |
| 30 | + from sphinx.ext.autodoc import get_documenters |
29 | 31 | from sphinx.ext.autodoc.directive import ( |
30 | 32 | AutodocDirective, AUTODOC_DEFAULT_OPTIONS, DocumenterBridge, |
31 | 33 | process_documenter_options) |
| 34 | + try: |
| 35 | + from sphinx.util.inspect import signature, stringify_signature |
| 36 | + except ImportError: |
| 37 | + from sphinx.ext.autodoc import Signature |
| 38 | + |
32 | 39 | else: |
33 | 40 | from sphinx.ext.autodoc import ( |
34 | 41 | getargspec, formatargspec, AutoDirective as AutodocDirective, |
|
67 | 74 | 'ignore-module-all'} |
68 | 75 |
|
69 | 76 |
|
| 77 | +if signature is not None: # sphinx >= 2.4.0 |
| 78 | + def process_signature(obj): |
| 79 | + sig = signature(obj) |
| 80 | + return stringify_signature(sig) |
| 81 | +elif Signature is not None: # sphinx >= 1.7 |
| 82 | + def process_signature(obj): |
| 83 | + try: |
| 84 | + args = Signature(obj).format_args() |
| 85 | + except TypeError: |
| 86 | + return None |
| 87 | + else: |
| 88 | + args = args.replace('\\', '\\\\') |
| 89 | + return args |
| 90 | +else: |
| 91 | + def process_signature(obj): |
| 92 | + try: |
| 93 | + argspec = getargspec(obj) |
| 94 | + except TypeError: |
| 95 | + # still not possible: happens e.g. for old-style classes |
| 96 | + # with __call__ in C |
| 97 | + return None |
| 98 | + if argspec[0] and argspec[0][0] in ('cls', 'self'): |
| 99 | + del argspec[0][0] |
| 100 | + if sphinx_version < [1, 4]: |
| 101 | + return formatargspec(*argspec) |
| 102 | + else: |
| 103 | + return formatargspec(obj, *argspec) |
| 104 | + |
| 105 | + |
70 | 106 | class AutosummaryDocumenter(object): |
71 | 107 | """Abstract class for for extending Documenter methods |
72 | 108 |
|
@@ -276,26 +312,9 @@ def format_args(self): |
276 | 312 | if callmeth is None: |
277 | 313 | return None |
278 | 314 | if sphinx_version < [1, 7]: |
279 | | - try: |
280 | | - argspec = getargspec(callmeth) |
281 | | - except TypeError: |
282 | | - # still not possible: happens e.g. for old-style classes |
283 | | - # with __call__ in C |
284 | | - return None |
285 | | - if argspec[0] and argspec[0][0] in ('cls', 'self'): |
286 | | - del argspec[0][0] |
287 | | - if sphinx_version < [1, 4]: |
288 | | - return formatargspec(*argspec) |
289 | | - else: |
290 | | - return formatargspec(callmeth, *argspec) |
| 315 | + pass |
291 | 316 | else: |
292 | | - try: |
293 | | - args = Signature(callmeth).format_args() |
294 | | - except TypeError: |
295 | | - return None |
296 | | - else: |
297 | | - args = args.replace('\\', '\\\\') |
298 | | - return args |
| 317 | + return process_signature(callmeth) |
299 | 318 |
|
300 | 319 | def get_doc(self, encoding=None, ignore=1): |
301 | 320 | """Reimplemented to include data from the call method""" |
@@ -339,27 +358,7 @@ def format_args(self): |
339 | 358 | callmeth = self.get_attr(self.object, '__call__', None) |
340 | 359 | if callmeth is None: |
341 | 360 | return None |
342 | | - if sphinx_version < [1, 7]: |
343 | | - try: |
344 | | - argspec = getargspec(callmeth) |
345 | | - except TypeError: |
346 | | - # still not possible: happens e.g. for old-style classes |
347 | | - # with __call__ in C |
348 | | - return None |
349 | | - if argspec[0] and argspec[0][0] in ('cls', 'self'): |
350 | | - del argspec[0][0] |
351 | | - if sphinx_version < [1, 4]: |
352 | | - return formatargspec(*argspec) |
353 | | - else: |
354 | | - return formatargspec(callmeth, *argspec) |
355 | | - else: |
356 | | - try: |
357 | | - args = Signature(callmeth).format_args() |
358 | | - except TypeError: |
359 | | - return None |
360 | | - else: |
361 | | - args = args.replace('\\', '\\\\') |
362 | | - return args |
| 361 | + return process_signature(callmeth) |
363 | 362 |
|
364 | 363 | def get_doc(self, encoding=None, ignore=1): |
365 | 364 | """Reimplemented to include data from the call method""" |
|
0 commit comments