-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Enable formatting for Jupyter notebooks #7749
Conversation
2068ea6
to
21f58ab
Compare
@@ -487,80 +508,3 @@ pub(crate) fn lint_stdin( | |||
notebook_indexes, | |||
}) | |||
} | |||
|
|||
#[derive(Debug)] | |||
pub(crate) struct LintSource(pub(crate) SourceKind); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this, and moved the logic into SourceKind
, so that it can be used for formatting too.
c59207d
to
8bcc8db
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. |
return Ok(None); | ||
} | ||
|
||
let mut output = String::with_capacity(notebook.source_code().len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid the allocation here by only reserving once we see the first formatted cell
f49f550
to
b55fc0a
Compare
8bcc8db
to
d97d454
Compare
Summary
This PR enables
ruff format
to format Jupyter notebooks.Most of the work is contained in a new
format_source
method that formats a genericSourceKind
, then returnsSome(transformed)
if the source required formatting, orNone
otherwise.Closes #7598.
Test Plan
Ran
cat foo.py | cargo run -p ruff_cli -- format --stdin-filename Untitled.ipynb
; verified that the console showed a reasonable error:warning: Failed to read notebook Untitled.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: EOF while parsing a value at line 1 column 0
Ran
cat Untitled.ipynb | cargo run -p ruff_cli -- format --stdin-filename Untitled.ipynb
; verified that the JSON output contained formatted source code.