Repository navigation
Add try_parse, which never raises - #152
Merged
Merged
Conversation
try_parse() parses what it can of a docstring and never raises. It returns a ParseResult of the docstring and a list of problems, each with its line in the docstring as written. The text of a field or item that cannot be read goes to the long description. parse() is try_parse() plus a raise: one ParseError for the whole docstring, carrying every problem and the result. Its message is the first problem's, with how many more there are. In auto mode try_parse() keeps a style that reads the docstring with no problems, so it returns what parse() returns whenever parse() does.
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.
parse()stops at the first part of a docstring it cannot read. Users who want whatever can be read, such as documentation tools or a linter, have no way to get it. This addstry_parse(), which keeps going and reports what went wrong:The text of a field or item that cannot be read goes to the long description, so nothing is dropped. The no-drop invariant test now runs through
try_parse()on every case, including the onesparse()rejects. Lines count from 1 in the docstring as written, including blank lines before the text.parse()is nowtry_parse()plus a raise. It raises oneParseErrorfor the whole docstring.ParseError.problemslists every problem, andParseError.resultholds what was parsed anyway. The message is still the first problem's, with " (and 2 more)" added when there are others.There is no
strict=flag, and each function has one return type:parse()returns aDocstring,try_parse()aParseResult(docstring, problems). Every style module and everyParsergetstry_parse()too.In auto mode,
try_parse()prefers a style that reads the docstring with no problems, then the one with most meta. So wheneverparse()returns,try_parse()returns the same docstring with no problems. A test checks that for every style on every case. The downside: a Google docstring with one bad item comes back as a plain ReST description in auto mode, asparse()does today. Passing the style gives the Google reading with its problem.Parity against main: 749 of 29,074 docstrings differ, all in
parse(). Each one is aParseErrormessage that gained " (and N more)", and nothing else changed. They are 734 generated docstrings, 13 from the stdlib and 2 case inputs (Google and Epydoc account for nearly all of them, one is ReST). One case file changes for the same reason.Bench on 3.8: parse is 1.02–1.08x of main, the worst being ReST at 1.08. Compose is unchanged.