You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the sphinx.ext.autodoc.PropertyDocumenter does not fire the autodoc-before-process-signature event, and thus the sphinx.ext.autodoc.type_comments.update_annotations_using_type_comments event handler is never called for properties. Instead, this documenter solely relies on an existing return annotation, which may or may not exist
logger.warning(__("Failed to get a function signature for %s: %s"),
self.fullname, exc)
pass
exceptValueError:
pass
In particular, the bar property does not have a return annotation int because the type comment is never parsed. Note that PropertyDocumenter.add_directive_header is given as input the stringified signature of the object, but for properties, the signature does not include the return annotation. More precisely, when entering PropertyDocumenter.add_directive_header,
is now the correct signature and is used to extract return annotation for the property type. However, this signature is never given the chance to be updated using type comments since the autodoc-before-process-signature event is never fired.
How to Fix
This issue can be easily fixed by one of the following method:
Fire the autodoc-before-process-signature event directly in PropertyDocumenter.import_object method in order to update the signature of the real object. I fix this in my project as follows:
classCustomPropertyDocumenter(PropertyDocumenter):
option_spec: OptionSpec=PropertyDocumenter.option_spec.copy()
defdocument_members(self, *args, **kwargs):
return# do not support membersdefimport_object(self, raiseerror=None):
rv=super().import_object(raiseerror)
ifrv:
func=self._get_function()
# update underlying property's annotations if neededself.app.emit('autodoc-before-process-signature', func, False)
returnrvdef_get_function(self):
ifsafe_getattr(self.object, 'fget', None): # propertyreturnself.object.fgetifsafe_getattr(self.object, 'func', None): # cached_propertyreturnself.object.funcreturnNone
I think it makes sense to fire this event at the earliest possible stage since the directive options (which are added early during the generation stage) depend on the signature of the member being documented. In particular, autodoc-before-process-signature should be indeed fired before calling Documenter.format_signature, namely inbetween Documenter.import_object1 and Documenter.format_signature2:
Describe the bug
Consider the following code:
Currently, the
sphinx.ext.autodoc.PropertyDocumenter
does not fire theautodoc-before-process-signature
event, and thus thesphinx.ext.autodoc.type_comments.update_annotations_using_type_comments
event handler is never called for properties. Instead, this documenter solely relies on an existing return annotation, which may or may not existsphinx/sphinx/ext/autodoc/__init__.py
Lines 2731 to 2747 in 669bcc0
In particular, the
bar
property does not have a return annotationint
because the type comment is never parsed. Note thatPropertyDocumenter.add_directive_header
is given as input the stringified signature of the object, but for properties, the signature does not include the return annotation. More precisely, when enteringPropertyDocumenter.add_directive_header
,sphinx/sphinx/ext/autodoc/__init__.py
Lines 2716 to 2730 in 669bcc0
the real function is extracted from the descriptor after the signature string has been computed. In particular, the signature being computed at
sphinx/sphinx/ext/autodoc/__init__.py
Lines 2731 to 2735 in 669bcc0
is now the correct signature and is used to extract return annotation for the property type. However, this signature is never given the chance to be updated using type comments since the
autodoc-before-process-signature
event is never fired.How to Fix
This issue can be easily fixed by one of the following method:
autodoc-before-process-signature
event directly inPropertyDocumenter.import_object
method in order to update the signature of the real object. I fix this in my project as follows:I think it makes sense to fire this event at the earliest possible stage since the directive options (which are added early during the generation stage) depend on the signature of the member being documented. In particular,
autodoc-before-process-signature
should be indeed fired before callingDocumenter.format_signature
, namely inbetweenDocumenter.import_object
1 andDocumenter.format_signature
2:How to Reproduce
class.py
index.rst
conf.py
Environment Information
Sphinx extensions
['sphinx.ext.autodoc']
Additional context
Solving this issue may serve in solving #10396
Footnotes
https://github.com/sphinx-doc/sphinx/blob/669bcc0a190ce9921451bad28431886fbaad170b/sphinx/ext/autodoc/__init__.py#L878 ↩
https://github.com/sphinx-doc/sphinx/blob/669bcc0a190ce9921451bad28431886fbaad170b/sphinx/ext/autodoc/__init__.py#L930 ↩
The text was updated successfully, but these errors were encountered: