fix: escape invalid backslash sequences in generated docstrings#712
Open
Alan4506 wants to merge 3 commits into
Open
fix: escape invalid backslash sequences in generated docstrings#712Alan4506 wants to merge 3 commits into
Alan4506 wants to merge 3 commits into
Conversation
486a844 to
153a05e
Compare
Contributor
|
Are you able to add tests for these behaviors? I think it'd be valuable to add some tests with different scenarios. |
Contributor
Author
017b3d1 to
bba6f23
Compare
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.
Background
Generated docstrings are Python string literals, so every backslash must either form a valid escape sequence or the literal is malformed. Python 3.12+ emits a
SyntaxWarningfor unrecognized escape sequences, and strict type checkers (pyright) reportreportInvalidStringEscapeSequenceas an error, which fails codegen's post-generation type check.MarkdownConverter.postProcessPandocOutputis responsible for cleaning up the backslash escapes that pandoc adds. After #707 the single strip regex still produced invalid Python, reproducible with the Secrets Manager model.Problem
The original line in
MarkdownConverter.java:A Python string literal requires backslashes to come in pairs (each pair is one literal backslash). The trouble is that pandoc escapes literal backslashes (
\→\\) and Markdown-significant characters (*→\*) independently, and when the two sit next to each other the backslashes pile up into runs of varying length. A single strip regex cannot get every run right — it either consumes one half of a valid pair or leaves a lone backslash behind. Both happen with the Secrets Manager model, which documents password character sets that contain a literal\:\\, which pandoc preserves. The regex matched the second backslash followed by]and stripped it, turning the valid\\into a lone\— it broke a previously valid escape.Generated code before this fix
src/aws_sdk_secretsmanager/client.py(GetRandomPassword docstring):src/aws_sdk_secretsmanager/models.py(ExcludePunctuation docstring):pyright (run by
PythonFormatterduring codegen) fails the build:At runtime these also raise
SyntaxWarning: invalid escape sequence '\]'/'\ 'on import.The fix
Replace the strip regex with
normalizeBackslashEscapes, which handles each maximal run of backslashes (plus the character that follows it) as a unit instead of matching one backslash at a time. Because backslashes must be paired in a Python literal, therun / 2fully paired backslashes are always kept; if the run is odd, the one leftover backslash binds to the next character and is:\",\n, octal, hex, Unicode, line continuation),[,*), or\\.Working on whole runs keeps the result correct no matter how many backslashes pandoc emits, including odd-length runs where a literal backslash sits next to a Markdown character.
Generated code after this fix
src/aws_sdk_secretsmanager/client.py:src/aws_sdk_secretsmanager/models.py:Both are now valid Python:
[\\]renders as[\](a single literal backslash), matching the intended documentation.Testing
reportInvalidStringEscapeSequenceerrors, and the module imports underpython -W error::SyntaxWarningwith no warning. Also inspected the generated code as above.:core:test --tests "*MarkdownConverterTest*"passes, including regression tests for paired backslashes, lone backslashes, valid escapes, and backslashes adjacent to Markdown characters.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.