Skip to content

Commit 34ece77

Browse files
committed
feat: add async property for coroutine functions
1 parent 229d7d2 commit 34ece77

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Diff for: src/pytkdocs/loader.py

+15
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def is_function(self) -> bool:
9191
"""Is this node's object a function?"""
9292
return inspect.isfunction(self.obj)
9393

94+
def is_coroutine_function(self) -> bool:
95+
"""Is this node's object a coroutine?"""
96+
return inspect.iscoroutinefunction(self.obj)
97+
9498
def is_property(self) -> bool:
9599
"""Is this node's object a property?"""
96100
return isinstance(self.obj, property)
@@ -460,13 +464,18 @@ def get_function_documentation(self, node: ObjectNode) -> Function:
460464
self.errors.append(f"Couldn't read source for '{path}': {error}")
461465
source = None
462466

467+
properties: List[str] = []
468+
if node.is_coroutine_function():
469+
properties.append("async")
470+
463471
return Function(
464472
name=node.name,
465473
path=node.dotted_path,
466474
file_path=node.file_path,
467475
docstring=inspect.getdoc(function),
468476
signature=signature,
469477
source=source,
478+
properties=properties,
470479
)
471480

472481
def get_property_documentation(self, node: ObjectNode) -> Attribute:
@@ -664,6 +673,12 @@ def get_method_documentation(self, node: ObjectNode, properties: Optional[List[s
664673
except TypeError:
665674
source = None
666675

676+
if node.is_coroutine_function():
677+
if properties is None:
678+
properties = ["async"]
679+
else:
680+
properties.append("async")
681+
667682
return Method(
668683
name=node.name,
669684
path=path,

Diff for: tests/fixtures/asyncio.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ClassContainingCoroutineMethod:
2+
async def coroutine_method(self) -> None:
3+
return
4+
5+
async def coroutine_function() -> None:
6+
return

Diff for: tests/test_loader.py

+28
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,31 @@ def test_unwrap_object_with_getattr_method_raising_exception():
409409
"""Try loading an object that defines a `__getattr__` method which raises an exception."""
410410
loader = Loader()
411411
loader.get_object_documentation("tests.fixtures.unwrap_getattr_raises")
412+
413+
414+
def test_loading_coroutine():
415+
"""Load documentation for a coroutine."""
416+
loader = Loader()
417+
obj = loader.get_object_documentation("tests.fixtures.asyncio.coroutine_function")
418+
assert "async" in obj.properties
419+
420+
421+
def test_loading_coroutine_method():
422+
"""Load documentation for a coroutine method."""
423+
loader = Loader()
424+
obj = loader.get_object_documentation("tests.fixtures.asyncio.ClassContainingCoroutineMethod.coroutine_method")
425+
assert "async" in obj.properties
426+
427+
428+
def test_loading_function_without_async_property():
429+
"""Load documentation for a function that is not a coroutine."""
430+
loader = Loader()
431+
obj = loader.get_object_documentation("tests.fixtures.the_package.the_module.the_function")
432+
assert "async" not in obj.properties
433+
434+
435+
def test_loading_method_without_async_property():
436+
"""Load documentation for a method that is not a coroutine."""
437+
loader = Loader()
438+
obj = loader.get_object_documentation("tests.fixtures.the_package.the_module.TheClass.the_method")
439+
assert "async" not in obj.properties

0 commit comments

Comments
 (0)