Add support for setting default value in type declaration for numpydoc, various fixes to numpydoc compose & unit tests.#109
Merged
mauvilsa merged 10 commits intorr-:masterfrom Mar 17, 2026
Conversation
…ydoc. Fix regex for default specified in description. Fix composition test for parameters docstring. Add unit tests that test regex matching independently
…arsed test string docstring has DocstringMeta obj if expected
mauvilsa
requested changes
Mar 16, 2026
mauvilsa
requested changes
Mar 16, 2026
Collaborator
mauvilsa
left a comment
There was a problem hiding this comment.
Some comments from Codex. Not sure if they are true. But please have a look:
-
Potential crash in
compose()when examples don’t start with>>>- In
docstring_parser/numpydoc.py,compose()now appendsexample.snippetandexample.descriptiondirectly. If a NumpydocExamplessection begins with narrative text (no>>>),ExamplesSection.parse()yieldsDocstringExample(snippet=None, description="..."). That meansparts.append(example.snippet)insertsNone, and"\n".join(parts)will raiseTypeError. - This is a real-world pattern in Numpydoc (explanatory lines before prompts).
- Suggestion: guard before appending or coerce to
"", e.g.:- Append only if
example.snippetis notNone. - Append
example.descriptiononly if not empty.
- Append only if
- Relevant code:
docstring_parser/numpydoc.py(compose examples block).
- In
-
Defaults found in descriptions won’t render unless
is_optionalis True- In
compose(), the logic only emits, default=...whenone.is_optionalis true. But defaults found viaPARAM_DEFAULT_REGEX_IN_DESCdo not setis_optionalanywhere. - Result: a docstring with
arg : intand “Default: 10” in the description will parse a default but will not re-compose it, which is surprising given the new feature intent. - Suggestion: if a default is found in the description, consider setting
is_optional = TrueinParamSection._parse_item()or changecompose()to add defaults whendefaultis present regardless ofis_optional. - Relevant code:
docstring_parser/numpydoc.pyinParamSection._parse_item()andcompose().
- In
-
The new meta-check in tests is brittle
- The test in
docstring_parser/tests/test_numpydoc.pynow checksif "-" in source: assert len(docstring.meta) > 0. That will fail for future test cases that contain hyphens in prose but no section header (a pretty plausible addition). - Suggestion: use the parser’s title regex (e.g.,
parser_w_meta_section.titles_re.search(source)) rather than a generic"-"check. - Relevant code:
docstring_parser/tests/test_numpydoc.pyneartest_compose().
- The test in
Contributor
Author
|
@mauvilsa I've addressed the issues that were brought up:
|
mauvilsa
approved these changes
Mar 17, 2026
Contributor
Author
|
@mauvilsa would it be possible to make a new release version with this change sometime soon? I have a few projects I'd like to use it with. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See the parameters section of the numpydoc style guide for examples.
The ones provided there are:
I've added support for all three cases. I added some checks to the unit tests to verify that this works, both for parsing and composition. I did find that the existing regex for finding defaults had some issues. For example:
would return a default value of
s, since the regex would match the "word" afterdefault, which would be the characters. I fixed this original regex to account for this, and I added unit tests that test all the regexes for numpydoc parsing independently of the remaining classes, to double check their functionality.Additionally, while working on this I also found a few issues
test_numpydoc::test_composetest.Essentially, the test was passing, but only because the input strings were not actually valid numpydoc docstrings, and thus did not match any of the regular expressions. The entire text was treated as a description, so it matched the expected output.
For example:
the metadata title should not have a colon at the end, and thus the underline should be one shorter. The
abcdarg should also be shifted back a tab.I modified the test inputs to be valid numpydoc, which then resulted in some failures due to the output of compose not matching what the test expected (mainly missing or too many newlines), which I fixed. Some test cases used
Metatitles that were not in the default sections, which meant they were also being parsed as a description - I fixed this by making a custom parser with a few extra sections defined to cover these. To avoid this happening in the future, I also made a check to make sure that the parse operation was producing a docstring object with at least oneMetain themetalist, if expected.The check is a bit primitive, and perhaps a better option would be to add an additional argument like
meta_expectedto each test case, but it works for the current test suite.Finally, examples were not handled correctly in
compose. Only the description was being added to the composed docstring, not the snippet (caught this once I fixedtest_compose. I've also fixed that.