sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.63k stars 2.13k forks source link

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

Open DareDevilDenis opened 9 months ago

DareDevilDenis commented 9 months ago

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

picnixz commented 9 months ago

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.