Open picnixz opened 1 year ago
After some inspection, sphinx.util.get_full_modname
may also have an issue where the module of a partial function is incorrectly guessed. I am not very sure whether the issue is related to my personal extensions or whether it is what should be expected from.
I think we (or I) need to investigate a bit more in that direction. For instance sphinx.util.inspect.getdoc
correctly extracts the docstring from a partial function so there might have similar issues elsewhere.
Describe the bug
Currently,
sphinx.ext.viewcode
allows to jump to sourcecode of a class, method, or function but not to aproperty
or a class attribute (assuming that the latter has a type comment or an annotation). Using the example below, the tags found bysphinx.pycode.ModuleAnalyzer
areIn particular, the definition of the property
bar
is detected but it will not be shown when rendered. This can be avoided ifviewcode_follow_imported_members = False
(which is by defaultTrue
) but I feel that this should not be the real purpose. Now, with the default valueTrue
, the following code is executedhttps://github.com/sphinx-doc/sphinx/blob/669bcc0a190ce9921451bad28431886fbaad170b/sphinx/ext/viewcode.py#L113-L119
However, at the end of the block,
modname
isNone
if no handler is specified ! the reason is that_get_full_modname
callssphinx.util.get_full_modname
which indeed finds the correct member. However,https://github.com/sphinx-doc/sphinx/blob/669bcc0a190ce9921451bad28431886fbaad170b/sphinx/util/__init__.py#L196-L210
returns the
__module__
field which is usuallyNone
for properties and class attributes. With the example below, we get:value
fullname
modname
new_modname
<class 'class.MyClass'>
'MyClass'
class
class
<property object at 0x...>
'MyClass.bar'
class
None
<function MyClass.baz at 0x...>
'MyClass.baz'
class
class
0
'MyClass.foo'
class
None
As we can observe, because the module of
MyClass.bar
andMyClass.foo
were not properly deduced, they are no more considered as entries with a possible tag. ForMyClass.foo
, this is expected since the definition is not even detected, however forMyClass.bar
, this is unexpected.Fix for properties
For properties, this report should be categorized as a "bug" since the definition is properly detected but never handled. One possible fix is to have an event handler for
viewcode-follow-imported
implemented as follows:One issue with the above fix is that it may conflict with other handlers, so it might not be a universal patch.
Feature for class attributes
Even if the
tags
dictionary contains the class attribute fullname, the issue that we had with properties would subsist since the module name is incorrectly detected. It could be patched using the same patch, but some conflicts with 3rd party extensions may arise as well.In order to detect the definition location of a class attribute, one can easily extend the current
sphinx.pycode.ModuleAnalyzer
andsphinx.pycode.parser.VariableCommentPicker
as follows:Then, we would implement the following event handlers:
How to Reproduce
class.py
index.rst
conf.py
Environment Information
Sphinx extensions
Additional context
No response