Skip to content

Commit bb8e248

Browse files
Backport PR #65399 on branch 3.0.x (DOC: fix source link for classes in the reference docs) (#65425)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent bce6815 commit bb8e248

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎doc/source/conf.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ def linkcode_resolve(domain, info) -> str | None:
667667
except AttributeError:
668668
return None
669669

670+
if isinstance(obj, type):
671+
if hasattr(obj, "_module_source"):
672+
obj.__module__, obj._module_source = obj._module_source, obj.__module__
673+
670674
try:
671675
fn = inspect.getsourcefile(inspect.unwrap(obj))
672676
except TypeError:
@@ -694,6 +698,9 @@ def linkcode_resolve(domain, info) -> str | None:
694698

695699
fn = os.path.relpath(fn, start=os.path.dirname(pandas.__file__))
696700

701+
if isinstance(obj, type) and hasattr(obj, "_module_source"):
702+
obj.__module__, obj._module_source = obj._module_source, obj.__module__
703+
697704
if "+" in version:
698705
return f"https://github.com/pandas-dev/pandas/blob/main/pandas/{fn}{linespec}"
699706
else:

‎pandas/util/_decorators.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,15 @@ def example():
526526

527527
def decorator(func: F) -> F:
528528
if module is not None:
529+
if isinstance(func, type):
530+
# Store the original module for classes to ensure linkcode_resolve
531+
# can resolve the true source location after re-exporting
532+
try:
533+
func._module_source = func.__module__ # type: ignore[attr-defined]
534+
except AttributeError:
535+
pass
536+
529537
func.__module__ = module
530-
return func
538+
return cast("F", func) # type: ignore[redundant-cast]
531539

532540
return decorator

0 commit comments

Comments
 (0)