|
44 | 44 | """The supported methods to infer docstring styles.""" |
45 | 45 |
|
46 | 46 |
|
47 | | -_patterns = { |
| 47 | +_pattern_templates = { |
48 | 48 | Parser.google: ( |
49 | 49 | r"\n[ \t]*{0}:([ \t]+.+)?\n[ \t]+.+", |
50 | 50 | [ |
|
122 | 122 | ), |
123 | 123 | } |
124 | 124 |
|
| 125 | +_patterns = { |
| 126 | + style: re.compile( |
| 127 | + pattern.format(f"(?:{'|'.join(map(re.escape, replacements))})"), |
| 128 | + re.IGNORECASE | re.MULTILINE, |
| 129 | + ) |
| 130 | + for style, (pattern, replacements) in _pattern_templates.items() |
| 131 | +} |
| 132 | + |
125 | 133 |
|
126 | 134 | class PerStyleOptions(TypedDict, total=False): |
127 | 135 | """Per-style options for docstring parsing.""" |
@@ -168,15 +176,14 @@ def infer_docstring_style( |
168 | 176 |
|
169 | 177 | per_style_options = per_style_options or {} |
170 | 178 |
|
171 | | - style_order = [Parser(style) if isinstance(style, str) else style for style in style_order or _default_style_order] |
| 179 | + if not style_order: |
| 180 | + style_order = _default_style_order |
| 181 | + else: |
| 182 | + style_order = [Parser(style) if isinstance(style, str) else style for style in style_order] |
172 | 183 |
|
173 | 184 | if method == "heuristics": |
174 | 185 | for style in style_order: |
175 | | - pattern, replacements = _patterns[style] |
176 | | - patterns = [ |
177 | | - re.compile(pattern.format(replacement), re.IGNORECASE | re.MULTILINE) for replacement in replacements |
178 | | - ] |
179 | | - if any(pattern.search(docstring.value) for pattern in patterns): |
| 186 | + if _patterns[style].search(docstring.value): |
180 | 187 | return style, None |
181 | 188 | return default if default is None or isinstance(default, Parser) else Parser(default), None |
182 | 189 |
|
|
0 commit comments