Fix handling of docstrings with tokenization errors (Fixes #18388) #18575
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.
This changes fixes #18388.
When parsing docstrings, stubgen will bail on a docstring when the first tokenization error is encountered. This behavior is brittle, because docstrings need not be entirely valid python and can contain characters that cause early failure. Consider the following example:
The presence of the LaTeX code will cause
TokenError
to occur, and the second overload will never get parsed.This change causes mypy to resume parsing after an error is occurred, such that later overloads can still be discovered. The new behavior is somewhat more robust to failures of this kind. I also added two tests with example docstrings that previously failed.