diff --git a/CHANGES b/CHANGES index 432e114a0a..75f28ba2cd 100644 --- a/CHANGES +++ b/CHANGES @@ -37,6 +37,8 @@ Features added * #3014: autodoc: Add :event:`autodoc-process-bases` to modify the base classes of the class definitions * #9272: autodoc: Render enum values for the default argument value better +* #9384: autodoc: ``autodoc_typehints='none'`` now erases typehints for + variables, attributes and properties * #3257: autosummary: Support instance attributes for classes * #9129: html search: Show search summaries when html_copy_source = False * #9307: html search: Prevent corrections and completions in search field diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 7cf06752d7..1cecb1f797 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1982,11 +1982,13 @@ def add_directive_header(self, sig: str) -> None: self.add_line(' :annotation: %s' % self.options.annotation, sourcename) else: - # obtain annotation for this data - annotations = get_type_hints(self.parent, None, self.config.autodoc_type_aliases) - if self.objpath[-1] in annotations: - objrepr = stringify_typehint(annotations.get(self.objpath[-1])) - self.add_line(' :type: ' + objrepr, sourcename) + if self.config.autodoc_typehints != 'none': + # obtain annotation for this data + annotations = get_type_hints(self.parent, None, + self.config.autodoc_type_aliases) + if self.objpath[-1] in annotations: + objrepr = stringify_typehint(annotations.get(self.objpath[-1])) + self.add_line(' :type: ' + objrepr, sourcename) try: if self.options.no_value or self.should_suppress_value_header(): @@ -2584,11 +2586,13 @@ def add_directive_header(self, sig: str) -> None: elif self.options.annotation: self.add_line(' :annotation: %s' % self.options.annotation, sourcename) else: - # obtain type annotation for this attribute - annotations = get_type_hints(self.parent, None, self.config.autodoc_type_aliases) - if self.objpath[-1] in annotations: - objrepr = stringify_typehint(annotations.get(self.objpath[-1])) - self.add_line(' :type: ' + objrepr, sourcename) + if self.config.autodoc_typehints != 'none': + # obtain type annotation for this attribute + annotations = get_type_hints(self.parent, None, + self.config.autodoc_type_aliases) + if self.objpath[-1] in annotations: + objrepr = stringify_typehint(annotations.get(self.objpath[-1])) + self.add_line(' :type: ' + objrepr, sourcename) try: if self.options.no_value or self.should_suppress_value_header(): @@ -2672,7 +2676,7 @@ def add_directive_header(self, sig: str) -> None: if inspect.isabstractmethod(self.object): self.add_line(' :abstractmethod:', sourcename) - if safe_getattr(self.object, 'fget', None): + if safe_getattr(self.object, 'fget', None) and self.config.autodoc_typehints != 'none': try: signature = inspect.signature(self.object.fget, type_aliases=self.config.autodoc_type_aliases) diff --git a/tests/roots/test-ext-autodoc/target/typehints.py b/tests/roots/test-ext-autodoc/target/typehints.py index bb56054c30..9e15043998 100644 --- a/tests/roots/test-ext-autodoc/target/typehints.py +++ b/tests/roots/test-ext-autodoc/target/typehints.py @@ -1,5 +1,8 @@ from typing import Any, Tuple, Union +CONST1: int +CONST2: int = 1 + def incr(a: int, b: int = 1) -> int: return a + b @@ -11,6 +14,9 @@ def decr(a, b = 1): class Math: + CONST1: int + CONST2: int = 1 + def __init__(self, s: str, o: Any = None) -> None: pass @@ -32,6 +38,10 @@ def horse(self, # type: (...) -> None return + @property + def prop(self) -> int: + return 0 + def tuple_args(x: Tuple[int, Union[int, str]]) -> Tuple[int, int]: pass diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index e0f08ea770..0f05804043 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -554,10 +554,26 @@ def test_autodoc_typehints_signature(app): '.. py:module:: target.typehints', '', '', + '.. py:data:: CONST1', + ' :module: target.typehints', + ' :type: int', + '', + '', '.. py:class:: Math(s: str, o: Optional[Any] = None)', ' :module: target.typehints', '', '', + ' .. py:attribute:: Math.CONST1', + ' :module: target.typehints', + ' :type: int', + '', + '', + ' .. py:attribute:: Math.CONST2', + ' :module: target.typehints', + ' :type: int', + ' :value: 1', + '', + '', ' .. py:method:: Math.decr(a: int, b: int = 1) -> int', ' :module: target.typehints', '', @@ -574,6 +590,11 @@ def test_autodoc_typehints_signature(app): ' :module: target.typehints', '', '', + ' .. py:property:: Math.prop', + ' :module: target.typehints', + ' :type: int', + '', + '', '.. py:class:: NewAnnotation(i: int)', ' :module: target.typehints', '', @@ -620,10 +641,23 @@ def test_autodoc_typehints_none(app): '.. py:module:: target.typehints', '', '', + '.. py:data:: CONST1', + ' :module: target.typehints', + '', + '', '.. py:class:: Math(s, o=None)', ' :module: target.typehints', '', '', + ' .. py:attribute:: Math.CONST1', + ' :module: target.typehints', + '', + '', + ' .. py:attribute:: Math.CONST2', + ' :module: target.typehints', + ' :value: 1', + '', + '', ' .. py:method:: Math.decr(a, b=1)', ' :module: target.typehints', '', @@ -640,6 +674,10 @@ def test_autodoc_typehints_none(app): ' :module: target.typehints', '', '', + ' .. py:property:: Math.prop', + ' :module: target.typehints', + '', + '', '.. py:class:: NewAnnotation(i)', ' :module: target.typehints', '',