diff options
author | Joost van Zwieten <joostvanzwieten@gmail.com> | 2016-07-29 12:27:00 +0200 |
---|---|---|
committer | Joost van Zwieten <joostvanzwieten@gmail.com> | 2016-08-07 16:18:22 +0200 |
commit | 099b58f7fee7d4688faebace615e781245bf20b8 (patch) | |
tree | 84863c6d1e33d0a666e6934d845bcee910774f46 | |
parent | da7e2d9dda35714111cdc98ba07b3a75d7d3886f (diff) | |
download | sphinx-git-099b58f7fee7d4688faebace615e781245bf20b8.tar.gz |
ext.autodoc: fix formatting instance annotations
Currently `format_annotation` fails on instances, because instances don't have
`__module__` and `__qualname__` attributes. Defer building the
`qualified_name` of an annotation until we have established that the annotation
is a type.
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | sphinx/ext/autodoc.py | 7 | ||||
-rw-r--r-- | tests/test_autodoc.py | 5 | ||||
-rw-r--r-- | tests/typing_test_data.py | 8 |
4 files changed, 17 insertions, 4 deletions
@@ -15,6 +15,7 @@ Bugs fixed * #2778: Fix autodoc crashes if obj.__dict__ is a property method and raises exception * Fix duplicated toc in epub3 output. * #2775: Fix failing linkcheck with servers not supporting identidy encoding +* #2833: Fix formatting instance annotations in ext.autodoc. Release 1.4.5 (released Jul 13, 2016) ===================================== diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py index 6ae0966fb..08bc99cdf 100644 --- a/sphinx/ext/autodoc.py +++ b/sphinx/ext/autodoc.py @@ -261,12 +261,13 @@ def format_annotation(annotation): Displaying complex types from ``typing`` relies on its private API. """ + if not isinstance(annotation, type): + return repr(annotation) + qualified_name = (annotation.__module__ + '.' + annotation.__qualname__ if annotation else repr(annotation)) - if not isinstance(annotation, type): - return repr(annotation) - elif annotation.__module__ == 'builtins': + if annotation.__module__ == 'builtins': return annotation.__qualname__ elif typing: if isinstance(annotation, typing.TypeVar): diff --git a/tests/test_autodoc.py b/tests/test_autodoc.py index 747746478..fca3c2b9e 100644 --- a/tests/test_autodoc.py +++ b/tests/test_autodoc.py @@ -1025,7 +1025,7 @@ def test_type_hints(): from sphinx.util.inspect import getargspec try: - from typing_test_data import f0, f1, f2, f3, f4, f5, f6, f7, f8 + from typing_test_data import f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 except (ImportError, SyntaxError): raise SkipTest('Cannot import Python code with function annotations') @@ -1061,3 +1061,6 @@ def test_type_hints(): # Tuple types verify_arg_spec(f8, '(x: typing.Tuple[int, str],' ' y: typing.Tuple[int, ...]) -> None') + + # Instance annotations + verify_arg_spec(f9, '(x: CustomAnnotation, y: 123) -> None') diff --git a/tests/typing_test_data.py b/tests/typing_test_data.py index 74a906ad1..461be7831 100644 --- a/tests/typing_test_data.py +++ b/tests/typing_test_data.py @@ -46,3 +46,11 @@ def f7(x: Callable[[int, str], int]) -> None: def f8(x: Tuple[int, str], y: Tuple[int, ...]) -> None: pass + + +class CustomAnnotation: + def __repr__(self): + return 'CustomAnnotation' + +def f9(x: CustomAnnotation(), y: 123) -> None: + pass |