Conversation
Sphinx-only directives such as versionchanged were treated as fatal ERROR-level messages, so defopt.run aborted when a function or a third-party type docstring contained them. Completely invalid RST still raises.
| # Consume the directive block the same way docutils does, then | ||
| # continue parsing instead of reporting an ERROR. | ||
| _indented, _indent, _offset, blank_finish = ( | ||
| self.state_machine.get_first_known_indented(0, strip_indent=False)) |
There was a problem hiding this comment.
Can you explain (briefly) the choice of using get_first_known_indented here rather than e.g. get_indented or get_known_indented?
TL;DR: it's the standard docutils way to "eat" an unknown directive so parsing can continue. |
| with self.assertRaises(SystemMessage): | ||
| defopt._parse_docstring(inspect.cleandoc(doc)) | ||
|
|
||
| def test_sphinx_directive_in_docstring(self): |
There was a problem hiding this comment.
There doesn't need to be three separate tests (they are effectively all exercising the same codepath). OTOH the test needs to check that the directive is indeed completely stripped and does not appear in the help-text.
|
Is the patch AI-generated? This is fine, but needs to be properly disclosed (in particular in the commit message). |
Summary
Fixes #133.
defopt.runaborted with adocutils.utils.SystemMessage(ERROR/3) when an introspected docstring contained a Sphinx-only directive such as.. versionchanged::. That happens both for the command function itself and for third-party types whose constructor docstring is parsed (the original report usedpathlib.Path)._parse_docstringalready registers a few Sphinx roles, but unknown directives still produced a fatal error becausehalt_level=3. This PR treats unknown directives as skippable (consume the block, emit no error) so parsing continues. Completely invalid RST still raises via the existing halt level.Test plan
test_sphinx_directive_in_docstring— parse a function docstring that contains.. versionchanged::and still extract the description +:param:docstest_sphinx_directive_does_not_abort_run—defopt.runsucceeds for that functiontest_sphinx_directive_in_type_docstring— a custom type whose class docstring contains a Sphinx directive no longer abortsdefopt.runtest_bad_docstill raisesSystemMessageon genuinely invalid RST