Skip to content

Commit 7554f60

Browse files
author
Joel Gibson
committed
Allow method_descriptors to be serialized as methods
1 parent c4b58a6 commit 7554f60

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

Diff for: src/pytkdocs/loader.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ def is_method(self) -> bool:
173173
function_type = type(lambda: None)
174174
return self.parent_is_class() and isinstance(self.obj, function_type)
175175

176+
def is_method_descriptor(self) -> bool:
177+
"""
178+
Tell if this node's object is a method descriptor.
179+
180+
Built-in methods (e.g. those implemented in C/Rust) are often
181+
method descriptors, rather than normal methods.
182+
183+
Returns:
184+
If this node's object is a method descriptor.
185+
"""
186+
return inspect.ismethoddescriptor(self.obj)
187+
176188
def is_staticmethod(self) -> bool:
177189
"""
178190
Tell if this node's object is a staticmethod.
@@ -360,6 +372,8 @@ def get_object_documentation(self, dotted_path: str, members: Optional[Union[Set
360372
root_object = self.get_staticmethod_documentation(leaf)
361373
elif leaf.is_classmethod():
362374
root_object = self.get_classmethod_documentation(leaf)
375+
elif leaf.is_method_descriptor():
376+
root_object = self.get_regular_method_documentation(leaf)
363377
elif leaf.is_method():
364378
root_object = self.get_regular_method_documentation(leaf)
365379
elif leaf.is_function():
@@ -792,7 +806,7 @@ def get_regular_method_documentation(self, node: ObjectNode) -> Method:
792806

793807
def get_method_documentation(self, node: ObjectNode, properties: Optional[List[str]] = None) -> Method:
794808
"""
795-
Get the documentation for a method.
809+
Get the documentation for a method or method descriptor.
796810
797811
Arguments:
798812
node: The node representing the method and its parents.
@@ -819,12 +833,23 @@ def get_method_documentation(self, node: ObjectNode, properties: Optional[List[s
819833
else:
820834
properties.append("async")
821835

836+
try:
837+
# for "built-in" functions, e.g. those implemented in C,
838+
# inspect.signature() uses the __text_signature__ attribute, which
839+
# provides a limited but still useful amount of signature information.
840+
# "built-in" functions with no __text_signature__ will
841+
# raise a ValueError().
842+
signature = inspect.signature(method)
843+
except ValueError as error:
844+
self.errors.append(f"Couldn't read signature for '{path}': {error}")
845+
signature = None
846+
822847
return Method(
823848
name=node.name,
824849
path=path,
825850
file_path=node.file_path,
826851
docstring=inspect.getdoc(method),
827-
signature=inspect.signature(method),
852+
signature=signature,
828853
properties=properties or [],
829854
source=source,
830855
)

0 commit comments

Comments
 (0)