Parse docstring into components.
(docstring: str | None)
| 165 | |
| 166 | |
| 167 | def parse_docstring(docstring: str | None) -> dict[str, Any]: |
| 168 | """Parse docstring into components.""" |
| 169 | if not docstring: |
| 170 | return { |
| 171 | "description": "", |
| 172 | "params": {}, |
| 173 | "returns": None, |
| 174 | "examples": [], |
| 175 | "deprecated": None, |
| 176 | } |
| 177 | |
| 178 | lines = docstring.strip().split("\n") |
| 179 | description_lines = [] |
| 180 | params: dict[str, str] = {} |
| 181 | returns = None |
| 182 | examples: list[str] = [] |
| 183 | deprecated_lines: list[str] = [] |
| 184 | |
| 185 | section = "description" |
| 186 | current_param = None |
| 187 | example_lines: list[str] = [] |
| 188 | |
| 189 | for line in lines: |
| 190 | stripped = line.strip() |
| 191 | |
| 192 | # Check for a ``.. deprecated::`` directive (reStructuredText). |
| 193 | if re.match(r"\.\.\s+deprecated::", stripped): |
| 194 | section = "deprecated" |
| 195 | rest = re.sub(r"\.\.\s+deprecated::\s*", "", stripped).strip() |
| 196 | if rest: |
| 197 | deprecated_lines.append(rest) |
| 198 | continue |
| 199 | |
| 200 | # Check for :param name: description |
| 201 | param_match = re.match(r":param\s+(\w+):\s*(.*)", stripped) |
| 202 | if param_match: |
| 203 | section = "params" |
| 204 | current_param = param_match.group(1) |
| 205 | params[current_param] = param_match.group(2) |
| 206 | continue |
| 207 | |
| 208 | # Check for :returns: or :return: |
| 209 | return_match = re.match(r":returns?:\s*(.*)", stripped) |
| 210 | if return_match: |
| 211 | section = "returns" |
| 212 | returns = return_match.group(1) |
| 213 | continue |
| 214 | |
| 215 | # Check for Example section |
| 216 | if stripped.lower().startswith("example"): |
| 217 | section = "examples" |
| 218 | continue |
| 219 | |
| 220 | # Add to appropriate section |
| 221 | if section == "description": |
| 222 | description_lines.append(stripped) |
| 223 | elif section == "params" and current_param and stripped: |
| 224 | params[current_param] += " " + stripped |
no test coverage detected
searching dependent graphs…