Extract and format a docstring for insertion into a stub file.
(name, module, indentation)
| 62 | |
| 63 | |
| 64 | def _get_docstring(name, module, indentation): |
| 65 | """Extract and format a docstring for insertion into a stub file.""" |
| 66 | obj, parent, obj_name = _resolve_object(module, name) |
| 67 | if obj is None: |
| 68 | print(f"{name} not found in {module.__name__}") |
| 69 | return None |
| 70 | |
| 71 | docstring = inspect.getdoc(obj) |
| 72 | if not docstring: |
| 73 | return None |
| 74 | |
| 75 | # Remove signature prefix |
| 76 | parent_name = getattr(parent, "__name__", None) if parent else None |
| 77 | if docstring.startswith(obj_name) or ( |
| 78 | parent_name and docstring.startswith(f"{parent_name}.{obj_name}") |
| 79 | ): |
| 80 | docstring = "\n".join(docstring.splitlines()[2:]) |
| 81 | |
| 82 | # Skip empty docstrings |
| 83 | if not docstring.strip(): |
| 84 | return None |
| 85 | |
| 86 | prefix = " " * indentation |
| 87 | return '"""\n' + indent(docstring + '\n"""', prefix) |
| 88 | |
| 89 | |
| 90 | class DocstringInserter(libcst.CSTTransformer): |
no test coverage detected