mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #9386 from tk0miya/9384_autodoc_typehints_supports_attributes
Fix #9384: autodoc_typehints='none' supports typehints for attributes
This commit is contained in:
commit
54c6c666c1
2
CHANGES
2
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
|
||||
|
@ -1982,11 +1982,13 @@ class DataDocumenter(GenericAliasMixin, NewTypeMixin, TypeVarMixin,
|
||||
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 @@ class AttributeDocumenter(GenericAliasMixin, NewTypeMixin, SlotsMixin, # type:
|
||||
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 @@ class PropertyDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): #
|
||||
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)
|
||||
|
@ -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 @@ class Math:
|
||||
# type: (...) -> None
|
||||
return
|
||||
|
||||
@property
|
||||
def prop(self) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def tuple_args(x: Tuple[int, Union[int, str]]) -> Tuple[int, int]:
|
||||
pass
|
||||
|
@ -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',
|
||||
'',
|
||||
|
Loading…
Reference in New Issue
Block a user