Skip to content

Commit 9b42560

Browse files
committed
support pandas-style default spec by postprocessing tokens
1 parent 274d9fe commit 9b42560

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

sphinx/ext/napoleon/docstring.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,17 @@ def combine_set(tokens):
845845

846846

847847
def _tokenize_type_spec(spec: str) -> List[str]:
848+
def postprocess(item):
849+
if item.startswith("default"):
850+
return [item[:7], item[7:]]
851+
else:
852+
return [item]
853+
848854
tokens = list(
849855
item
850-
for item in _token_regex.split(spec)
851-
if item is not None and item.strip()
856+
for raw_token in _token_regex.split(spec)
857+
for item in postprocess(raw_token)
858+
if item
852859
)
853860
return tokens
854861

tests/test_ext_napoleon_docstring.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ def test_tokenize_type_spec(self):
20132013
"int or float or None, optional",
20142014
'{"F", "C", "N"}',
20152015
"{'F', 'C', 'N'}, default: 'F'",
2016+
"{'F', 'C', 'N or C'}, default 'F'",
20162017
'"ma{icious"',
20172018
r"'with \'quotes\''",
20182019
)
@@ -2022,6 +2023,7 @@ def test_tokenize_type_spec(self):
20222023
["int", " or ", "float", " or ", "None", ", ", "optional"],
20232024
["{", '"F"', ", ", '"C"', ", ", '"N"', "}"],
20242025
["{", "'F'", ", ", "'C'", ", ", "'N'", "}", ", ", "default", ": ", "'F'"],
2026+
["{", "'F'", ", ", "'C'", ", ", "'N or C'", "}", ", ", "default", " ", "'F'"],
20252027
['"ma{icious"'],
20262028
[r"'with \'quotes\''"],
20272029
)

0 commit comments

Comments
 (0)