(doc: str)
| 94 | # As of Feb 2025, the automatic style detection in griffe is an Insiders feature. This |
| 95 | # code approximates it. |
| 96 | def _detect_docstring_style(doc: str) -> DocstringStyle: |
| 97 | scores: dict[DocstringStyle, int] = {"sphinx": 0, "numpy": 0, "google": 0} |
| 98 | |
| 99 | # Sphinx style detection: look for :param, :type, :return:, and :rtype: |
| 100 | sphinx_patterns = [r"^:param\s", r"^:type\s", r"^:return:", r"^:rtype:"] |
| 101 | for pattern in sphinx_patterns: |
| 102 | if re.search(pattern, doc, re.MULTILINE): |
| 103 | scores["sphinx"] += 1 |
| 104 | |
| 105 | # Numpy style detection: look for headers like 'Parameters', 'Returns', or 'Yields' followed by |
| 106 | # a dashed underline |
| 107 | numpy_patterns = [ |
| 108 | r"^Parameters\s*\n\s*-{3,}", |
| 109 | r"^Returns\s*\n\s*-{3,}", |
| 110 | r"^Yields\s*\n\s*-{3,}", |
| 111 | ] |
| 112 | for pattern in numpy_patterns: |
| 113 | if re.search(pattern, doc, re.MULTILINE): |
| 114 | scores["numpy"] += 1 |
| 115 | |
| 116 | # Google style detection: look for section headers with a trailing colon |
| 117 | google_patterns = [r"^(Args|Arguments):", r"^(Returns):", r"^(Raises):"] |
| 118 | for pattern in google_patterns: |
| 119 | if re.search(pattern, doc, re.MULTILINE): |
| 120 | scores["google"] += 1 |
| 121 | |
| 122 | max_score = max(scores.values()) |
| 123 | if max_score == 0: |
| 124 | return "google" |
| 125 | |
| 126 | # Priority order: sphinx > numpy > google in case of tie |
| 127 | styles: list[DocstringStyle] = ["sphinx", "numpy", "google"] |
| 128 | |
| 129 | for style in styles: |
| 130 | if scores[style] == max_score: |
| 131 | return style |
| 132 | |
| 133 | return "google" |
| 134 | |
| 135 | |
| 136 | @contextlib.contextmanager |
no outgoing calls
no test coverage detected