Count docstrings in module, function, and class bodies.
(source)
| 23 | |
| 24 | |
| 25 | def _count_docstrings(source): |
| 26 | """Count docstrings in module, function, and class bodies.""" |
| 27 | tree = ast.parse(source) |
| 28 | count = 0 |
| 29 | for node in ast.walk(tree): |
| 30 | if isinstance(node, (ast.Module, ast.FunctionDef, |
| 31 | ast.AsyncFunctionDef, ast.ClassDef)): |
| 32 | if (node.body |
| 33 | and isinstance(node.body[0], ast.Expr) |
| 34 | and isinstance(node.body[0].value, ast.Constant) |
| 35 | and isinstance(node.body[0].value.value, str)): |
| 36 | count += 1 |
| 37 | return count |
| 38 | |
| 39 | |
| 40 | # TODO(GH-48970): Check stubs ARE present once annotations are complete |