From 44fbe9da7b13742d4ea14c15b5a3cd5fdeeade53 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 16 Nov 2020 02:11:15 +0900 Subject: [PATCH] Fix #8434: autodoc_type_aliases does not effect to variables --- CHANGES | 2 + sphinx/ext/autodoc/__init__.py | 6 ++- .../test-ext-autodoc/target/annotations.py | 10 +++++ tests/test_ext_autodoc_configs.py | 40 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index a55a43687..6dd7a7420 100644 --- a/CHANGES +++ b/CHANGES @@ -38,6 +38,8 @@ Bugs fixed * #4606: autodoc: the location of the warning is incorrect for inherited method * #8105: autodoc: the signature of class constructor is incorrect if the class is decorated +* #8434: autodoc: :confval:`autodoc_type_aliases` does not effect to variables + and attributes Testing -------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 171a3e7b0..a79e540eb 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1702,7 +1702,8 @@ class DataDocumenter(ModuleLevelDocumenter): if not self.options.annotation: # obtain annotation for this data try: - annotations = get_type_hints(self.parent) + annotations = get_type_hints(self.parent, None, + self.config.autodoc_type_aliases) except NameError: # Failed to evaluate ForwardRef (maybe TYPE_CHECKING) annotations = safe_getattr(self.parent, '__annotations__', {}) @@ -2093,7 +2094,8 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): if not self.options.annotation: # obtain type annotation for this attribute try: - annotations = get_type_hints(self.parent) + annotations = get_type_hints(self.parent, None, + self.config.autodoc_type_aliases) except NameError: # Failed to evaluate ForwardRef (maybe TYPE_CHECKING) annotations = safe_getattr(self.parent, '__annotations__', {}) diff --git a/tests/roots/test-ext-autodoc/target/annotations.py b/tests/roots/test-ext-autodoc/target/annotations.py index 691176b08..e9ff2f604 100644 --- a/tests/roots/test-ext-autodoc/target/annotations.py +++ b/tests/roots/test-ext-autodoc/target/annotations.py @@ -4,6 +4,9 @@ from typing import overload myint = int +#: docstring +variable: myint + def sum(x: myint, y: myint) -> myint: """docstring""" @@ -23,3 +26,10 @@ def mult(x: float, y: float) -> float: def mult(x, y): """docstring""" return x, y + + +class Foo: + """docstring""" + + #: docstring + attr: myint diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index 3d1005ac0..4dff7c3db 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -700,6 +700,19 @@ def test_autodoc_type_aliases(app): '.. py:module:: target.annotations', '', '', + '.. py:class:: Foo()', + ' :module: target.annotations', + '', + ' docstring', + '', + '', + ' .. py:attribute:: Foo.attr', + ' :module: target.annotations', + ' :type: int', + '', + ' docstring', + '', + '', '.. py:function:: mult(x: int, y: int) -> int', ' mult(x: float, y: float) -> float', ' :module: target.annotations', @@ -712,6 +725,13 @@ def test_autodoc_type_aliases(app): '', ' docstring', '', + '', + '.. py:data:: variable', + ' :module: target.annotations', + ' :type: int', + '', + ' docstring', + '', ] # define aliases @@ -722,6 +742,19 @@ def test_autodoc_type_aliases(app): '.. py:module:: target.annotations', '', '', + '.. py:class:: Foo()', + ' :module: target.annotations', + '', + ' docstring', + '', + '', + ' .. py:attribute:: Foo.attr', + ' :module: target.annotations', + ' :type: myint', + '', + ' docstring', + '', + '', '.. py:function:: mult(x: myint, y: myint) -> myint', ' mult(x: float, y: float) -> float', ' :module: target.annotations', @@ -734,6 +767,13 @@ def test_autodoc_type_aliases(app): '', ' docstring', '', + '', + '.. py:data:: variable', + ' :module: target.annotations', + ' :type: myint', + '', + ' docstring', + '', ]