Skip to content
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

Add a NotebookError type to avoid returning Diagnostics on error #7035

Merged
merged 2 commits into from
Sep 1, 2023

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Aug 31, 2023

Summary

This PR refactors the error-handling cases around Jupyter notebooks to use errors rather than Box<Diagnostics>, which creates some oddities in the downstream handling. So, instead of formatting errors as diagnostics eagerly (in the notebook methods), we now return errors and convert those errors to diagnostics at the last possible moment (in diagnostics.rs). This is more ergonomic, as errors can be composed and reported-on in different ways, whereas diagnostics require a Printer, etc.

See, e.g., #7013 (comment).

Test Plan

Ran cargo run over a Python file labeled with a .ipynb suffix, and saw:

foo.ipynb:1:1: E999 SyntaxError: Expected a Jupyter Notebook, which must be internally stored as JSON, but found a Python source file: expected value at line 1 column 1

@charliermarsh charliermarsh marked this pull request as ready for review August 31, 2023 22:17
@charliermarsh charliermarsh added the internal An internal refactor or improvement label Aug 31, 2023
};

Ok(notebook)
fn notebook_from_path(path: &Path) -> Result<Option<Notebook>, NotebookError> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return Result<Option<Notebook>> instead of using empty Diagnostics to indicate the "we parsed the notebook, but it's not a Python notebook, so it should be skipped" case.

@charliermarsh
Copy link
Member Author

The jupyter module no longer depends on much in the ruff crate -- just the source map stuff in autofix. This PR removes the dependency on diagnostics, specific rules, etc. So we could move it into its own crate, if we want.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 31, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a ton! This is much better :)

TextRange::default(),
)
})?;
reader.rewind()?;
let mut raw_notebook: RawNotebook = match serde_json::from_reader(reader.by_ref()) {
Ok(notebook) => notebook,
Err(err) => {
// Translate the error into a diagnostic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can remove this comment now that the error isn't being converted to a diagnostic

@dhruvmanila
Copy link
Member

So we could move it into its own crate, if we want.

Yeah, that's nice. I think once we establish some abstraction around linting multiple filetypes it would be good to move this into it's own crate (ruff_notebook, ruff_ipython_notebook?)

crates/ruff_cli/src/diagnostics.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants