Parse a Google-style docstring into structured sections. Args: docstring: The raw docstring text Returns: Dictionary with keys: description, args, returns, raises, examples, note
(docstring: str)
| 102 | |
| 103 | |
| 104 | def parse_google_docstring(docstring: str) -> dict[str, Any]: |
| 105 | """Parse a Google-style docstring into structured sections. |
| 106 | |
| 107 | Args: |
| 108 | docstring: The raw docstring text |
| 109 | |
| 110 | Returns: |
| 111 | Dictionary with keys: description, args, returns, raises, examples, note |
| 112 | """ |
| 113 | if not docstring: |
| 114 | return {} |
| 115 | |
| 116 | # Clean the docstring |
| 117 | cleaned = inspect.cleandoc(docstring) |
| 118 | |
| 119 | # Initialize sections |
| 120 | sections = { |
| 121 | "description": "", |
| 122 | "args": [], |
| 123 | "returns": "", |
| 124 | "raises": [], |
| 125 | "examples": "", |
| 126 | "note": "", |
| 127 | } |
| 128 | |
| 129 | # Split into sections based on Google-style headers |
| 130 | # Match "Args:", "Returns:", "Raises:", "Examples:", "Note:", etc. |
| 131 | section_pattern = r"^(Args?|Returns?|Raises?|Examples?|Note):\s*$" |
| 132 | |
| 133 | lines = cleaned.split("\n") |
| 134 | current_section = "description" |
| 135 | current_content = [] |
| 136 | |
| 137 | for line in lines: |
| 138 | # Check if this line is a section header |
| 139 | match = re.match(section_pattern, line.strip()) |
| 140 | if match: |
| 141 | # Save previous section content |
| 142 | if current_section == "description": |
| 143 | sections["description"] = clean_rst_markup( |
| 144 | "\n".join(current_content).strip() |
| 145 | ) |
| 146 | elif current_section in ("args", "raises"): |
| 147 | # Parse parameter list |
| 148 | sections[current_section] = parse_param_list("\n".join(current_content)) |
| 149 | elif current_section in ("returns", "examples", "note"): |
| 150 | sections[current_section] = clean_rst_markup( |
| 151 | "\n".join(current_content).strip() |
| 152 | ) |
| 153 | |
| 154 | # Start new section |
| 155 | current_section = ( |
| 156 | match.group(1).lower().rstrip("s") |
| 157 | ) # Normalize to singular |
| 158 | if current_section == "arg": |
| 159 | current_section = "args" |
| 160 | elif current_section == "return": |
| 161 | current_section = "returns" |
no test coverage detected
searching dependent graphs…