jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1224596?usp=email )

Change subject: IMPR: Add source links for properties
......................................................................

IMPR: Add source links for properties

Use sphinx.ext.linkcode module and add linkcode_resolve funktion to
docs/conf.py to enable a source link for properties.

This is a workarround for sphinx upstream issue
https://github.com/sphinx-doc/sphinx/issues/11279

Bug: T333762
Change-Id: I057e84d6f3745a0e58c01649a2f93df3b32fb930
---
M docs/conf.py
1 file changed, 60 insertions(+), 1 deletion(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/docs/conf.py b/docs/conf.py
index 1f695ea..c2572dc 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -1,6 +1,6 @@
 """Configuration file for Sphinx."""
 #
-# (C) Pywikibot team, 2014-2025
+# (C) Pywikibot team, 2014-2026
 #
 # Distributed under the terms of the MIT license.
 #
@@ -21,6 +21,7 @@
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use Path.resolve() to make it absolute, like shown here.
 #
+import inspect
 import os
 import re
 import sys
@@ -58,6 +59,7 @@
     'sphinx.ext.autosectionlabel',
     'sphinx.ext.autosummary',
     'sphinx.ext.extlinks',
+    'sphinx.ext.linkcode',
     'sphinx.ext.napoleon',
     'sphinx.ext.viewcode',
     'sphinxext.opengraph',
@@ -511,6 +513,63 @@
 }


+def linkcode_resolve(domain, info) -> str | None:
+    """Provide external Phabricator links for @property objects only.
+
+    This function uses ``sphinx.ext.linkcode`` and is a workaround for
+    the following Sphinx issue: `sphinx-doc/sphinx#11279
+    <https://github.com/sphinx-doc/sphinx/issues/11279>`_
+
+    ..note: These links point directly to diffusion repository.
+    .. seealso:: :phab:`T333762`
+    .. versionadded:: 11.0
+    """
+    if domain != 'py':
+        return None
+
+    module_name = info.get('module')
+    fullname = info.get('fullname')
+    if not (module_name and fullname):
+        return None
+
+    # Load the module
+    module = sys.modules.get(module_name)
+    if module is None:
+        return None
+
+    # Traverse the object
+    obj = module
+    for part in fullname.split('.'):
+        if isinstance(obj, type):
+            obj = vars(obj).get(part)
+        else:
+            obj = getattr(obj, part, None)
+
+        if obj is None:
+            return None
+
+    # Only generate link for properties
+    if not isinstance(obj, property):
+        return None
+
+    # Use the getter function for source
+    obj = obj.fget
+
+    source_file = inspect.getsourcefile(obj)
+
+    # Convert absolute path to repo-relative path
+    repo_root = Path(__file__).resolve().parents[1]
+    relative_path = Path(source_file).resolve().relative_to(repo_root)
+
+    _, lineno = inspect.getsourcelines(obj)
+
+    # Build Phabricator URL
+    return (
+        'https://phabricator.wikimedia.org/diffusion/PWBC/browse/master/'
+        f'{relative_path}#L{lineno}'
+    )
+
+
 def pywikibot_docstring_fixups(app, what, name, obj, options, lines) -> None:
     """Remove plain 'Initializer.' or 'Allocator.' docstring.


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1224596?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I057e84d6f3745a0e58c01649a2f93df3b32fb930
Gerrit-Change-Number: 1224596
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: BinĂ¡ris <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to