Skip to content

Commit f4251dd

Browse files
committed
REF: Make pdoc.Doc.obj point to raw property/descriptor
1 parent a5bf43b commit f4251dd

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pdoc/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ def _is_descriptor(obj):
367367
inspect.ismemberdescriptor(obj))
368368

369369

370+
def _unwrap_descriptor(obj):
371+
if isinstance(obj, property):
372+
return (getattr(obj, 'fget', False) or
373+
getattr(obj, 'fset', False) or
374+
getattr(obj, 'fdel', obj))
375+
return getattr(obj, '__get__', obj)
376+
377+
370378
def _filter_type(type: Type[T],
371379
values: Union[Iterable['Doc'], Mapping[str, 'Doc']]) -> List[T]:
372380
"""
@@ -488,7 +496,7 @@ def source(self) -> str:
488496
available, an empty string.
489497
"""
490498
try:
491-
lines, _ = inspect.getsourcelines(self.obj)
499+
lines, _ = inspect.getsourcelines(_unwrap_descriptor(self.obj))
492500
except (ValueError, TypeError, OSError):
493501
return ''
494502
return inspect.cleandoc(''.join(['\n'] + lines))
@@ -1002,7 +1010,7 @@ def definition_order_index(
10021010
(inspect.isclass(obj) or _is_descriptor(obj)) and inspect.getdoc(obj)),
10031011
cls=self,
10041012
kind=kind,
1005-
obj=getattr(obj, 'fget', getattr(obj, '__get__', None)),
1013+
obj=_is_descriptor(obj) and obj or None,
10061014
instance_var=(_is_descriptor(obj) or
10071015
name in getattr(self.obj, '__slots__', ())))
10081016

@@ -1277,7 +1285,8 @@ def return_annotation(self, *, link=None) -> str:
12771285
lambda: _get_type_hints(cast(Class, self.cls).obj)[self.name],
12781286
# global variables
12791287
lambda: _get_type_hints(not self.cls and self.module.obj)[self.name],
1280-
lambda: inspect.signature(self.obj).return_annotation,
1288+
# properties
1289+
lambda: inspect.signature(_unwrap_descriptor(self.obj)).return_annotation,
12811290
# Use raw annotation strings in unmatched forward declarations
12821291
lambda: cast(Class, self.cls).obj.__annotations__[self.name],
12831292
# Extract annotation from the docstring for C builtin function

0 commit comments

Comments
 (0)