sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

Link to Python parent class is not clickable when absolute reference is used

Open DareDevilDenis opened this issue 1 year ago • 1 comments

Describe the bug

My project has the following structure:

source
 |
 | __init__.py
 | package_a
 |  | __init__.py
 |  | common_module.py
 |  | module_a.py
 |
 | package_b
 |  | __init__.py
 |  | module_b.py

module_b.py has a class ChildClass that inherits from ParentClass in module_a.py

The problem is that in the HTML for module_b.py the reference to ParentClass is not clickable. However, the link to the propery of type CommonClass is clickable:

image

module_a.py has the content:

from dataclasses import dataclass

@dataclass
class ParentClass:
    """This is the parent class."""

module_b.py has the content:

from dataclasses import dataclass, field
from package_a.module_a import ParentClass
from package_a.common_module import CommonClass

@dataclass
class ChildClass(ParentClass):
    """This is ChildClass."""

    my_property: CommonClass = field(init=False)

source.package_b.module_b.rst has the content:

source.package\_b.module\_b module
==================================

.. automodule:: source.package_b.module_b
   :members:
   :undoc-members:
   :show-inheritance:

If the imports in module_b.py are changed from absolute references:

from package_a.module_a import ParentClass
from package_a.common_module import CommonClass

to relative references:

from ..package_a.module_a import ParentClass
from ..package_a.common_module import CommonClass

then the link to ParentClass becomes clickable, however we should not be forced to use relative references (in fact, the Ruff linter says "Prefer absolute imports over relative imports from parent module")

How to Reproduce

The attached file has this small project and the HTML output: python_parent_class_not_clickable.zip

Environment Information

Platform:              win32; (Windows-10-10.0.19045-SP0)
Python version:        3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)])
Python implementation: CPython
Sphinx version:        7.2.6
Docutils version:      0.20.1
Jinja2 version:        3.1.3
Pygments version:      2.17.2

Sphinx extensions

['sphinx.ext.autodoc', 'sphinx.ext.napoleon']

Additional context

No response

DareDevilDenis avatar Feb 08 '24 15:02 DareDevilDenis

Thank you for your report. I don't have time for checking this one now but it's interesting to know that relative imports seem to do the trick. At least it can help in the investigation.

EDIT: Since we already have quite a lot of issues marked with help wanted, I will not add it to this issue, until orher more important issues are solved.

picnixz avatar Feb 13 '24 23:02 picnixz