-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Sanitize invalid Python escape sequences in generated docstrings #47113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+235
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6aa545a
Sanitize invalid Python escape sequences in generated docstrings
msyyc 54dc39b
Address review feedback
msyyc 9bdb0b0
Merge branch 'main' into fix-generated-docstring-escapes
msyyc ec281ac
Address second review pass: remove unused Path import; correct stale …
msyyc f6795eb
Apply black formatting
msyyc d4f6047
Potential fix for pull request finding
msyyc fdc1b29
Merge branch 'main' into fix-generated-docstring-escapes
msyyc 72c0500
Fix sdk_generator: always create package_entry, not only on sanitize …
msyyc 6025a9b
black format
msyyc 0601ecd
Merge branch 'main' into fix-generated-docstring-escapes
msyyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
143 changes: 143 additions & 0 deletions
143
eng/tools/azure-sdk-tools/tests/test_sanitize_docstrings.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| from packaging_tools.generate_utils import ( | ||
| _TRIPLE_QUOTED_RE, | ||
| _fix_triple_quoted, | ||
| sanitize_generated_docstrings, | ||
| ) | ||
|
|
||
|
|
||
| def _run(src: str) -> str: | ||
| return _TRIPLE_QUOTED_RE.sub(_fix_triple_quoted, src) | ||
|
|
||
|
|
||
| def test_fixes_invalid_W_escape(): | ||
| src = '"""hi (Regex match [\\W_])"""' | ||
| assert _run(src) == '"""hi (Regex match [\\\\W_])"""' | ||
|
|
||
|
|
||
| def test_fixes_other_invalid_escapes(): | ||
| # \d \s are also invalid Python escapes | ||
| src = '"""\\d \\s \\p"""' | ||
| assert _run(src) == '"""\\\\d \\\\s \\\\p"""' | ||
|
|
||
|
|
||
| def test_idempotent(): | ||
| once = _run('"""[\\W_]"""') | ||
| assert _run(once) == once | ||
|
|
||
|
|
||
| def test_leaves_valid_escapes_alone(): | ||
| src = '"""line1\\nline2\\ttab \\\\backslash \\"quote\\""""' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_strings_alone(): | ||
| src = 'r"""[\\W_]"""' | ||
| assert _run(src) == src | ||
|
|
||
|
msyyc marked this conversation as resolved.
|
||
|
|
||
| def test_leaves_byte_strings_alone(): | ||
| # Byte triple-quoted strings are rare in docstrings but must not be touched. | ||
| # The regex matches them, but `_fix_triple_quoted` bails out because the | ||
| # captured prefix contains `b`. | ||
| src = 'b"""[\\W_]"""' | ||
|
msyyc marked this conversation as resolved.
|
||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_fstring_alone_rf_prefix(): | ||
| # Regression: ``rf"""..."""`` was previously sanitized because the | ||
| # lookbehind only saw ``f`` immediately before the quote. | ||
| src = 'rf"""[\\W_]"""' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_fstring_alone_fr_prefix(): | ||
| src = 'fr"""[\\W_]"""' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_byte_string_alone_rb_prefix(): | ||
| src = 'rb"""[\\W_]"""' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_byte_string_alone_br_prefix(): | ||
| src = 'br"""[\\W_]"""' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_raw_byte_string_alone_mixed_case_prefix(): | ||
| for src in ('Rb"""[\\W_]"""', 'bR"""[\\W_]"""', 'rF"""[\\W_]"""', 'Fr"""[\\W_]"""'): | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_leaves_single_quoted_strings_alone(): | ||
| # Generated docstrings are always triple-quoted; regex literals in | ||
| # generated code use double-quoted strings and must not be modified. | ||
| src = 'PATTERN = "\\W"' | ||
| assert _run(src) == src | ||
|
|
||
|
|
||
| def test_single_triple_quoted_string(): | ||
| src = "'''(Regex match [\\W_])'''" | ||
| assert _run(src) == "'''(Regex match [\\\\W_])'''" | ||
|
|
||
|
|
||
| def test_unicode_prefix_preserved(): | ||
| src = 'u"""[\\W_]"""' | ||
| assert _run(src) == 'u"""[\\\\W_]"""' | ||
|
|
||
|
|
||
| def test_fstring_prefix_preserved(): | ||
| src = 'f"""[\\W_]"""' | ||
| assert _run(src) == 'f"""[\\\\W_]"""' | ||
|
|
||
|
|
||
| def test_multiline_docstring(): | ||
| src = '"""line1\nHas a special character (Regex match [\\W_])\nline3"""' | ||
| expected = '"""line1\nHas a special character (Regex match [\\\\W_])\nline3"""' | ||
| assert _run(src) == expected | ||
|
|
||
|
|
||
| def test_sanitize_writes_only_changed_files(tmp_path): | ||
| # Simulate an sdk package layout: <pkg>/azure/<ns>/_models.py + _patch.py | ||
| pkg = tmp_path / "azure-mgmt-foo" | ||
| ns = pkg / "azure" / "mgmt" / "foo" | ||
| ns.mkdir(parents=True) | ||
| models = ns / "_models.py" | ||
| models.write_text( | ||
| 'class M:\n """Has [\\W_]"""\n pass\n', | ||
| encoding="utf-8", | ||
| ) | ||
| # _patch.py is hand-written; must never be touched even if it has invalid escapes | ||
| patch = ns / "_patch.py" | ||
| patch_src = '"""user-authored [\\W_]"""\n' | ||
| patch.write_text(patch_src, encoding="utf-8") | ||
| unchanged = ns / "_clean.py" | ||
| unchanged_src = '"""nothing to fix here"""\n' | ||
| unchanged.write_text(unchanged_src, encoding="utf-8") | ||
|
|
||
| sanitize_generated_docstrings(str(pkg)) | ||
|
|
||
| assert models.read_text(encoding="utf-8") == 'class M:\n """Has [\\\\W_]"""\n pass\n' | ||
| assert patch.read_text(encoding="utf-8") == patch_src | ||
| assert unchanged.read_text(encoding="utf-8") == unchanged_src | ||
|
|
||
|
|
||
| def test_sanitize_is_idempotent_on_disk(tmp_path): | ||
| pkg = tmp_path / "azure-mgmt-foo" | ||
| ns = pkg / "azure" / "mgmt" / "foo" | ||
| ns.mkdir(parents=True) | ||
| models = ns / "_models.py" | ||
| models.write_text('"""Has [\\W_]"""\n', encoding="utf-8") | ||
|
|
||
| sanitize_generated_docstrings(str(pkg)) | ||
| first = models.read_text(encoding="utf-8") | ||
| sanitize_generated_docstrings(str(pkg)) | ||
| assert models.read_text(encoding="utf-8") == first | ||
|
|
||
|
|
||
| def test_sanitize_missing_azure_dir_is_noop(tmp_path): | ||
| # No azure/ subdir -> nothing to do, no crash. | ||
| pkg = tmp_path / "azure-mgmt-foo" | ||
| pkg.mkdir() | ||
| sanitize_generated_docstrings(str(pkg)) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.