Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #9384: autodoc_typehints='none' supports typehints for attributes #9386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions 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
Expand All @@ -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

Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -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',
'',
Expand All @@ -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',
'',
Expand Down Expand Up @@ -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',
'',
Expand All @@ -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',
'',
Expand Down