Repository navigation
fix(nbfmt): preserve Unicode separators in notebook JSON - #1432
BichengWang wants to merge 2 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a bug in the notebook formatter where specific Unicode line/paragraph separators were being incorrectly treated as line breaks during JSON serialization. By switching from splitlines() to a explicit newline split, the formatter now preserves these characters, ensuring that formatted notebooks remain valid JSON and byte-identical where appropriate. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the notebook formatter in tools/nbfmt/formatter.py to split JSON lines using '\n' instead of splitlines(), which preserves Unicode line separators within strings. Additionally, a comprehensive suite of regression tests has been added in tools/test_nbfmt.py to verify that Unicode separators are correctly preserved in markdown cells, outputs, and metadata. There are no review comments, and we have no feedback to provide.
|
Thanks @BichengWang , nice catch. Reproduced locally on main (a cell with "before\u2028after" comes out of nbfmt as invalid JSON, Invalid control character) and confirmed your branch fixes it with the text preserved. nbfmt --test on all 154 notebooks still passes and the 20 tests are green. One note: tools/test_nbfmt.py isn't picked up by CI yet (the workflow only runs tools/test_check_all_links.py). Leaving it to @Giom-V whether to keep the test file as-is, wire it into a workflow, or merge just the formatter fix. |
|
Added the formatter tests to the notebook workflow and enabled it for tool and workflow changes. All 20 formatter and link-checker tests pass locally. |
|
Thanks @BichengWang, the CI wiring looks right, the test is stdlib-only, the format/lint steps already handle a tools-only change, and the new step ran green on this PR. LGTM. Final call on the CI approach stays with @Giom-V |
|
Thanks @BichengWang we appreciate the careful work. Per the updated CONTRIBUTING.md, we now only take tools/ changes for problems that affect main today, and this case doesn't occur in the repo at the moment, so we're closing it. If it ever shows up in practice, we'll gladly revisit. |
Formatting a valid notebook containing U+0085, U+2028, or U+2029 currently writes invalid JSON.
json.dumps(..., ensure_ascii=False)preserves these characters inside strings, butsplitlines()treats them as line boundaries and the subsequent join inserts unescaped newlines into those strings. For example, formatting a markdown cell with source["before\u2028after"]makes the saved notebook failjson.loadswithInvalid control character.Split the serialized JSON on
\nonly. Add regression coverage for each separator in string and list cell sources, saved outputs, and metadata, including content preservation and formatting idempotence.Validation:
python3 -m unittest tools.test_nbfmt tools.test_check_all_links— 20 tests pass offline.git diff --checkpasses.Full unittest discovery also imports the optional notebook execution suite and is blocked by the environment's missing
nbformatdependency; the formatter and existing link-checker tests run without it.