Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix properties and cached_properties failing when adding git links #451

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pdoc/html_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import textwrap
import traceback
from contextlib import contextmanager
from functools import partial, lru_cache
from functools import partial, lru_cache, cached_property
from typing import Callable, Match, Optional
from warnings import warn
import xml.etree.ElementTree as etree
Expand Down Expand Up @@ -564,20 +564,34 @@ def format_git_link(template: str, dobj: pdoc.Doc):
try:
if 'commit' in _str_template_fields(template):
commit = _git_head_commit()
abs_path = inspect.getfile(inspect.unwrap(dobj.obj))
obj = dobj.obj

# special handlers for properties, cached_properties, and tuples
if isinstance(obj, property):
obj = obj.fget
elif isinstance(obj, cached_property):
obj = obj.func
elif (
(hasattr(obj, '__class__') and obj.__class__.__name__ == '_tuplegetter')
or inspect.ismemberdescriptor(obj)
):
class_name = dobj.qualname.rsplit('.', 1)[0]
obj = getattr(dobj.module.obj, class_name)

abs_path = inspect.getfile(inspect.unwrap(obj))
path = _project_relative_path(abs_path)

# Urls should always use / instead of \\
if os.name == 'nt':
path = path.replace('\\', '/')

lines, start_line = inspect.getsourcelines(dobj.obj)
lines, start_line = inspect.getsourcelines(obj)
start_line = start_line or 1 # GH-296
end_line = start_line + len(lines) - 1
url = template.format(**locals())
return url
except Exception:
warn(f'format_git_link for {dobj.obj} failed:\n{traceback.format_exc()}')
warn(f'format_git_link for {obj} failed:\n{traceback.format_exc()}')
return None


Expand Down